Skip to content

Commit

Permalink
Created OkHttpInstrumentationConfig to allow configuring automatic tr…
Browse files Browse the repository at this point in the history
…aces created for okhtp
  • Loading branch information
LikeTheSalad committed Sep 18, 2023
1 parent 36d9a4d commit b778cb0
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.instrumentation.agent.okhttp.v3_0;

import io.opentelemetry.instrumentation.library.okhttp.v3_0.OkHttp3Singletons;
import io.opentelemetry.instrumentation.library.okhttp.v3_0.internal.OkHttp3Singletons;
import net.bytebuddy.asm.Advice;
import okhttp3.OkHttpClient;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package io.opentelemetry.instrumentation.library.okhttp.v3_0;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Nonnull;

import io.opentelemetry.instrumentation.api.internal.HttpConstants;

/**
* Configuration for automatic instrumentation of okhttp requests.
*/
public class OkHttpInstrumentationConfig {
private static List<String> capturedRequestHeaders = new ArrayList<>();
private static List<String> capturedResponseHeaders = new ArrayList<>();
private static Set<String> knownMethods = new HashSet<>();
private static Map<String, String> peerServiceMapping = new HashMap<>();
private static boolean emitExperimentalHttpClientMetrics;

/**
* Configures the HTTP request headers that will be captured as span attributes as described in <a
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers">HTTP
* semantic conventions</a>.
*
* <p>The HTTP request header values will be captured under the {@code http.request.header.<name>}
* attribute key. The {@code <name>} part in the attribute key is the normalized header name:
* lowercase, with dashes replaced by underscores.
*
* @param requestHeaders A list of HTTP header names.
*/
public static void setCapturedRequestHeaders(@Nonnull List<String> requestHeaders) {
OkHttpInstrumentationConfig.capturedRequestHeaders = requestHeaders;
}

public static List<String> getCapturedRequestHeaders() {
return capturedRequestHeaders;
}

/**
* Configures the HTTP response headers that will be captured as span attributes as described in
* <a
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers">HTTP
* semantic conventions</a>.
*
* <p>The HTTP response header values will be captured under the {@code
* http.response.header.<name>} attribute key. The {@code <name>} part in the attribute key is the
* normalized header name: lowercase, with dashes replaced by underscores.
*
* @param responseHeaders A list of HTTP header names.
*/
public static void setCapturedResponseHeaders(@Nonnull List<String> responseHeaders) {
OkHttpInstrumentationConfig.capturedResponseHeaders = responseHeaders;
}

public static List<String> getCapturedResponseHeaders() {
return capturedResponseHeaders;
}

/**
* Configures the attrs extractor to recognize an alternative set of HTTP request methods.
*
* <p>By default, the 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.
*/
public static void setKnownMethods(@Nonnull Set<String> knownMethods) {
OkHttpInstrumentationConfig.knownMethods = knownMethods;
}

public static Set<String> getKnownMethods() {
return knownMethods;
}

/**
* Configures the extractor of the {@code peer.service} span attribute, described in <a
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/span-general.md#general-remote-service-attributes">the
* specification</a>.
*/
public static void setPeerServiceMapping(@Nonnull Map<String, String> peerServiceMapping) {
OkHttpInstrumentationConfig.peerServiceMapping = peerServiceMapping;
}

public static Map<String, String> getPeerServiceMapping() {
return peerServiceMapping;
}

/**
* When enabled keeps track of <a
* href="https://github.com/open-telemetry/semantic-conventions/blob/main/specification/metrics/semantic_conventions/http-metrics.md#http-client">non-stable
* HTTP client metrics</a>: <a
* href="https://github.com/open-telemetry/semantic-conventions/blob/main/specification/metrics/semantic_conventions/http-metrics.md#metric-httpclientrequestsize">the
* request size </a> and the <a
* href="https://github.com/open-telemetry/semantic-conventions/blob/main/specification/metrics/semantic_conventions/http-metrics.md#metric-httpclientresponsesize">
* the response size</a>.
*/
public static void setEmitExperimentalHttpClientMetrics(boolean emitExperimentalHttpClientMetrics) {
OkHttpInstrumentationConfig.emitExperimentalHttpClientMetrics = emitExperimentalHttpClientMetrics;
}

public static boolean emitExperimentalHttpClientMetrics() {
return emitExperimentalHttpClientMetrics;
}
}
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.library.okhttp.v3_0;
package io.opentelemetry.instrumentation.library.okhttp.v3_0.internal;

import static java.util.Collections.singletonList;

Expand All @@ -13,33 +13,35 @@
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientResend;
import io.opentelemetry.instrumentation.api.instrumenter.net.PeerServiceAttributesExtractor;
import io.opentelemetry.instrumentation.library.okhttp.v3_0.OkHttpInstrumentationConfig;
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.ConnectionErrorSpanInterceptor;
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.OkHttpAttributesGetter;
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.OkHttpInstrumenterFactory;
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.TracingInterceptor;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/** Holder of singleton interceptors for adding to instrumented clients. */
/**
* Holder of singleton interceptors for adding to instrumented clients.
*/
public final class OkHttp3Singletons {

private static final Instrumenter<Request, Response> INSTRUMENTER =
OkHttpInstrumenterFactory.create(
GlobalOpenTelemetry.get(),
builder ->
builder.setCapturedRequestHeaders(
CommonConfig.get().getClientRequestHeaders())
OkHttpInstrumentationConfig.getCapturedRequestHeaders())
.setCapturedResponseHeaders(
CommonConfig.get().getClientResponseHeaders())
OkHttpInstrumentationConfig.getCapturedResponseHeaders())
.setKnownMethods(
CommonConfig.get().getKnownHttpRequestMethods()),
OkHttpInstrumentationConfig.getKnownMethods()),
singletonList(
PeerServiceAttributesExtractor.create(
OkHttpAttributesGetter.INSTANCE,
CommonConfig.get().getPeerServiceMapping())),
CommonConfig.get().shouldEmitExperimentalHttpClientMetrics());
OkHttpInstrumentationConfig.getPeerServiceMapping())),
OkHttpInstrumentationConfig.emitExperimentalHttpClientMetrics());

public static final Interceptor CONTEXT_INTERCEPTOR =
chain -> {
Expand All @@ -54,5 +56,6 @@ public final class OkHttp3Singletons {
public static final Interceptor TRACING_INTERCEPTOR =
new TracingInterceptor(INSTRUMENTER, GlobalOpenTelemetry.getPropagators());

private OkHttp3Singletons() {}
private OkHttp3Singletons() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ java {

dependencies {
signature("com.toasttab.android:gummy-bears-api-${project.property("android.minSdk")}:0.5.1:coreLib@signature")
implementation("com.google.code.findbugs:jsr305:3.0.2")
}

animalsniffer {
Expand Down

0 comments on commit b778cb0

Please sign in to comment.