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

Remove old HTTP semconv code #9968

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -19,47 +19,25 @@ final class CapturedHttpHeadersUtil {
// these are naturally bounded because they only store keys listed in
// otel.instrumentation.http.server.capture-request-headers and
// otel.instrumentation.http.server.capture-response-headers
private static final ConcurrentMap<String, AttributeKey<List<String>>>
oldSemconvRequestKeysCache = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, AttributeKey<List<String>>>
stableSemconvRequestKeysCache = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, AttributeKey<List<String>>>
oldSemconvResponseKeysCache = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, AttributeKey<List<String>>>
stableSemconvResponseKeysCache = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, AttributeKey<List<String>>> requestKeysCache =
new ConcurrentHashMap<>();
private static final ConcurrentMap<String, AttributeKey<List<String>>> responseKeysCache =
new ConcurrentHashMap<>();

static List<String> lowercase(List<String> names) {
return unmodifiableList(
names.stream().map(s -> s.toLowerCase(Locale.ROOT)).collect(Collectors.toList()));
}

static AttributeKey<List<String>> oldSemconvRequestAttributeKey(String headerName) {
return oldSemconvRequestKeysCache.computeIfAbsent(
headerName, n -> createOldSemconvKey("request", n));
static AttributeKey<List<String>> requestAttributeKey(String headerName) {
return requestKeysCache.computeIfAbsent(headerName, n -> createKey("request", n));
}

static AttributeKey<List<String>> stableSemconvRequestAttributeKey(String headerName) {
return stableSemconvRequestKeysCache.computeIfAbsent(
headerName, n -> createStableSemconvKey("request", n));
static AttributeKey<List<String>> responseAttributeKey(String headerName) {
return responseKeysCache.computeIfAbsent(headerName, n -> createKey("response", n));
}

static AttributeKey<List<String>> oldSemconvResponseAttributeKey(String headerName) {
return oldSemconvResponseKeysCache.computeIfAbsent(
headerName, n -> createOldSemconvKey("response", n));
}

static AttributeKey<List<String>> stableSemconvResponseAttributeKey(String headerName) {
return stableSemconvResponseKeysCache.computeIfAbsent(
headerName, n -> createStableSemconvKey("response", n));
}

private static AttributeKey<List<String>> createOldSemconvKey(String type, String headerName) {
// headerName is always lowercase, see CapturedHttpHeadersUtil#lowercase
String key = "http." + type + ".header." + headerName.replace('-', '_');
return AttributeKey.stringArrayKey(key);
}

private static AttributeKey<List<String>> createStableSemconvKey(String type, String headerName) {
private static AttributeKey<List<String>> createKey(String type, String headerName) {
// headerName is always lowercase, see CapturedHttpHeadersUtil#lowercase
String key = "http." + type + ".header." + headerName;
return AttributeKey.stringArrayKey(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.http.internal.HttpAttributes;
import io.opentelemetry.instrumentation.api.instrumenter.net.internal.InternalNetClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalNetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import io.opentelemetry.instrumentation.api.internal.SpanKey;
import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider;
import io.opentelemetry.semconv.SemanticAttributes;
Expand All @@ -42,81 +40,39 @@ public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
return builder(httpAttributesGetter).build();
}

/**
* Creates the HTTP client attributes extractor with default configuration.
*
* @deprecated Make sure that your {@linkplain HttpClientAttributesGetter getter} implements all
* the network-related methods and use {@link #create(HttpClientAttributesGetter)} instead.
* This method will be removed in the 2.0 release.
*/
@Deprecated
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
HttpClientAttributesGetter<REQUEST, RESPONSE> httpAttributesGetter,
io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter<
REQUEST, RESPONSE>
netAttributesGetter) {
return builder(httpAttributesGetter, netAttributesGetter).build();
}

/**
* Returns a new {@link HttpClientAttributesExtractorBuilder} that can be used to configure the
* HTTP client attributes extractor.
*/
public static <REQUEST, RESPONSE> HttpClientAttributesExtractorBuilder<REQUEST, RESPONSE> builder(
HttpClientAttributesGetter<REQUEST, RESPONSE> httpAttributesGetter) {
return new HttpClientAttributesExtractorBuilder<>(httpAttributesGetter, httpAttributesGetter);
return new HttpClientAttributesExtractorBuilder<>(httpAttributesGetter);
}

/**
* Returns a new {@link HttpClientAttributesExtractorBuilder} that can be used to configure the
* HTTP client attributes extractor.
*
* @deprecated Make sure that your {@linkplain HttpClientAttributesGetter getter} implements all
* the network-related methods and use {@link #builder(HttpClientAttributesGetter)} instead.
* This method will be removed in the 2.0 release.
*/
@Deprecated
public static <REQUEST, RESPONSE> HttpClientAttributesExtractorBuilder<REQUEST, RESPONSE> builder(
HttpClientAttributesGetter<REQUEST, RESPONSE> httpAttributesGetter,
io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter<
REQUEST, RESPONSE>
netAttributesGetter) {
return new HttpClientAttributesExtractorBuilder<>(httpAttributesGetter, netAttributesGetter);
}

private final InternalNetClientAttributesExtractor<REQUEST, RESPONSE> internalNetExtractor;
private final InternalNetworkAttributesExtractor<REQUEST, RESPONSE> internalNetworkExtractor;
private final InternalServerAttributesExtractor<REQUEST> internalServerExtractor;
private final ToIntFunction<Context> resendCountIncrementer;

HttpClientAttributesExtractor(HttpClientAttributesExtractorBuilder<REQUEST, RESPONSE> builder) {
super(
builder.httpAttributesGetter,
builder.netAttributesGetter,
HttpStatusCodeConverter.CLIENT,
builder.capturedRequestHeaders,
builder.capturedResponseHeaders,
builder.knownMethods);
internalNetExtractor = builder.buildNetExtractor();
internalNetworkExtractor = builder.buildNetworkExtractor();
internalServerExtractor = builder.buildServerExtractor();
resendCountIncrementer = builder.resendCountIncrementer;
}

@Override
@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
super.onStart(attributes, parentContext, request);

internalServerExtractor.onStart(attributes, request);

String fullUrl = stripSensitiveData(getter.getUrlFull(request));
if (SemconvStability.emitStableHttpSemconv()) {
internalSet(attributes, SemanticAttributes.URL_FULL, fullUrl);
}
if (SemconvStability.emitOldHttpSemconv()) {
internalSet(attributes, SemanticAttributes.HTTP_URL, fullUrl);
}
internalSet(attributes, SemanticAttributes.URL_FULL, fullUrl);

int resendCount = resendCountIncrementer.applyAsInt(parentContext);
if (resendCount > 0) {
Expand All @@ -133,7 +89,6 @@ public void onEnd(
@Nullable Throwable error) {
super.onEnd(attributes, context, request, response, error);

internalNetExtractor.onEnd(attributes, request, response);
internalNetworkExtractor.onEnd(attributes, request, response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.net.internal.InternalNetClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.AddressAndPortExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalNetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.ServerAddressAndPortExtractor;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand All @@ -29,28 +27,18 @@ public final class HttpClientAttributesExtractorBuilder<REQUEST, RESPONSE> {

final HttpClientAttributesGetter<REQUEST, RESPONSE> httpAttributesGetter;

@SuppressWarnings("deprecation") // using the net extractor for the old->stable semconv story
final io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter<
REQUEST, RESPONSE>
netAttributesGetter;

final AddressAndPortExtractor<REQUEST> serverAddressAndPortExtractor;
List<String> capturedRequestHeaders = emptyList();
List<String> capturedResponseHeaders = emptyList();
Set<String> knownMethods = HttpConstants.KNOWN_METHODS;
ToIntFunction<Context> resendCountIncrementer = HttpClientRequestResendCount::getAndIncrement;

HttpClientAttributesExtractorBuilder(
HttpClientAttributesGetter<REQUEST, RESPONSE> httpAttributesGetter,
@SuppressWarnings("deprecation") // using the net extractor for the old->stable semconv story
io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter<
REQUEST, RESPONSE>
netAttributesGetter) {
HttpClientAttributesGetter<REQUEST, RESPONSE> httpAttributesGetter) {
this.httpAttributesGetter = httpAttributesGetter;
this.netAttributesGetter = netAttributesGetter;
serverAddressAndPortExtractor =
new ServerAddressAndPortExtractor<>(
netAttributesGetter, new HostAddressAndPortExtractor<>(httpAttributesGetter));
httpAttributesGetter, new HostAddressAndPortExtractor<>(httpAttributesGetter));
}

/**
Expand Down Expand Up @@ -129,28 +117,15 @@ public AttributesExtractor<REQUEST, RESPONSE> build() {
return new HttpClientAttributesExtractor<>(this);
}

InternalNetClientAttributesExtractor<REQUEST, RESPONSE> buildNetExtractor() {
return new InternalNetClientAttributesExtractor<>(
netAttributesGetter, serverAddressAndPortExtractor, SemconvStability.emitOldHttpSemconv());
}

InternalNetworkAttributesExtractor<REQUEST, RESPONSE> buildNetworkExtractor() {
return new InternalNetworkAttributesExtractor<>(
netAttributesGetter,
serverAddressAndPortExtractor,
httpAttributesGetter,
// network.{transport,type} are opt-in, network.protocol.* have HTTP-specific logic
/* captureProtocolAttributes= */ false,
/* captureLocalSocketAttributes= */ false,
/* captureOldPeerDomainAttribute= */ true,
SemconvStability.emitStableHttpSemconv(),
SemconvStability.emitOldHttpSemconv());
/* captureLocalSocketAttributes= */ false);
}

InternalServerAttributesExtractor<REQUEST> buildServerExtractor() {
return new InternalServerAttributesExtractor<>(
serverAddressAndPortExtractor,
SemconvStability.emitStableHttpSemconv(),
SemconvStability.emitOldHttpSemconv(),
InternalServerAttributesExtractor.Mode.PEER);
return new InternalServerAttributesExtractor<>(serverAddressAndPortExtractor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@
* library/framework. It will be used by the {@link HttpClientAttributesExtractor} to obtain the
* various HTTP client attributes in a type-generic way.
*/
@SuppressWarnings(
"deprecation") // implementing the NetClientAttributesGetter for the old->stable semconv story;
// will be removed in 2.0
public interface HttpClientAttributesGetter<REQUEST, RESPONSE>
extends HttpCommonAttributesGetter<REQUEST, RESPONSE>,
io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesGetter<
REQUEST, RESPONSE>,
NetworkAttributesGetter<REQUEST, RESPONSE>,
ServerAttributesGetter<REQUEST, RESPONSE> {
ServerAttributesGetter<REQUEST> {

/**
* Returns the absolute URL describing a network resource according to <a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package io.opentelemetry.instrumentation.api.instrumenter.http;

import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.createStableDurationHistogramBuilder;
import static java.util.logging.Level.FINE;

import com.google.auto.value.AutoValue;
Expand All @@ -18,10 +17,8 @@
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
import io.opentelemetry.instrumentation.api.instrumenter.OperationMetrics;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javax.annotation.Nullable;

/**
* {@link OperationListener} which keeps track of <a
Expand All @@ -30,7 +27,6 @@
*/
public final class HttpClientMetrics implements OperationListener {

private static final double NANOS_PER_MS = TimeUnit.MILLISECONDS.toNanos(1);
private static final double NANOS_PER_S = TimeUnit.SECONDS.toNanos(1);

private static final ContextKey<State> HTTP_CLIENT_REQUEST_METRICS_STATE =
Expand All @@ -48,30 +44,17 @@ public static OperationMetrics get() {
return HttpClientMetrics::new;
}

@Nullable private final DoubleHistogram stableDuration;
@Nullable private final DoubleHistogram oldDuration;
private final DoubleHistogram duration;

private HttpClientMetrics(Meter meter) {
if (SemconvStability.emitStableHttpSemconv()) {
DoubleHistogramBuilder stableDurationBuilder =
createStableDurationHistogramBuilder(
meter, "http.client.request.duration", "Duration of HTTP client requests.");
HttpMetricsAdvice.applyStableClientDurationAdvice(stableDurationBuilder);
stableDuration = stableDurationBuilder.build();
} else {
stableDuration = null;
}
if (SemconvStability.emitOldHttpSemconv()) {
DoubleHistogramBuilder oldDurationBuilder =
meter
.histogramBuilder("http.client.duration")
.setUnit("ms")
.setDescription("The duration of the outbound HTTP request");
HttpMetricsAdvice.applyOldClientDurationAdvice(oldDurationBuilder);
oldDuration = oldDurationBuilder.build();
} else {
oldDuration = null;
}
DoubleHistogramBuilder stableDurationBuilder =
meter
.histogramBuilder("http.client.request.duration")
.setUnit("s")
.setDescription("Duration of HTTP client requests.")
.setExplicitBucketBoundariesAdvice(HttpMetricsAdvice.DURATION_SECONDS_BUCKETS);
HttpMetricsAdvice.applyClientDurationAdvice(stableDurationBuilder);
duration = stableDurationBuilder.build();
}

@Override
Expand All @@ -94,13 +77,7 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {

Attributes attributes = state.startAttributes().toBuilder().putAll(endAttributes).build();

if (stableDuration != null) {
stableDuration.record((endNanos - state.startTimeNanos()) / NANOS_PER_S, attributes, context);
}

if (oldDuration != null) {
oldDuration.record((endNanos - state.startTimeNanos()) / NANOS_PER_MS, attributes, context);
}
duration.record((endNanos - state.startTimeNanos()) / NANOS_PER_S, attributes, context);
}

@AutoValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.PeerServiceResolver;
import io.opentelemetry.instrumentation.api.instrumenter.net.internal.UrlParser;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.function.Supplier;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -66,11 +65,6 @@ public void onEnd(
Integer serverPort = attributesGetter.getServerPort(request);
Supplier<String> pathSupplier = () -> getUrlPath(attributesGetter, request);
String peerService = mapToPeerService(serverAddress, serverPort, pathSupplier);
if (peerService == null && SemconvStability.emitOldHttpSemconv()) {
String serverSocketDomain = attributesGetter.getServerSocketDomain(request, response);
Integer serverSocketPort = attributesGetter.getServerSocketPort(request, response);
peerService = mapToPeerService(serverSocketDomain, serverSocketPort, null);
}
if (peerService != null) {
attributes.put(SemanticAttributes.PEER_SERVICE, peerService);
}
Expand Down
Loading
Loading