Skip to content

Commit

Permalink
Flavor extractor (#4274)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Oct 4, 2021
1 parent 11b2f16 commit 506ae2b
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package io.opentelemetry.instrumentation.api.instrumenter.http;

import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -34,10 +36,22 @@ protected final void onEnd(
@Nullable RESPONSE response,
@Nullable Throwable error) {
super.onEnd(attributes, request, response, error);
set(attributes, SemanticAttributes.HTTP_FLAVOR, flavor(request, response));
}

// Attributes that always exist in a request

@Nullable
protected abstract String url(REQUEST request);

// Attributes which are not always available when the request is ready.

/**
* Extracts the {@code http.flavor} span attribute.
*
* <p>This is called from {@link Instrumenter#end(Context, Object, Object, Throwable)}, whether
* {@code response} is {@code null} or not.
*/
@Nullable
protected abstract String flavor(REQUEST request, @Nullable RESPONSE response);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ protected void onEnd(
attributes,
SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED,
requestContentLengthUncompressed(request, response));
set(attributes, SemanticAttributes.HTTP_FLAVOR, flavor(request, response));
if (response != null) {
Integer statusCode = statusCode(request, response);
if (statusCode != null) {
Expand Down Expand Up @@ -90,15 +89,6 @@ protected void onEnd(
protected abstract Long requestContentLengthUncompressed(
REQUEST request, @Nullable RESPONSE response);

/**
* Extracts the {@code http.flavor} span attribute.
*
* <p>This is called from {@link Instrumenter#end(Context, Object, Object, Throwable)}, whether
* {@code response} is {@code null} or not.
*/
@Nullable
protected abstract String flavor(REQUEST request, @Nullable RESPONSE response);

/**
* Extracts the {@code http.status_code} span attribute.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public abstract class HttpServerAttributesExtractor<REQUEST, RESPONSE>
protected final void onStart(AttributesBuilder attributes, REQUEST request) {
super.onStart(attributes, request);

set(attributes, SemanticAttributes.HTTP_FLAVOR, flavor(request));
set(attributes, SemanticAttributes.HTTP_SCHEME, scheme(request));
set(attributes, SemanticAttributes.HTTP_HOST, host(request));
set(attributes, SemanticAttributes.HTTP_TARGET, target(request));
Expand All @@ -46,6 +47,9 @@ protected final void onEnd(

// Attributes that always exist in a request

@Nullable
protected abstract String flavor(REQUEST request);

@Nullable
protected abstract String target(REQUEST request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected Integer statusCode(Map<String, String> request, Map<String, String> re
}

@Override
protected String flavor(Map<String, String> request, Map<String, String> response) {
protected String flavor(Map<String, String> request) {
return request.get("flavor");
}

Expand Down Expand Up @@ -114,6 +114,7 @@ void normal() {
extractor.onStart(attributes, request);
assertThat(attributes.build())
.containsOnly(
entry(SemanticAttributes.HTTP_FLAVOR, "http/2"),
entry(SemanticAttributes.HTTP_METHOD, "POST"),
entry(SemanticAttributes.HTTP_TARGET, "/repositories/1"),
entry(SemanticAttributes.HTTP_HOST, "github.com:80"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected Integer statusCode(RequestContext ctx, RequestLog requestLog) {
}

@Override
protected String flavor(RequestContext ctx, @Nullable RequestLog requestLog) {
protected String flavor(RequestContext ctx) {
SessionProtocol protocol = ctx.sessionProtocol();
if (protocol.isMultiplex()) {
return SemanticAttributes.HttpFlavorValues.HTTP_2_0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public class LibertyDispatcherHttpAttributesExtractor
}

@Override
protected @Nullable String flavor(
LibertyRequest libertyRequest, @Nullable LibertyResponse libertyResponse) {
protected @Nullable String flavor(LibertyRequest libertyRequest) {
String flavor = libertyRequest.getProtocol();
if (flavor != null) {
// remove HTTP/ prefix to comply with semantic conventions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected Long requestContentLengthUncompressed(Request request, @Nullable Respo

@Override
@Nullable
protected String flavor(Request request, @Nullable Response response) {
protected String flavor(Request request) {
switch (request.getProtocol()) {
case "HTTP/1.0":
return SemanticAttributes.HttpFlavorValues.HTTP_1_0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected String host(Request request) {
}

@Override
protected @Nullable String flavor(Request request, @Nullable Response response) {
protected @Nullable String flavor(Request request) {
String version = (String) request.getAttributes().get("org.restlet.http.version");
switch (version) {
case "HTTP/1.0":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ public ServletHttpAttributesExtractor(ServletAccessor<REQUEST, RESPONSE> accesso
}

@Override
protected @Nullable String flavor(
ServletRequestContext<REQUEST> requestContext,
@Nullable ServletResponseContext<RESPONSE> responseContext) {
protected @Nullable String flavor(ServletRequestContext<REQUEST> requestContext) {
String flavor = accessor.getRequestProtocol(requestContext.request());
if (flavor != null) {
// remove HTTP/ prefix to comply with semantic conventions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ final class SpringWebMvcHttpAttributesExtractor
}

@Override
protected @Nullable String flavor(
HttpServletRequest request, @Nullable HttpServletResponse response) {
protected @Nullable String flavor(HttpServletRequest request) {
return request.getProtocol();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected String method(Request request) {
}

@Override
protected @Nullable String flavor(Request request, @Nullable Response response) {
protected @Nullable String flavor(Request request) {
String flavor = request.protocol().toString();
if (flavor != null) {
// remove HTTP/ prefix to comply with semantic conventions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected String method(HttpServerExchange exchange) {
}

@Override
protected String flavor(HttpServerExchange exchange, @Nullable HttpServerExchange unused) {
protected String flavor(HttpServerExchange exchange) {
String flavor = exchange.getProtocol().toString();
// remove HTTP/ prefix to comply with semantic conventions
if (flavor.startsWith("HTTP/")) {
Expand Down

0 comments on commit 506ae2b

Please sign in to comment.