Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add telemetry to GCS #334

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2024 Aiven Oy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.aiven.kafka.connect.common.config.validators;

import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.common.config.ConfigException;

public class ClassValidator implements ConfigDef.Validator {
private final Class<?> baseClass;

public ClassValidator(final Class<?> baseClass) {
this.baseClass = baseClass;
}

@Override
public void ensureValid(final String name, final Object value) {
if (value != null && !baseClass.isAssignableFrom((Class<?>) value)) {
throw new ConfigException(name, value, "must be a subclass of " + baseClass.getName());
}
}
}
Comment on lines +1 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead bring this validator implementation here? IMO it makes code a little more readable when you write Subclass.of(SomeClass.class) rather then new ClassValidator(SomeClass.class)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a common library that you use for these. It seems copying and duplication is a bad way forward, particularly for a common concept like validation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no common library yet and when I created the validator I'm suggesting I did not manage to find a ready to use implementation.
I think for now copying is good enough but in future I think if this pattern will repeat we will consider creating our own commons library to address this.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.api.gax.tracing.ApiTracerFactory;
import io.aiven.kafka.connect.common.config.validators.ClassValidator;
import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.common.config.types.Password;
Expand Down Expand Up @@ -59,6 +61,7 @@ public final class GcsSinkConfig extends AivenCommonConfig {
public static final String GCS_BUCKET_NAME_CONFIG = "gcs.bucket.name";
public static final String GCS_OBJECT_CONTENT_ENCODING_CONFIG = "gcs.object.content.encoding";
public static final String GCS_USER_AGENT = "gcs.user.agent";
public static final String GCS_METRICS = "gcs.metrics.class";
private static final String GROUP_FILE = "File";
public static final String FILE_NAME_PREFIX_CONFIG = "file.name.prefix";
public static final String FILE_NAME_TEMPLATE_CONFIG = "file.name.template";
Expand Down Expand Up @@ -144,14 +147,16 @@ private static void addGcsConfigGroup(final ConfigDef configDef) {

configDef.define(GCS_BUCKET_NAME_CONFIG, ConfigDef.Type.STRING, ConfigDef.NO_DEFAULT_VALUE,
new ConfigDef.NonEmptyString(), ConfigDef.Importance.HIGH,
"The GCS bucket name to store output files in.", GROUP_GCS, gcsGroupCounter++, ConfigDef.Width.NONE, // NOPMD
// the
// gcsGroupCounter
// updated
// value
// never
// used
"The GCS bucket name to store output files in.", GROUP_GCS, gcsGroupCounter++, ConfigDef.Width.NONE,
GCS_BUCKET_NAME_CONFIG);
configDef.define(GCS_METRICS, ConfigDef.Type.CLASS,
null, new ClassValidator(ApiTracerFactory.class),
ConfigDef.Importance.LOW,
"class for GCS metrics. Default is not to attache metrics",
GROUP_GCS, gcsGroupCounter++, ConfigDef.Width.NONE, // NOPMD
// retryPolicyGroupCounter updated value never used
GCS_METRICS);

}

private static void addGcsRetryPolicies(final ConfigDef configDef) {
Expand Down Expand Up @@ -438,4 +443,15 @@ public String getGcsEndpoint() {
public String getUserAgent() {
return getString(GCS_USER_AGENT);
}

public ApiTracerFactory getApiTracerFactory() {
if (getClass(GCS_METRICS) == null) {
return null;
}
try {
return getClass(GCS_METRICS).asSubclass(ApiTracerFactory.class).getDeclaredConstructor().newInstance();
Copy link
Contributor

@AnatolyPopov AnatolyPopov Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that not all implementations of ApiTracerFactory have a default constructor that you are using here. So I'm just curious what implementation is supposed to be used here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that not all implementations of ApiTracerFactory have a default constructor
I was going to use a non open source one.

Happy to add a more complex builder mechanism, with some standard implementations for suggested Tracers

} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be ConfigException with the message clarifying why the instantiation of ApiTracerFactory failed and which class is expected there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Just my lazyness - its just that ConfigException doesn't follow normal exception chaining patterns

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void start(final Map<String, String> props) {
Objects.requireNonNull(props, "props cannot be null");

this.config = new GcsSinkConfig(props);
this.storage = StorageOptions.newBuilder()
StorageOptions.Builder builder = StorageOptions.newBuilder()
.setHost(config.getGcsEndpoint())
.setCredentials(config.getCredentials())
.setHeaderProvider(FixedHeaderProvider.create(USER_AGENT_HEADER_KEY, config.getUserAgent()))
Expand All @@ -81,8 +81,11 @@ public void start(final Map<String, String> props) {
.setRetryDelayMultiplier(config.getGcsRetryBackoffDelayMultiplier())
.setTotalTimeout(config.getGcsRetryBackoffTotalTimeout())
.setMaxAttempts(config.getGcsRetryBackoffMaxAttempts())
.build())
.build()
.build());
if (config.getApiTracerFactory() != null) {
builder.setApiTracerFactory(config.getApiTracerFactory());
}
this.storage = builder.build()
.getService();
initRest();
if (Objects.nonNull(config.getKafkaRetryBackoffMs())) {
Expand Down
Loading