Skip to content

Commit

Permalink
make DefaultHttpClientTelemetryBuilder internal
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Jun 21, 2024
1 parent d40519b commit a753b80
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.incubator.builder.internal;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreCommonConfig;
import java.lang.reflect.Field;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public class HttpClientInstrumenterBuilder {
private HttpClientInstrumenterBuilder() {}

@CanIgnoreReturnValue
public static <REQUEST, RESPONSE> DefaultHttpClientTelemetryBuilder<REQUEST, RESPONSE> configure(
CoreCommonConfig config, Object builder) {
DefaultHttpClientTelemetryBuilder<REQUEST, RESPONSE> defaultBuilder = unwrapBuilder(builder);
set(config::getKnownHttpRequestMethods, defaultBuilder::setKnownMethods);
set(config::getClientRequestHeaders, defaultBuilder::setCapturedRequestHeaders);
set(config::getClientResponseHeaders, defaultBuilder::setCapturedResponseHeaders);
set(config::getPeerServiceResolver, defaultBuilder::setPeerServiceResolver);
set(
config::shouldEmitExperimentalHttpClientTelemetry,
defaultBuilder::setEmitExperimentalHttpClientMetrics);
return defaultBuilder;
}

private static <T> void set(Supplier<T> supplier, Consumer<T> consumer) {
T t = supplier.get();
if (t != null) {
consumer.accept(t);
}
}

/**
* This method is used to access the builder field of the builder object.
*
* <p>This approach allows us to re-use the existing builder classes from the library modules
*/
@SuppressWarnings("unchecked")
private static <REQUEST, RESPONSE>
DefaultHttpClientTelemetryBuilder<REQUEST, RESPONSE> unwrapBuilder(Object builder) {
if (builder instanceof DefaultHttpClientTelemetryBuilder<?, ?>) {
return (DefaultHttpClientTelemetryBuilder<REQUEST, RESPONSE>) builder;
}
try {
Field field = builder.getClass().getDeclaredField("builder");
field.setAccessible(true);
return (DefaultHttpClientTelemetryBuilder<REQUEST, RESPONSE>) field.get(builder);
} catch (Exception e) {
throw new IllegalStateException("Could not access builder field", e);
}
}
}
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.instrumentation.api.incubator.internal.config;
package io.opentelemetry.instrumentation.api.incubator.config.internal;

import java.util.function.Consumer;
import java.util.function.Supplier;
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.instrumentation.api.incubator.internal.config;
package io.opentelemetry.instrumentation.api.incubator.config.internal;

import static java.util.Collections.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.instrumentation.api.incubator.internal.config;
package io.opentelemetry.instrumentation.api.incubator.config.internal;

import static java.util.Collections.emptyList;

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.instrumentation.api.incubator.internal.config;
package io.opentelemetry.instrumentation.api.incubator.config.internal;

import java.util.Objects;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.semconv.util.ClassAndMethod;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.logs.LogRecordBuilder;
import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.log4j.appender.v2_17.internal.ContextDataAccessor;
import io.opentelemetry.instrumentation.log4j.appender.v2_17.internal.LogEventMapper;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import static java.util.Collections.emptyList;

import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.logback.appender.v1_0.internal.LoggingEventMapper;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import io.micrometer.core.instrument.MeterRegistry;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.micrometer.v1_5.OpenTelemetryMeterRegistry;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.internal.instrumenter.HttpClientInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.HttpClientInstrumenterBuilder;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.CommonConfigUtil;
import io.opentelemetry.instrumentation.spring.web.v3_1.SpringWebTelemetry;
import io.opentelemetry.instrumentation.spring.web.v3_1.SpringWebTelemetryBuilder;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import java.util.List;
import org.springframework.http.client.ClientHttpRequestInterceptor;
Expand All @@ -23,11 +24,9 @@ private RestTemplateInstrumentation() {}
static RestTemplate addIfNotPresent(
RestTemplate restTemplate, OpenTelemetry openTelemetry, ConfigProperties config) {

ClientHttpRequestInterceptor instrumentationInterceptor =
HttpClientInstrumenterBuilder.configure(
CommonConfigUtil.getCommonConfig(config), SpringWebTelemetry.builder(openTelemetry))
.build()
.newInterceptor();
SpringWebTelemetryBuilder builder = SpringWebTelemetry.builder(openTelemetry);
HttpClientInstrumenterBuilder.configure(CommonConfigUtil.getCommonConfig(config), builder);
ClientHttpRequestInterceptor instrumentationInterceptor = builder.build().newInterceptor();

List<ClientHttpRequestInterceptor> restTemplateInterceptors = restTemplate.getInterceptors();
if (restTemplateInterceptors.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties;

import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreCommonConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreCommonConfig;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties;

import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreInstrumentationConfig;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import java.time.Duration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.javaagent.bootstrap.internal;

import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreCommonConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreCommonConfig;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static java.util.Collections.emptyList;
import static java.util.logging.Level.WARNING;

import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreInstrumentationConfig;
import java.util.List;
import java.util.logging.Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.javaagent.bootstrap.internal;

import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreInstrumentationConfig;
import java.time.Duration;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import static java.util.Collections.emptyList;

import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreInstrumentationConfig;
import java.util.List;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import static java.util.Objects.requireNonNull;

import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreInstrumentationConfig;
import java.util.logging.Logger;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientTelemetryBuilder;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.HttpClientInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter;
import java.lang.reflect.Field;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
Expand Down Expand Up @@ -50,44 +49,7 @@ public static <REQUEST, RESPONSE> Instrumenter<REQUEST, RESPONSE> createWithCust
public static <REQUEST, RESPONSE> Instrumenter<REQUEST, RESPONSE> createWithCustomizer(
Object builder,
Consumer<InstrumenterBuilder<REQUEST, RESPONSE>> instrumenterBuilderConsumer) {
CommonConfig config = CommonConfig.get();
DefaultHttpClientTelemetryBuilder<REQUEST, RESPONSE> defaultBuilder = unwrapBuilder(builder);
set(config::getKnownHttpRequestMethods, defaultBuilder::setKnownMethods);
set(config::getClientRequestHeaders, defaultBuilder::setCapturedRequestHeaders);
set(config::getClientResponseHeaders, defaultBuilder::setCapturedResponseHeaders);
// is not exposed in the public API
set(config::getPeerServiceResolver, defaultBuilder::setPeerServiceResolver);
set(
config::shouldEmitExperimentalHttpClientTelemetry,
defaultBuilder::setEmitExperimentalHttpClientMetrics);
// is not exposed in the public API
return defaultBuilder.instrumenter(instrumenterBuilderConsumer);
}

/**
* This method is used to access the builder field of the builder object.
*
* <p>This approach allows us to re-use the existing builder classes from the library modules
*/
@SuppressWarnings("unchecked")
private static <REQUEST, RESPONSE>
DefaultHttpClientTelemetryBuilder<REQUEST, RESPONSE> unwrapBuilder(Object builder) {
if (builder instanceof DefaultHttpClientTelemetryBuilder<?, ?>) {
return (DefaultHttpClientTelemetryBuilder<REQUEST, RESPONSE>) builder;
}
try {
Field field = builder.getClass().getDeclaredField("builder");
field.setAccessible(true);
return (DefaultHttpClientTelemetryBuilder<REQUEST, RESPONSE>) field.get(builder);
} catch (Exception e) {
throw new IllegalStateException("Could not access builder field", e);
}
}

private static <T> void set(Supplier<T> supplier, Consumer<T> consumer) {
T t = supplier.get();
if (t != null) {
consumer.accept(t);
}
return HttpClientInstrumenterBuilder.<REQUEST, RESPONSE>configure(CommonConfig.get(), builder)
.instrumenter(instrumenterBuilderConsumer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.javaagent.tooling.config;

import io.opentelemetry.instrumentation.api.incubator.internal.config.CoreInstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreInstrumentationConfig;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import java.time.Duration;
Expand Down

0 comments on commit a753b80

Please sign in to comment.