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

Use HTTP instead of non-known method in http span name #9441

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Changes from 3 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
@@ -6,6 +6,8 @@
package io.opentelemetry.instrumentation.api.instrumenter.http;

import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Nullable;

/**
@@ -17,25 +19,47 @@
public final class HttpSpanNameExtractor<REQUEST> implements SpanNameExtractor<REQUEST> {

/**
* Returns a {@link SpanNameExtractor} which should be used for HTTP requests. HTTP attributes
* will be examined to determine the name of the span.
* Returns a {@link SpanNameExtractor} which should be used for HTTP requests with default
* configuration. HTTP attributes will be examined to determine the name of the span.
*/
public static <REQUEST> SpanNameExtractor<REQUEST> create(
HttpCommonAttributesGetter<REQUEST, ?> getter) {
return new HttpSpanNameExtractor<>(getter);
return builder(getter).build();
}

/*
public static <REQUEST> SpanNameExtractor<REQUEST> create(
HttpCommonAttributesGetter<REQUEST, ?> getter, Set<String> knownMethods) {
return new HttpSpanNameExtractor<>(getter, knownMethods);
laurit marked this conversation as resolved.
Show resolved Hide resolved
}

*/
laurit marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns a new {@link HttpSpanNameExtractorBuilder} that can be used to configure the HTTP span
* name extractor.
*/
public static <REQUEST> HttpSpanNameExtractorBuilder<REQUEST> builder(
HttpCommonAttributesGetter<REQUEST, ?> getter) {
return new HttpSpanNameExtractorBuilder<>(getter);
}

private final HttpCommonAttributesGetter<REQUEST, ?> getter;
private final Set<String> knownMethods;

private HttpSpanNameExtractor(HttpCommonAttributesGetter<REQUEST, ?> getter) {
this.getter = getter;
HttpSpanNameExtractor(HttpSpanNameExtractorBuilder<REQUEST> builder) {
this.getter = builder.httpAttributesGetter;
this.knownMethods = new HashSet<>(builder.knownMethods);
}

@Override
public String extract(REQUEST request) {
String method = getter.getHttpRequestMethod(request);
String route = extractRoute(request);
if (method != null) {
if (!knownMethods.contains(method)) {
method = "HTTP";
}
return route == null ? method : method + " " + route;
}
return "HTTP";
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import java.util.HashSet;
import java.util.Set;

/** A builder of {@link HttpSpanNameExtractor}. */
public final class HttpSpanNameExtractorBuilder<REQUEST> {

final HttpCommonAttributesGetter<REQUEST, ?> httpAttributesGetter;
Set<String> knownMethods = HttpConstants.KNOWN_METHODS;

HttpSpanNameExtractorBuilder(HttpCommonAttributesGetter<REQUEST, ?> httpAttributesGetter) {
this.httpAttributesGetter = httpAttributesGetter;
}

/**
* Configures the extractor to recognize an alternative set of HTTP request methods.
*
* <p>By default, this extractor defines "known" methods as the ones listed in <a
* href="https://www.rfc-editor.org/rfc/rfc9110.html#name-methods">RFC9110</a> and the PATCH
* method defined in <a href="https://www.rfc-editor.org/rfc/rfc5789.html">RFC5789</a>. If an
* unknown method is encountered, the extractor will use the value {@value HttpConstants#_OTHER}
* instead of it and put the original value in an extra {@code http.request.method_original}
* attribute.
*
* <p>Note: calling this method <b>overrides</b> the default known method sets completely; it does
* not supplement it.
*
* @param knownMethods A set of recognized HTTP request methods.
*/
@CanIgnoreReturnValue
public HttpSpanNameExtractorBuilder<REQUEST> setKnownMethods(Set<String> knownMethods) {
this.knownMethods = new HashSet<>(knownMethods);
return this;
}

/**
* Returns a new {@link HttpSpanNameExtractor} with the settings of this {@link
* HttpSpanNameExtractorBuilder}.
*/
public HttpSpanNameExtractor<REQUEST> build() {
laurit marked this conversation as resolved.
Show resolved Hide resolved
return new HttpSpanNameExtractor<>(this);
}
}
Original file line number Diff line number Diff line change
@@ -32,7 +32,9 @@ public class AkkaHttpClientSingletons {
Instrumenter.<HttpRequest, HttpResponse>builder(
GlobalOpenTelemetry.get(),
AkkaHttpUtil.instrumentationName(),
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpClientAttributesExtractor.builder(httpAttributesGetter)
Original file line number Diff line number Diff line change
@@ -29,7 +29,9 @@ public final class AkkaHttpServerSingletons {
Instrumenter.<HttpRequest, HttpResponse>builder(
GlobalOpenTelemetry.get(),
AkkaHttpUtil.instrumentationName(),
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpServerAttributesExtractor.builder(httpAttributesGetter)
Original file line number Diff line number Diff line change
@@ -30,7 +30,9 @@ public final class ApacheHttpAsyncClientSingletons {
Instrumenter.<ApacheHttpClientRequest, HttpResponse>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpClientAttributesExtractor.builder(httpAttributesGetter)
Original file line number Diff line number Diff line change
@@ -30,7 +30,9 @@ public final class ApacheHttpClientSingletons {
Instrumenter.<HttpMethod, HttpMethod>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpClientAttributesExtractor.builder(httpAttributesGetter)
Original file line number Diff line number Diff line change
@@ -30,7 +30,9 @@ public final class ApacheHttpClientSingletons {
Instrumenter.<ApacheHttpClientRequest, HttpResponse>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpClientAttributesExtractor.builder(httpAttributesGetter)
Original file line number Diff line number Diff line change
@@ -17,7 +17,9 @@
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientMetrics;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanStatusExtractor;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.http.HttpResponse;
@@ -35,6 +37,7 @@ public final class ApacheHttpClientTelemetryBuilder {
httpAttributesExtractorBuilder =
HttpClientAttributesExtractor.builder(ApacheHttpClientHttpAttributesGetter.INSTANCE);
private boolean emitExperimentalHttpClientMetrics = false;
private Set<String> knownMethods = HttpConstants.KNOWN_METHODS;
laurit marked this conversation as resolved.
Show resolved Hide resolved

ApacheHttpClientTelemetryBuilder(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
@@ -90,6 +93,7 @@ public ApacheHttpClientTelemetryBuilder setCapturedResponseHeaders(List<String>
@CanIgnoreReturnValue
public ApacheHttpClientTelemetryBuilder setKnownMethods(Set<String> knownMethods) {
httpAttributesExtractorBuilder.setKnownMethods(knownMethods);
this.knownMethods = new HashSet<>(knownMethods);
return this;
}

@@ -118,7 +122,9 @@ public ApacheHttpClientTelemetry build() {
Instrumenter.<ApacheHttpClientRequest, HttpResponse>builder(
openTelemetry,
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(knownMethods)
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(httpAttributesExtractorBuilder.build())
.addAttributesExtractors(additionalExtractors)
Original file line number Diff line number Diff line change
@@ -31,7 +31,9 @@ public final class ApacheHttpClientSingletons {
Instrumenter.<HttpRequest, HttpResponse>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpClientAttributesExtractor.builder(httpAttributesGetter)
Original file line number Diff line number Diff line change
@@ -26,9 +26,11 @@
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerRoute;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanStatusExtractor;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import io.opentelemetry.instrumentation.armeria.v1_3.internal.ArmeriaHttpClientAttributesGetter;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
@@ -43,6 +45,7 @@ public final class ArmeriaTelemetryBuilder {
@Nullable private String peerService;
private boolean emitExperimentalHttpClientMetrics = false;
private boolean emitExperimentalHttpServerMetrics = false;
private Set<String> knownMethods = HttpConstants.KNOWN_METHODS;

private final List<AttributesExtractor<? super RequestContext, ? super RequestLog>>
additionalExtractors = new ArrayList<>();
@@ -167,6 +170,7 @@ public ArmeriaTelemetryBuilder setCapturedServerResponseHeaders(List<String> res
public ArmeriaTelemetryBuilder setKnownMethods(Set<String> knownMethods) {
httpClientAttributesExtractorBuilder.setKnownMethods(knownMethods);
httpServerAttributesExtractorBuilder.setKnownMethods(knownMethods);
this.knownMethods = new HashSet<>(knownMethods);
return this;
}

@@ -206,12 +210,16 @@ public ArmeriaTelemetry build() {
Instrumenter.builder(
openTelemetry,
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(clientAttributesGetter));
HttpSpanNameExtractor.builder(clientAttributesGetter)
.setKnownMethods(knownMethods)
.build());
InstrumenterBuilder<ServiceRequestContext, RequestLog> serverInstrumenterBuilder =
Instrumenter.builder(
openTelemetry,
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(serverAttributesGetter));
HttpSpanNameExtractor.builder(serverAttributesGetter)
.setKnownMethods(knownMethods)
.build());

Stream.of(clientInstrumenterBuilder, serverInstrumenterBuilder)
.forEach(instrumenter -> instrumenter.addAttributesExtractors(additionalExtractors));
Original file line number Diff line number Diff line change
@@ -31,7 +31,9 @@ public final class AsyncHttpClientSingletons {
Instrumenter.<Request, Response>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpClientAttributesExtractor.builder(httpAttributesGetter)
Original file line number Diff line number Diff line change
@@ -30,7 +30,9 @@ public final class AsyncHttpClientSingletons {
Instrumenter.<RequestContext, Response>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpClientAttributesExtractor.builder(httpAttributesGetter)
Original file line number Diff line number Diff line change
@@ -31,7 +31,9 @@ public class GoogleHttpClientSingletons {
Instrumenter.<HttpRequest, HttpResponse>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpClientAttributesExtractor.builder(httpAttributesGetter)
Original file line number Diff line number Diff line change
@@ -30,7 +30,9 @@ public final class GrizzlySingletons {
Instrumenter.<HttpRequestPacket, HttpResponsePacket>builder(
GlobalOpenTelemetry.get(),
"io.opentelemetry.grizzly-2.3",
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpServerAttributesExtractor.builder(httpAttributesGetter)
Original file line number Diff line number Diff line change
@@ -28,7 +28,9 @@ public final class HttpUrlConnectionSingletons {
Instrumenter.<HttpURLConnection, Integer>builder(
GlobalOpenTelemetry.get(),
"io.opentelemetry.http-url-connection",
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(CommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpClientAttributesExtractor.builder(httpAttributesGetter)
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ public class JavaHttpClientSingletons {
PeerServiceAttributesExtractor.create(
JavaHttpClientAttributesGetter.INSTANCE,
CommonConfig.get().getPeerServiceMapping())),
CommonConfig.get().getKnownHttpRequestMethods(),
CommonConfig.get().shouldEmitExperimentalHttpClientMetrics());
}

Original file line number Diff line number Diff line change
@@ -10,11 +10,13 @@
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractorBuilder;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import io.opentelemetry.instrumentation.httpclient.internal.HttpHeadersSetter;
import io.opentelemetry.instrumentation.httpclient.internal.JavaHttpClientInstrumenterFactory;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
@@ -25,9 +27,9 @@ public final class JavaHttpClientTelemetryBuilder {

private final List<AttributesExtractor<? super HttpRequest, ? super HttpResponse<?>>>
additionalExtractors = new ArrayList<>();

private Consumer<HttpClientAttributesExtractorBuilder<HttpRequest, HttpResponse<?>>>
extractorConfigurer = builder -> {};
private Set<String> knownMethods = HttpConstants.KNOWN_METHODS;
private boolean emitExperimentalHttpClientMetrics = false;

JavaHttpClientTelemetryBuilder(OpenTelemetry openTelemetry) {
@@ -86,6 +88,7 @@ public JavaHttpClientTelemetryBuilder setCapturedResponseHeaders(List<String> re
public JavaHttpClientTelemetryBuilder setKnownMethods(Set<String> knownMethods) {
extractorConfigurer =
extractorConfigurer.andThen(builder -> builder.setKnownMethods(knownMethods));
this.knownMethods = new HashSet<>(knownMethods);
return this;
}

@@ -108,6 +111,7 @@ public JavaHttpClientTelemetry build() {
openTelemetry,
extractorConfigurer,
additionalExtractors,
knownMethods,
emitExperimentalHttpClientMetrics);

return new JavaHttpClientTelemetry(
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

/**
@@ -33,6 +34,7 @@ public static Instrumenter<HttpRequest, HttpResponse<?>> createInstrumenter(
Consumer<HttpClientAttributesExtractorBuilder<HttpRequest, HttpResponse<?>>>
extractorConfigurer,
List<AttributesExtractor<? super HttpRequest, ? super HttpResponse<?>>> additionalExtractors,
Set<String> knownMethods,
boolean emitExperimentalHttpClientMetrics) {

JavaHttpClientAttributesGetter httpAttributesGetter = JavaHttpClientAttributesGetter.INSTANCE;
@@ -46,7 +48,9 @@ public static Instrumenter<HttpRequest, HttpResponse<?>> createInstrumenter(
Instrumenter.<HttpRequest, HttpResponse<?>>builder(
openTelemetry,
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributesGetter))
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(knownMethods)
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(httpAttributesExtractorBuilder.build())
.addAttributesExtractors(additionalExtractors)
Loading