Skip to content

Commit

Permalink
http server builder
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Aug 22, 2024
1 parent bcd3de5 commit 489ef33
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ public final class ArmeriaTelemetryBuilder {
ArmeriaTelemetryBuilder::getServerBuilder);
}

@SuppressWarnings({"rawtypes", "unchecked"})
ArmeriaTelemetryBuilder(OpenTelemetry openTelemetry) {
clientBuilder = ArmeriaInstrumenterBuilderFactory.getClientBuilder(openTelemetry);
serverBuilder = ArmeriaInstrumenterBuilderFactory.getServerBuilder(openTelemetry);
}

/**
* Sets the status extractor for both client and server spans.
*
* @deprecated Use {@link #setClientStatusExtractor(Function)} or {@link
* #setServerStatusExtractor(Function)} instead.
*/
@Deprecated
@SuppressWarnings({"unchecked", "rawtypes"})
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder setStatusExtractor(
Expand All @@ -56,10 +62,36 @@ public ArmeriaTelemetryBuilder setStatusExtractor(
return this;
}

/** Sets the status extractor for client spans. */
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder setClientStatusExtractor(
Function<
SpanStatusExtractor<? super ClientRequestContext, ? super RequestLog>,
? extends SpanStatusExtractor<? super ClientRequestContext, ? super RequestLog>>
statusExtractor) {
clientBuilder.setStatusExtractor(statusExtractor);
return this;
}

/** Sets the status extractor for server spans. */
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder setServerStatusExtractor(
Function<
SpanStatusExtractor<? super ServiceRequestContext, ? super RequestLog>,
? extends SpanStatusExtractor<? super ServiceRequestContext, ? super RequestLog>>
statusExtractor) {
serverBuilder.setStatusExtractor(statusExtractor);
return this;
}

/**
* Adds an additional {@link AttributesExtractor} to invoke to set attributes to instrumented
* items. The {@link AttributesExtractor} will be executed after all default extractors.
*
* @deprecated Use {@link #addClientAttributeExtractor(AttributesExtractor)} or {@link
* #addServerAttributeExtractor(AttributesExtractor)} instead.
*/
@Deprecated
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder addAttributeExtractor(
AttributesExtractor<? super RequestContext, ? super RequestLog> attributesExtractor) {
Expand All @@ -80,6 +112,18 @@ public ArmeriaTelemetryBuilder addClientAttributeExtractor(
return this;
}

/**
* Adds an extra server-only {@link AttributesExtractor} to invoke to set attributes to
* instrumented items. The {@link AttributesExtractor} will be executed after all default
* extractors.
*/
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder addServerAttributeExtractor(
AttributesExtractor<? super ServiceRequestContext, ? super RequestLog> attributesExtractor) {
serverBuilder.addAttributesExtractor(attributesExtractor);
return this;
}

/** Sets the {@code peer.service} attribute for http client spans. */
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder setPeerService(String peerService) {
Expand Down Expand Up @@ -162,7 +206,6 @@ public ArmeriaTelemetryBuilder setKnownMethods(Set<String> knownMethods) {
public ArmeriaTelemetryBuilder setEmitExperimentalHttpClientMetrics(
boolean emitExperimentalHttpClientMetrics) {
clientBuilder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientMetrics);
serverBuilder.setEmitExperimentalHttpServerMetrics(emitExperimentalHttpClientMetrics);
return this;
}

Expand All @@ -175,32 +218,29 @@ public ArmeriaTelemetryBuilder setEmitExperimentalHttpClientMetrics(
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder setEmitExperimentalHttpServerMetrics(
boolean emitExperimentalHttpServerMetrics) {
clientBuilder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpServerMetrics);
serverBuilder.setEmitExperimentalHttpServerMetrics(emitExperimentalHttpServerMetrics);
return this;
}

/** Sets custom client {@link SpanNameExtractor} via transform function. */
@SuppressWarnings({"rawtypes", "unchecked"})
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder setClientSpanNameExtractor(
Function<
SpanNameExtractor<RequestContext>,
? extends SpanNameExtractor<? super RequestContext>>
SpanNameExtractor<? super ClientRequestContext>,
? extends SpanNameExtractor<? super ClientRequestContext>>
clientSpanNameExtractor) {
clientBuilder.setSpanNameExtractor((Function) clientSpanNameExtractor);
clientBuilder.setSpanNameExtractor(clientSpanNameExtractor);
return this;
}

/** Sets custom server {@link SpanNameExtractor} via transform function. */
@SuppressWarnings({"rawtypes", "unchecked"})
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder setServerSpanNameExtractor(
Function<
SpanNameExtractor<RequestContext>,
? extends SpanNameExtractor<? super RequestContext>>
SpanNameExtractor<? super ServiceRequestContext>,
? extends SpanNameExtractor<? super ServiceRequestContext>>
serverSpanNameExtractor) {
serverBuilder.setSpanNameExtractor((Function) serverSpanNameExtractor);
serverBuilder.setSpanNameExtractor(serverSpanNameExtractor);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

package io.opentelemetry.instrumentation.armeria.v1_3.internal;

import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.RequestContext;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.logging.RequestLog;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter;
Expand All @@ -17,7 +17,7 @@
import javax.annotation.Nullable;

enum ArmeriaHttpClientAttributesGetter
implements HttpClientAttributesGetter<RequestContext, RequestLog> {
implements HttpClientAttributesGetter<ClientRequestContext, RequestLog> {
INSTANCE;

private static final ClassValue<Method> authorityMethodCache =
Expand All @@ -34,12 +34,12 @@ protected Method computeValue(Class<?> type) {
};

@Override
public String getHttpRequestMethod(RequestContext ctx) {
public String getHttpRequestMethod(ClientRequestContext ctx) {
return ctx.method().name();
}

@Override
public String getUrlFull(RequestContext ctx) {
public String getUrlFull(ClientRequestContext ctx) {
HttpRequest request = request(ctx);
StringBuilder uri = new StringBuilder();
String scheme = request.scheme();
Expand All @@ -61,14 +61,14 @@ public String getUrlFull(RequestContext ctx) {
}

@Override
public List<String> getHttpRequestHeader(RequestContext ctx, String name) {
public List<String> getHttpRequestHeader(ClientRequestContext ctx, String name) {
return request(ctx).headers().getAll(name);
}

@Override
@Nullable
public Integer getHttpResponseStatusCode(
RequestContext ctx, RequestLog requestLog, @Nullable Throwable error) {
ClientRequestContext ctx, RequestLog requestLog, @Nullable Throwable error) {
HttpStatus status = requestLog.responseHeaders().status();
if (!status.equals(HttpStatus.UNKNOWN)) {
return status.code();
Expand All @@ -78,25 +78,26 @@ public Integer getHttpResponseStatusCode(

@Override
public List<String> getHttpResponseHeader(
RequestContext ctx, RequestLog requestLog, String name) {
ClientRequestContext ctx, RequestLog requestLog, String name) {
return requestLog.responseHeaders().getAll(name);
}

@Override
public String getNetworkProtocolName(RequestContext ctx, @Nullable RequestLog requestLog) {
public String getNetworkProtocolName(ClientRequestContext ctx, @Nullable RequestLog requestLog) {
return "http";
}

@Override
public String getNetworkProtocolVersion(RequestContext ctx, @Nullable RequestLog requestLog) {
public String getNetworkProtocolVersion(
ClientRequestContext ctx, @Nullable RequestLog requestLog) {
SessionProtocol protocol =
requestLog != null ? requestLog.sessionProtocol() : ctx.sessionProtocol();
return protocol.isMultiplex() ? "2" : "1.1";
}

@Nullable
@Override
public String getServerAddress(RequestContext ctx) {
public String getServerAddress(ClientRequestContext ctx) {
String authority = authority(ctx);
if (authority == null) {
return null;
Expand All @@ -107,7 +108,7 @@ public String getServerAddress(RequestContext ctx) {

@Nullable
@Override
public Integer getServerPort(RequestContext ctx) {
public Integer getServerPort(ClientRequestContext ctx) {
String authority = authority(ctx);
if (authority == null) {
return null;
Expand All @@ -126,12 +127,12 @@ public Integer getServerPort(RequestContext ctx) {
@Override
@Nullable
public InetSocketAddress getNetworkPeerInetSocketAddress(
RequestContext ctx, @Nullable RequestLog requestLog) {
ClientRequestContext ctx, @Nullable RequestLog requestLog) {
return RequestContextAccess.remoteAddress(ctx);
}

@Nullable
private static String authority(RequestContext ctx) {
private static String authority(ClientRequestContext ctx) {
// newer armeria versions expose authority through DefaultClientRequestContext#authority
// we are using this method as it provides default values based on endpoint
// in older versions armeria wraps the request, and we can get the same default values through
Expand All @@ -149,7 +150,7 @@ private static String authority(RequestContext ctx) {
return request.authority();
}

private static HttpRequest request(RequestContext ctx) {
private static HttpRequest request(ClientRequestContext ctx) {
HttpRequest request = ctx.request();
if (request == null) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpServerInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesGetter;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
Expand All @@ -23,23 +21,17 @@ private ArmeriaInstrumenterBuilderFactory() {}

private static final String INSTRUMENTATION_NAME = "io.opentelemetry.armeria-1.3";

@SuppressWarnings({"unchecked", "rawtypes"})
public static DefaultHttpServerInstrumenterBuilder<ServiceRequestContext, RequestLog>
getServerBuilder(OpenTelemetry openTelemetry) {
return new DefaultHttpServerInstrumenterBuilder<ServiceRequestContext, RequestLog>(
INSTRUMENTATION_NAME,
openTelemetry,
(HttpServerAttributesGetter) ArmeriaHttpServerAttributesGetter.INSTANCE)
return new DefaultHttpServerInstrumenterBuilder<>(
INSTRUMENTATION_NAME, openTelemetry, ArmeriaHttpServerAttributesGetter.INSTANCE)
.setHeaderGetter(RequestContextGetter.INSTANCE);
}

@SuppressWarnings({"unchecked", "rawtypes"})
public static DefaultHttpClientInstrumenterBuilder<ClientRequestContext, RequestLog>
getClientBuilder(OpenTelemetry openTelemetry) {
return new DefaultHttpClientInstrumenterBuilder<ClientRequestContext, RequestLog>(
INSTRUMENTATION_NAME,
openTelemetry,
(HttpClientAttributesGetter) ArmeriaHttpClientAttributesGetter.INSTANCE)
return new DefaultHttpClientInstrumenterBuilder<>(
INSTRUMENTATION_NAME, openTelemetry, ArmeriaHttpClientAttributesGetter.INSTANCE)
.setHeaderSetter(ClientRequestContextSetter.INSTANCE);
}
}

0 comments on commit 489ef33

Please sign in to comment.