Transition http-url-connection module to instrumenter API (#4044)

* Convert http-url-connection to instrumenter API

* Replace HttpUrlResponse with response status code Integer
This commit is contained in:
jack-berg 2021-09-07 16:54:16 -05:00 committed by GitHub
parent 23fc4ce443
commit f89933b67c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 167 additions and 101 deletions

View File

@ -7,7 +7,7 @@ package io.opentelemetry.javaagent.instrumentation.httpurlconnection;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass;
import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.instrumentation.httpurlconnection.HttpUrlConnectionTracer.tracer;
import static io.opentelemetry.javaagent.instrumentation.httpurlconnection.HttpUrlConnectionSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
@ -76,7 +76,7 @@ public class HttpUrlConnectionInstrumentation implements TypeInstrumentation {
return;
}
Context parentContext = currentContext();
if (!tracer().shouldStartSpan(parentContext)) {
if (!instrumenter().shouldStart(parentContext, connection)) {
return;
}
@ -94,7 +94,7 @@ public class HttpUrlConnectionInstrumentation implements TypeInstrumentation {
return;
}
Context context = tracer().startSpan(parentContext, connection);
Context context = instrumenter().start(parentContext, connection);
httpUrlState = new HttpUrlState(context);
storage.put(connection, httpUrlState);
scope = context.makeCurrent();
@ -122,16 +122,16 @@ public class HttpUrlConnectionInstrumentation implements TypeInstrumentation {
// HttpURLConnection unnecessarily throws exception on error response.
// None of the other http clients do this, so not recording the exception on the span
// to be consistent with the telemetry for other http clients.
tracer().end(httpUrlState.context, new HttpUrlResponse(connection, responseCode));
instrumenter().end(httpUrlState.context, connection, responseCode, null);
} else {
tracer().endExceptionally(httpUrlState.context, throwable);
instrumenter().end(httpUrlState.context, connection, null, throwable);
}
httpUrlState.finished = true;
} else if (methodName.equals("getInputStream") && responseCode > 0) {
// responseCode field is sometimes not populated.
// We can't call getResponseCode() due to some unwanted side-effects
// (e.g. breaks getOutputStream).
tracer().end(httpUrlState.context, new HttpUrlResponse(connection, responseCode));
instrumenter().end(httpUrlState.context, connection, responseCode, null);
httpUrlState.finished = true;
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.httpurlconnection;
import static io.opentelemetry.javaagent.instrumentation.httpurlconnection.HeadersInjectAdapter.SETTER;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanNameExtractor;
import io.opentelemetry.javaagent.instrumentation.api.instrumenter.PeerServiceAttributesExtractor;
import java.net.HttpURLConnection;
public class HttpUrlConnectionSingletons {
private static final Instrumenter<HttpURLConnection, Integer> INSTRUMENTER;
static {
HttpUrlHttpAttributesExtractor httpAttributesExtractor = new HttpUrlHttpAttributesExtractor();
HttpUrlNetAttributesExtractor netAttributesExtractor = new HttpUrlNetAttributesExtractor();
SpanNameExtractor<HttpURLConnection> spanNameExtractor =
HttpSpanNameExtractor.create(httpAttributesExtractor);
INSTRUMENTER =
Instrumenter.<HttpURLConnection, Integer>newBuilder(
GlobalOpenTelemetry.get(),
"io.opentelemetry.http-url-connection",
spanNameExtractor)
.addAttributesExtractor(httpAttributesExtractor)
.addAttributesExtractor(netAttributesExtractor)
.addAttributesExtractor(PeerServiceAttributesExtractor.create(netAttributesExtractor))
.newClientInstrumenter(SETTER);
}
public static Instrumenter<HttpURLConnection, Integer> instrumenter() {
return INSTRUMENTER;
}
}

View File

@ -1,69 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.httpurlconnection;
import static io.opentelemetry.javaagent.instrumentation.httpurlconnection.HeadersInjectAdapter.SETTER;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer;
import io.opentelemetry.instrumentation.api.tracer.net.NetPeerAttributes;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
public class HttpUrlConnectionTracer
extends HttpClientTracer<HttpURLConnection, HttpURLConnection, HttpUrlResponse> {
private static final HttpUrlConnectionTracer TRACER = new HttpUrlConnectionTracer();
private HttpUrlConnectionTracer() {
super(NetPeerAttributes.INSTANCE);
}
public static HttpUrlConnectionTracer tracer() {
return TRACER;
}
public Context startSpan(Context parentContext, HttpURLConnection request) {
return super.startSpan(parentContext, request, request);
}
@Override
protected String method(HttpURLConnection connection) {
return connection.getRequestMethod();
}
@Override
protected URI url(HttpURLConnection connection) throws URISyntaxException {
return connection.getURL().toURI();
}
@Override
protected Integer status(HttpUrlResponse response) {
return response.status();
}
@Override
protected String requestHeader(HttpURLConnection httpUrlConnection, String name) {
return httpUrlConnection.getRequestProperty(name);
}
@Override
protected String responseHeader(HttpUrlResponse response, String name) {
return response.header(name);
}
@Override
protected TextMapSetter<HttpURLConnection> getSetter() {
return SETTER;
}
@Override
protected String getInstrumentationName() {
return "io.opentelemetry.http-url-connection";
}
}

View File

@ -0,0 +1,87 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.httpurlconnection;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpAttributesExtractor;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.net.HttpURLConnection;
import org.checkerframework.checker.nullness.qual.Nullable;
class HttpUrlHttpAttributesExtractor extends HttpAttributesExtractor<HttpURLConnection, Integer> {
@Override
protected String method(HttpURLConnection connection) {
return connection.getRequestMethod();
}
@Override
protected String url(HttpURLConnection connection) {
return connection.getURL().toExternalForm();
}
@Override
protected @Nullable String target(HttpURLConnection connection) {
return null;
}
@Override
protected @Nullable String host(HttpURLConnection connection) {
return null;
}
@Override
protected @Nullable String route(HttpURLConnection connection) {
return null;
}
@Override
protected @Nullable String scheme(HttpURLConnection connection) {
return null;
}
@Override
protected @Nullable String userAgent(HttpURLConnection connection) {
return connection.getRequestProperty("User-Agent");
}
@Override
protected @Nullable Long requestContentLength(
HttpURLConnection connection, @Nullable Integer statusCode) {
return null;
}
@Override
protected @Nullable Long requestContentLengthUncompressed(
HttpURLConnection connection, @Nullable Integer response) {
return null;
}
@Override
protected String flavor(HttpURLConnection connection, @Nullable Integer statusCode) {
return SemanticAttributes.HttpFlavorValues.HTTP_1_1;
}
@Override
protected @Nullable String serverName(
HttpURLConnection connection, @Nullable Integer statusCode) {
return null;
}
@Override
protected Integer statusCode(HttpURLConnection connection, Integer statusCode) {
return statusCode;
}
@Override
protected @Nullable Long responseContentLength(HttpURLConnection connection, Integer statusCode) {
return null;
}
@Override
protected @Nullable Long responseContentLengthUncompressed(
HttpURLConnection connection, Integer statusCode) {
return null;
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.httpurlconnection;
import io.opentelemetry.instrumentation.api.instrumenter.net.NetAttributesExtractor;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.net.HttpURLConnection;
import org.checkerframework.checker.nullness.qual.Nullable;
class HttpUrlNetAttributesExtractor extends NetAttributesExtractor<HttpURLConnection, Integer> {
@Override
public @Nullable String transport(HttpURLConnection connection) {
return SemanticAttributes.NetTransportValues.IP_TCP;
}
@Override
public String peerName(HttpURLConnection connection, @Nullable Integer statusCode) {
return connection.getURL().getHost();
}
@Override
public Integer peerPort(HttpURLConnection connection, @Nullable Integer statusCode) {
return connection.getURL().getPort();
}
@Override
public @Nullable String peerIp(HttpURLConnection connection, @Nullable Integer statusCode) {
return null;
}
}

View File

@ -1,26 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.httpurlconnection;
import java.net.HttpURLConnection;
public class HttpUrlResponse {
private final HttpURLConnection connection;
private final int resolvedResponseCode;
public HttpUrlResponse(HttpURLConnection connection, int resolvedResponseCode) {
this.connection = connection;
this.resolvedResponseCode = resolvedResponseCode;
}
int status() {
return resolvedResponseCode;
}
String header(String name) {
return connection.getHeaderField(name);
}
}