Skip to content

Commit

Permalink
config properties support for spring starter clients (#11605)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger authored Jul 2, 2024
1 parent a7dbc71 commit 798bdd5
Show file tree
Hide file tree
Showing 158 changed files with 869 additions and 540 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CommonConfig;
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpClientExperimentalMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpClientPeerServiceAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpExperimentalAttributesExtractor;
Expand All @@ -29,6 +30,7 @@
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -192,4 +194,23 @@ public Instrumenter<REQUEST, RESPONSE> build() {
public OpenTelemetry getOpenTelemetry() {
return openTelemetry;
}

@CanIgnoreReturnValue
public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> configure(CommonConfig config) {
set(config::getKnownHttpRequestMethods, this::setKnownMethods);
set(config::getClientRequestHeaders, this::setCapturedRequestHeaders);
set(config::getClientResponseHeaders, this::setCapturedResponseHeaders);
set(config::getPeerServiceResolver, this::setPeerServiceResolver);
set(
config::shouldEmitExperimentalHttpClientTelemetry,
this::setEmitExperimentalHttpClientMetrics);
return this;
}

private static <T> void set(Supplier<T> supplier, Consumer<T> consumer) {
T t = supplier.get();
if (t != null) {
consumer.accept(t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.bootstrap.internal;
package io.opentelemetry.instrumentation.api.incubator.config.internal;

import static java.util.Collections.emptyMap;

Expand All @@ -21,12 +21,6 @@
*/
public final class CommonConfig {

private static final CommonConfig instance = new CommonConfig(InstrumentationConfig.get());

public static CommonConfig get() {
return instance;
}

private final PeerServiceResolver peerServiceResolver;
private final List<String> clientRequestHeaders;
private final List<String> clientResponseHeaders;
Expand All @@ -41,7 +35,7 @@ public static CommonConfig get() {
private final String loggingSpanIdKey;
private final String loggingTraceFlagsKey;

CommonConfig(InstrumentationConfig config) {
public CommonConfig(InstrumentationConfig config) {
peerServiceResolver =
PeerServiceResolver.create(
config.getMap("otel.instrumentation.common.peer-service-mapping", emptyMap()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.bootstrap.internal;
package io.opentelemetry.instrumentation.api.incubator.config.internal;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.bootstrap.internal;
package io.opentelemetry.instrumentation.api.incubator.config.internal;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.annotation.Nullable;

/**
Expand All @@ -28,72 +26,44 @@
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public abstract class InstrumentationConfig {

private static final Logger logger = Logger.getLogger(InstrumentationConfig.class.getName());

private static final InstrumentationConfig DEFAULT = new EmptyInstrumentationConfig();

// lazy initialized, so that javaagent can set it
private static volatile InstrumentationConfig instance = DEFAULT;

/**
* Sets the instrumentation configuration singleton. This method is only supposed to be called
* once, during the agent initialization, just before {@link InstrumentationConfig#get()} is used
* for the first time.
*
* <p>This method is internal and is hence not for public use. Its API is unstable and can change
* at any time.
*/
public static void internalInitializeConfig(InstrumentationConfig config) {
if (instance != DEFAULT) {
logger.warning("InstrumentationConfig#instance was already set earlier");
return;
}
instance = requireNonNull(config);
}

/** Returns the global instrumentation configuration. */
public static InstrumentationConfig get() {
return instance;
}
public interface InstrumentationConfig {

/**
* Returns a string-valued configuration property or {@code null} if a property with name {@code
* name} has not been configured.
*/
@Nullable
public abstract String getString(String name);
String getString(String name);

/**
* Returns a string-valued configuration property or {@code defaultValue} if a property with name
* {@code name} has not been configured.
*/
public abstract String getString(String name, String defaultValue);
String getString(String name, String defaultValue);

/**
* Returns a boolean-valued configuration property or {@code defaultValue} if a property with name
* {@code name} has not been configured.
*/
public abstract boolean getBoolean(String name, boolean defaultValue);
boolean getBoolean(String name, boolean defaultValue);

/**
* Returns an integer-valued configuration property or {@code defaultValue} if a property with
* name {@code name} has not been configured or when parsing has failed.
*/
public abstract int getInt(String name, int defaultValue);
int getInt(String name, int defaultValue);

/**
* Returns a long-valued configuration property or {@code defaultValue} if a property with name
* {@code name} has not been configured or when parsing has failed.
*/
public abstract long getLong(String name, long defaultValue);
long getLong(String name, long defaultValue);

/**
* Returns a double-valued configuration property or {@code defaultValue} if a property with name
* {@code name} has not been configured or when parsing has failed.
*/
public abstract double getDouble(String name, double defaultValue);
double getDouble(String name, double defaultValue);

/**
* Returns a duration-valued configuration property or {@code defaultValue} if a property with
Expand All @@ -113,13 +83,13 @@ public static InstrumentationConfig get() {
*
* <p>Examples: 10s, 20ms, 5000
*/
public abstract Duration getDuration(String name, Duration defaultValue);
Duration getDuration(String name, Duration defaultValue);

/**
* This is the same as calling {@code getList(String, List)} with the defaultValue equal to the
* emptyList()/
*/
public List<String> getList(String name) {
default List<String> getList(String name) {
return getList(name, emptyList());
}

Expand All @@ -128,13 +98,13 @@ public List<String> getList(String name) {
* {@code name} has not been configured. The format of the original value must be comma-separated,
* e.g. {@code one,two,three}. The returned list is unmodifiable.
*/
public abstract List<String> getList(String name, List<String> defaultValue);
List<String> getList(String name, List<String> defaultValue);

/**
* Returns a map-valued configuration property or {@code defaultValue} if a property with name
* {@code name} has not been configured or when parsing has failed. The format of the original
* value must be comma-separated for each key, with an '=' separating the key and value, e.g.
* {@code key=value,anotherKey=anotherValue}. The returned map is unmodifiable.
*/
public abstract Map<String, String> getMap(String name, Map<String, String> defaultValue);
Map<String, String> getMap(String name, Map<String, String> defaultValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanNameExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanStatusExtractor;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import io.opentelemetry.javaagent.instrumentation.akkahttp.AkkaHttpUtil;

public final class AkkaHttpServerSingletons {
Expand All @@ -31,21 +31,21 @@ public final class AkkaHttpServerSingletons {
GlobalOpenTelemetry.get(),
AkkaHttpUtil.instrumentationName(),
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpServerAttributesExtractor.builder(httpAttributesGetter)
.setCapturedRequestHeaders(CommonConfig.get().getServerRequestHeaders())
.setCapturedResponseHeaders(CommonConfig.get().getServerResponseHeaders())
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.setCapturedRequestHeaders(AgentCommonConfig.get().getServerRequestHeaders())
.setCapturedResponseHeaders(AgentCommonConfig.get().getServerResponseHeaders())
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build())
.addOperationMetrics(HttpServerMetrics.get())
.addContextCustomizer(
HttpServerRoute.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build());
if (CommonConfig.get().shouldEmitExperimentalHttpServerTelemetry()) {
if (AgentCommonConfig.get().shouldEmitExperimentalHttpServerTelemetry()) {
builder
.addAttributesExtractor(HttpExperimentalAttributesExtractor.create(httpAttributesGetter))
.addOperationMetrics(HttpServerExperimentalMetrics.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.opentelemetry.instrumentation.apachedubbo.v2_7.DubboTelemetry;
import io.opentelemetry.instrumentation.apachedubbo.v2_7.internal.DubboClientNetworkAttributesGetter;
import io.opentelemetry.instrumentation.api.incubator.semconv.net.PeerServiceAttributesExtractor;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
Expand All @@ -27,7 +27,7 @@ public OpenTelemetryFilter() {
.addAttributesExtractor(
PeerServiceAttributesExtractor.create(
new DubboClientNetworkAttributesGetter(),
CommonConfig.get().getPeerServiceResolver()))
AgentCommonConfig.get().getPeerServiceResolver()))
.build()
.newFilter();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.LocalRootSpan;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
import org.apache.shenyu.common.dto.MetaData;

public final class MetaDataHelper {
Expand Down Expand Up @@ -58,7 +58,7 @@ public final class MetaDataHelper {

static {
CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES =
InstrumentationConfig.get()
AgentInstrumentationConfig.get()
.getBoolean("otel.instrumentation.apache-shenyu.experimental-span-attributes", false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpClientPeerServiceAttributesExtractor;
import io.opentelemetry.instrumentation.armeria.v1_3.ArmeriaTelemetry;
import io.opentelemetry.instrumentation.armeria.v1_3.internal.ArmeriaHttpClientAttributesGetter;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import java.util.function.Function;

// Holds singleton references to decorators to match against during suppression.
Expand All @@ -24,19 +24,19 @@ public final class ArmeriaSingletons {
static {
ArmeriaTelemetry telemetry =
ArmeriaTelemetry.builder(GlobalOpenTelemetry.get())
.setCapturedClientRequestHeaders(CommonConfig.get().getClientRequestHeaders())
.setCapturedClientResponseHeaders(CommonConfig.get().getClientResponseHeaders())
.setCapturedServerRequestHeaders(CommonConfig.get().getServerRequestHeaders())
.setCapturedServerResponseHeaders(CommonConfig.get().getServerResponseHeaders())
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.setCapturedClientRequestHeaders(AgentCommonConfig.get().getClientRequestHeaders())
.setCapturedClientResponseHeaders(AgentCommonConfig.get().getClientResponseHeaders())
.setCapturedServerRequestHeaders(AgentCommonConfig.get().getServerRequestHeaders())
.setCapturedServerResponseHeaders(AgentCommonConfig.get().getServerResponseHeaders())
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.addClientAttributeExtractor(
HttpClientPeerServiceAttributesExtractor.create(
ArmeriaHttpClientAttributesGetter.INSTANCE,
CommonConfig.get().getPeerServiceResolver()))
AgentCommonConfig.get().getPeerServiceResolver()))
.setEmitExperimentalHttpClientMetrics(
CommonConfig.get().shouldEmitExperimentalHttpClientTelemetry())
AgentCommonConfig.get().shouldEmitExperimentalHttpClientTelemetry())
.setEmitExperimentalHttpServerMetrics(
CommonConfig.get().shouldEmitExperimentalHttpServerTelemetry())
AgentCommonConfig.get().shouldEmitExperimentalHttpServerTelemetry())
.build();

CLIENT_DECORATOR = telemetry.newClientDecorator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.awslambdaevents.v2_2.internal.AwsLambdaEventsInstrumenterFactory;
import io.opentelemetry.instrumentation.awslambdaevents.v2_2.internal.AwsLambdaSqsInstrumenterFactory;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;

public final class AwsLambdaInstrumentationHelper {

private static final io.opentelemetry.instrumentation.awslambdacore.v1_0.internal
.AwsLambdaFunctionInstrumenter
FUNCTION_INSTRUMENTER =
AwsLambdaEventsInstrumenterFactory.createInstrumenter(
GlobalOpenTelemetry.get(), CommonConfig.get().getKnownHttpRequestMethods());
GlobalOpenTelemetry.get(), AgentCommonConfig.get().getKnownHttpRequestMethods());

public static io.opentelemetry.instrumentation.awslambdacore.v1_0.internal
.AwsLambdaFunctionInstrumenter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.awssdk.v1_11.AwsSdkTelemetry;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
import io.opentelemetry.javaagent.bootstrap.internal.ExperimentalConfig;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;

/**
* A {@link RequestHandler2} for use in the agent. Unlike library instrumentation, the agent will
Expand All @@ -36,7 +36,7 @@ public class TracingRequestHandler extends RequestHandler2 {
public static final RequestHandler2 tracingHandler =
AwsSdkTelemetry.builder(GlobalOpenTelemetry.get())
.setCaptureExperimentalSpanAttributes(
InstrumentationConfig.get()
AgentInstrumentationConfig.get()
.getBoolean("otel.instrumentation.aws-sdk.experimental-span-attributes", false))
.setMessagingReceiveInstrumentationEnabled(
ExperimentalConfig.get().messagingReceiveInstrumentationEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
import io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkTelemetry;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
import io.opentelemetry.javaagent.bootstrap.internal.ExperimentalConfig;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import java.util.List;

public final class AwsSdkSingletons {
Expand All @@ -28,7 +28,7 @@ public final class AwsSdkSingletons {

private static boolean hasAgentConfiguration() {
try {
Class.forName("io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig");
Class.forName("io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig");
return true;
} catch (ClassNotFoundException e) {
return false;
Expand Down Expand Up @@ -69,7 +69,7 @@ private static boolean recordIndividualHttpError() {

private static boolean getBoolean(String name, boolean defaultValue) {
if (HAS_INSTRUMENTATION_CONFIG) {
return InstrumentationConfig.get().getBoolean(name, defaultValue);
return AgentInstrumentationConfig.get().getBoolean(name, defaultValue);
} else {
return ConfigPropertiesUtil.getBoolean(name, defaultValue);
}
Expand Down
Loading

0 comments on commit 798bdd5

Please sign in to comment.