Skip to content

Commit

Permalink
fix rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Jul 11, 2024
1 parent 8ece154 commit a8db58f
Show file tree
Hide file tree
Showing 39 changed files with 461 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CommonConfig;
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpClientExperimentalMetrics;
Expand Down Expand Up @@ -40,6 +41,9 @@
*/
public final class DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> {

// copied from PeerIncubatingAttributes
private static final AttributeKey<String> PEER_SERVICE = AttributeKey.stringKey("peer.service");

private final String instrumentationName;
private final OpenTelemetry openTelemetry;

Expand Down Expand Up @@ -83,7 +87,7 @@ public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> addAttributeExtra
}

@CanIgnoreReturnValue
public DefaultHttpClientTelemetryBuilder<REQUEST, RESPONSE> setStatusExtractor(
public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> setStatusExtractor(
Function<
SpanStatusExtractor<? super REQUEST, ? super RESPONSE>,
? extends SpanStatusExtractor<? super REQUEST, ? super RESPONSE>>
Expand Down Expand Up @@ -174,6 +178,13 @@ public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> setPeerServiceRes
HttpClientPeerServiceAttributesExtractor.create(attributesGetter, peerServiceResolver));
}

/** Sets the {@code peer.service} attribute for http client spans. */
@CanIgnoreReturnValue
public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> setPeerService(
String peerService) {
return addAttributeExtractor(AttributesExtractor.constant(PEER_SERVICE, peerService));
}

@CanIgnoreReturnValue
public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> setBuilderCustomizer(
Consumer<InstrumenterBuilder<REQUEST, RESPONSE>> builderCustomizer) {
Expand Down Expand Up @@ -211,6 +222,10 @@ public OpenTelemetry getOpenTelemetry() {
return openTelemetry;
}

public String getInstrumentationName() {
return instrumentationName;
}

@CanIgnoreReturnValue
public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> configure(CommonConfig config) {
set(config::getKnownHttpRequestMethods, this::setKnownMethods);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public final class DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> {
private final HttpServerRouteBuilder<REQUEST> httpServerRouteBuilder;
private final HttpServerAttributesGetter<REQUEST, RESPONSE> attributesGetter;
private boolean emitExperimentalHttpServerMetrics = false;
private Consumer<InstrumenterBuilder<REQUEST, RESPONSE>> builderCustomizer = b -> {};

public DefaultHttpServerInstrumenterBuilder(
String instrumentationName,
Expand Down Expand Up @@ -168,6 +169,13 @@ public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> setSpanNameExtrac
return this;
}

@CanIgnoreReturnValue
public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> setBuilderCustomizer(
Consumer<InstrumenterBuilder<REQUEST, RESPONSE>> builderCustomizer) {
this.builderCustomizer = builderCustomizer;
return this;
}

public Instrumenter<REQUEST, RESPONSE> build() {
InstrumenterBuilder<REQUEST, RESPONSE> builder = builder();

Expand All @@ -177,7 +185,7 @@ public Instrumenter<REQUEST, RESPONSE> build() {
return builder.buildInstrumenter(SpanKindExtractor.alwaysServer());
}

private InstrumenterBuilder<REQUEST, RESPONSE> builder() {
public InstrumenterBuilder<REQUEST, RESPONSE> builder() {
SpanNameExtractor<? super REQUEST> spanNameExtractor =
spanNameExtractorTransformer.apply(httpSpanNameExtractorBuilder.build());

Expand All @@ -195,6 +203,7 @@ private InstrumenterBuilder<REQUEST, RESPONSE> builder() {
.addAttributesExtractor(HttpExperimentalAttributesExtractor.create(attributesGetter))
.addOperationMetrics(HttpServerExperimentalMetrics.get());
}
builderCustomizer.accept(builder);

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
public final class ApacheHttpClientTelemetryBuilder {

private static final String INSTRUMENTATION_NAME = "io.opentelemetry.apache-httpclient-4.3";
private final DefaultHttpClientInstrumenterBuilder<ApacheHttpClientRequest, HttpResponse>
clientBuilder;
private final DefaultHttpClientInstrumenterBuilder<ApacheHttpClientRequest, HttpResponse> builder;

ApacheHttpClientTelemetryBuilder(OpenTelemetry openTelemetry) {
clientBuilder =
builder =
new DefaultHttpClientInstrumenterBuilder<>(
INSTRUMENTATION_NAME, openTelemetry, ApacheHttpClientHttpAttributesGetter.INSTANCE);
}
Expand All @@ -37,7 +36,7 @@ public final class ApacheHttpClientTelemetryBuilder {
public ApacheHttpClientTelemetryBuilder addAttributeExtractor(
AttributesExtractor<? super ApacheHttpClientRequest, ? super HttpResponse>
attributesExtractor) {
clientBuilder.addAttributeExtractor(attributesExtractor);
builder.addAttributeExtractor(attributesExtractor);
return this;
}

Expand All @@ -48,7 +47,7 @@ public ApacheHttpClientTelemetryBuilder addAttributeExtractor(
*/
@CanIgnoreReturnValue
public ApacheHttpClientTelemetryBuilder setCapturedRequestHeaders(List<String> requestHeaders) {
clientBuilder.setCapturedRequestHeaders(requestHeaders);
builder.setCapturedRequestHeaders(requestHeaders);
return this;
}

Expand All @@ -59,7 +58,7 @@ public ApacheHttpClientTelemetryBuilder setCapturedRequestHeaders(List<String> r
*/
@CanIgnoreReturnValue
public ApacheHttpClientTelemetryBuilder setCapturedResponseHeaders(List<String> responseHeaders) {
clientBuilder.setCapturedResponseHeaders(responseHeaders);
builder.setCapturedResponseHeaders(responseHeaders);
return this;
}

Expand All @@ -78,7 +77,7 @@ public ApacheHttpClientTelemetryBuilder setCapturedResponseHeaders(List<String>
*/
@CanIgnoreReturnValue
public ApacheHttpClientTelemetryBuilder setKnownMethods(Set<String> knownMethods) {
clientBuilder.setKnownMethods(knownMethods);
builder.setKnownMethods(knownMethods);
return this;
}

Expand All @@ -91,7 +90,7 @@ public ApacheHttpClientTelemetryBuilder setKnownMethods(Set<String> knownMethods
@CanIgnoreReturnValue
public ApacheHttpClientTelemetryBuilder setEmitExperimentalHttpClientMetrics(
boolean emitExperimentalHttpClientMetrics) {
clientBuilder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientMetrics);
builder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientMetrics);
return this;
}

Expand All @@ -102,7 +101,7 @@ public ApacheHttpClientTelemetryBuilder setSpanNameExtractor(
SpanNameExtractor<ApacheHttpClientRequest>,
? extends SpanNameExtractor<? super ApacheHttpClientRequest>>
spanNameExtractorTransformer) {
clientBuilder.setSpanNameExtractor(spanNameExtractorTransformer);
builder.setSpanNameExtractor(spanNameExtractorTransformer);
return this;
}

Expand All @@ -112,6 +111,6 @@ public ApacheHttpClientTelemetryBuilder setSpanNameExtractor(
*/
public ApacheHttpClientTelemetry build() {
return new ApacheHttpClientTelemetry(
clientBuilder.build(), clientBuilder.getOpenTelemetry().getPropagators());
builder.build(), builder.getOpenTelemetry().getPropagators());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public final class ApacheHttpClient5TelemetryBuilder {

private static final String INSTRUMENTATION_NAME = "io.opentelemetry.apache-httpclient-5.2";
private final DefaultHttpClientInstrumenterBuilder<ApacheHttpClient5Request, HttpResponse>
clientBuilder;
builder;

ApacheHttpClient5TelemetryBuilder(OpenTelemetry openTelemetry) {
clientBuilder =
builder =
new DefaultHttpClientInstrumenterBuilder<>(
INSTRUMENTATION_NAME, openTelemetry, ApacheHttpClient5HttpAttributesGetter.INSTANCE);
}
Expand All @@ -37,7 +37,7 @@ public final class ApacheHttpClient5TelemetryBuilder {
public ApacheHttpClient5TelemetryBuilder addAttributeExtractor(
AttributesExtractor<? super ApacheHttpClient5Request, ? super HttpResponse>
attributesExtractor) {
clientBuilder.addAttributeExtractor(attributesExtractor);
builder.addAttributeExtractor(attributesExtractor);
return this;
}

Expand All @@ -48,7 +48,7 @@ public ApacheHttpClient5TelemetryBuilder addAttributeExtractor(
*/
@CanIgnoreReturnValue
public ApacheHttpClient5TelemetryBuilder setCapturedRequestHeaders(List<String> requestHeaders) {
clientBuilder.setCapturedRequestHeaders(requestHeaders);
builder.setCapturedRequestHeaders(requestHeaders);
return this;
}

Expand All @@ -60,7 +60,7 @@ public ApacheHttpClient5TelemetryBuilder setCapturedRequestHeaders(List<String>
@CanIgnoreReturnValue
public ApacheHttpClient5TelemetryBuilder setCapturedResponseHeaders(
List<String> responseHeaders) {
clientBuilder.setCapturedResponseHeaders(responseHeaders);
builder.setCapturedResponseHeaders(responseHeaders);
return this;
}

Expand All @@ -79,7 +79,7 @@ public ApacheHttpClient5TelemetryBuilder setCapturedResponseHeaders(
*/
@CanIgnoreReturnValue
public ApacheHttpClient5TelemetryBuilder setKnownMethods(Set<String> knownMethods) {
clientBuilder.setKnownMethods(knownMethods);
builder.setKnownMethods(knownMethods);
return this;
}

Expand All @@ -92,7 +92,7 @@ public ApacheHttpClient5TelemetryBuilder setKnownMethods(Set<String> knownMethod
@CanIgnoreReturnValue
public ApacheHttpClient5TelemetryBuilder setEmitExperimentalHttpClientMetrics(
boolean emitExperimentalHttpClientMetrics) {
clientBuilder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientMetrics);
builder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientMetrics);
return this;
}

Expand All @@ -103,7 +103,7 @@ public ApacheHttpClient5TelemetryBuilder setSpanNameExtractor(
SpanNameExtractor<ApacheHttpClient5Request>,
? extends SpanNameExtractor<? super ApacheHttpClient5Request>>
spanNameExtractorTransformer) {
clientBuilder.setSpanNameExtractor(spanNameExtractorTransformer);
builder.setSpanNameExtractor(spanNameExtractorTransformer);
return this;
}

Expand All @@ -113,6 +113,6 @@ public ApacheHttpClient5TelemetryBuilder setSpanNameExtractor(
*/
public ApacheHttpClient5Telemetry build() {
return new ApacheHttpClient5Telemetry(
clientBuilder.build(), clientBuilder.getOpenTelemetry().getPropagators());
builder.build(), builder.getOpenTelemetry().getPropagators());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import com.linecorp.armeria.client.HttpClient;
import com.linecorp.armeria.server.HttpService;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpServerInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CommonConfig;
import io.opentelemetry.instrumentation.armeria.v1_3.ArmeriaTelemetry;
import io.opentelemetry.instrumentation.armeria.v1_3.ArmeriaTelemetryBuilder;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.instrumentation.armeria.v1_3.internal.ArmeriaInstrumenterBuilderUtil;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import java.util.function.Function;

// Holds singleton references to decorators to match against during suppression.
Expand All @@ -24,8 +24,9 @@ public final class ArmeriaSingletons {

static {
ArmeriaTelemetryBuilder builder = ArmeriaTelemetry.builder(GlobalOpenTelemetry.get());
DefaultHttpClientInstrumenterBuilder.unwrapAndConfigure(CommonConfig.get(), builder);
DefaultHttpServerInstrumenterBuilder.unwrapAndConfigure(CommonConfig.get(), builder);
CommonConfig config = AgentCommonConfig.get();
ArmeriaInstrumenterBuilderUtil.getClientBuilderExtractor().apply(builder).configure(config);
ArmeriaInstrumenterBuilderUtil.getServerBuilderExtractor().apply(builder).configure(config);
ArmeriaTelemetry telemetry = builder.build();

CLIENT_DECORATOR = telemetry.newClientDecorator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,37 @@
import com.linecorp.armeria.common.logging.RequestLog;
import com.linecorp.armeria.server.ServiceRequestContext;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpServerInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractorBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractorBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesGetter;
import io.opentelemetry.instrumentation.armeria.v1_3.internal.ArmeriaHttpClientAttributesGetter;
import io.opentelemetry.instrumentation.armeria.v1_3.internal.ArmeriaInstrumenterBuilderFactory;
import io.opentelemetry.instrumentation.armeria.v1_3.internal.ArmeriaInstrumenterBuilderUtil;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;

public final class ArmeriaTelemetryBuilder {

private static final String INSTRUMENTATION_NAME = "io.opentelemetry.armeria-1.3";
// copied from PeerIncubatingAttributes
private static final AttributeKey<String> PEER_SERVICE = AttributeKey.stringKey("peer.service");

@Nullable private String peerService;
private final DefaultHttpClientInstrumenterBuilder<ClientRequestContext, RequestLog>
clientBuilder;
private final DefaultHttpServerInstrumenterBuilder<ServiceRequestContext, RequestLog>
serverBuilder;

static {
ArmeriaInstrumenterBuilderUtil.setClientBuilderExtractor(
ArmeriaTelemetryBuilder::getClientBuilder);
ArmeriaInstrumenterBuilderUtil.setServerBuilderExtractor(
ArmeriaTelemetryBuilder::getServerBuilder);
}

@SuppressWarnings({"rawtypes", "unchecked"})
ArmeriaTelemetryBuilder(OpenTelemetry openTelemetry) {
clientBuilder =
new DefaultHttpClientInstrumenterBuilder<ClientRequestContext, RequestLog>(
INSTRUMENTATION_NAME,
openTelemetry,
(HttpClientAttributesGetter) ArmeriaHttpClientAttributesGetter.INSTANCE)
.setHeaderSetter(ClientRequestContextSetter.INSTANCE)
.setBuilderCustomizer(
builder -> {
if (peerService != null) {
builder.addAttributesExtractor(
AttributesExtractor.constant(PEER_SERVICE, peerService));
}
});
serverBuilder =
new DefaultHttpServerInstrumenterBuilder<>(
INSTRUMENTATION_NAME,
openTelemetry,
(HttpServerAttributesGetter) ArmeriaHttpServerAttributesGetter.INSTANCE)
.setHeaderGetter(RequestContextGetter.INSTANCE);
clientBuilder = ArmeriaInstrumenterBuilderFactory.getClientBuilder(openTelemetry);
serverBuilder = ArmeriaInstrumenterBuilderFactory.getServerBuilder(openTelemetry);
}

@SuppressWarnings({"unchecked", "rawtypes"})
Expand Down Expand Up @@ -101,7 +83,7 @@ public ArmeriaTelemetryBuilder addClientAttributeExtractor(
/** Sets the {@code peer.service} attribute for http client spans. */
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder setPeerService(String peerService) {
this.peerService = peerService;
clientBuilder.setPeerService(peerService);
return this;
}

Expand Down Expand Up @@ -225,4 +207,14 @@ public ArmeriaTelemetryBuilder setServerSpanNameExtractor(
public ArmeriaTelemetry build() {
return new ArmeriaTelemetry(clientBuilder.build(), serverBuilder.build());
}

private DefaultHttpClientInstrumenterBuilder<ClientRequestContext, RequestLog>
getClientBuilder() {
return clientBuilder;
}

private DefaultHttpServerInstrumenterBuilder<ServiceRequestContext, RequestLog>
getServerBuilder() {
return serverBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
import java.util.List;
import javax.annotation.Nullable;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public enum ArmeriaHttpClientAttributesGetter
enum ArmeriaHttpClientAttributesGetter
implements HttpClientAttributesGetter<RequestContext, RequestLog> {
INSTANCE;

Expand Down
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.armeria.v1_3;
package io.opentelemetry.instrumentation.armeria.v1_3.internal;

import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpStatus;
Expand All @@ -12,7 +12,6 @@
import com.linecorp.armeria.common.logging.RequestLog;
import com.linecorp.armeria.server.ServiceRequestContext;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesGetter;
import io.opentelemetry.instrumentation.armeria.v1_3.internal.RequestContextAccess;
import java.net.InetSocketAddress;
import java.util.List;
import javax.annotation.Nullable;
Expand Down
Loading

0 comments on commit a8db58f

Please sign in to comment.