Skip to content

Commit

Permalink
Set custom gRPC client/server span name extractor (#5244)
Browse files Browse the repository at this point in the history
* Set custom gRPC client/server span name extractor

* Fix imports

* Fix compilation

* Fix style rule

* Update to use Functions instead

* Fix formatting
  • Loading branch information
eugeniyk authored Feb 24, 2022
1 parent 6d58704 commit bf16564
Showing 1 changed file with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.PeerServiceAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesExtractor;
import io.opentelemetry.instrumentation.grpc.v1_6.internal.GrpcNetClientAttributesGetter;
import io.opentelemetry.instrumentation.grpc.v1_6.internal.GrpcNetServerAttributesGetter;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.annotation.Nullable;

Expand All @@ -30,6 +32,14 @@ public final class GrpcTracingBuilder {
private final OpenTelemetry openTelemetry;
@Nullable private String peerService;

@Nullable
private Function<SpanNameExtractor<GrpcRequest>, ? extends SpanNameExtractor<? super GrpcRequest>>
clientSpanNameExtractorTransformer;

@Nullable
private Function<SpanNameExtractor<GrpcRequest>, ? extends SpanNameExtractor<? super GrpcRequest>>
serverSpanNameExtractorTransformer;

private final List<AttributesExtractor<? super GrpcRequest, ? super Status>>
additionalExtractors = new ArrayList<>();

Expand All @@ -49,9 +59,26 @@ public GrpcTracingBuilder addAttributeExtractor(
return this;
}

/** Sets custom client {@link SpanNameExtractor} via transform function. */
public GrpcTracingBuilder setClientSpanNameExtractor(
Function<SpanNameExtractor<GrpcRequest>, ? extends SpanNameExtractor<? super GrpcRequest>>
clientSpanNameExtractor) {
this.clientSpanNameExtractorTransformer = clientSpanNameExtractor;
return this;
}

/** Sets custom server {@link SpanNameExtractor} via transform function. */
public GrpcTracingBuilder setServerSpanNameExtractor(
Function<SpanNameExtractor<GrpcRequest>, ? extends SpanNameExtractor<? super GrpcRequest>>
serverSpanNameExtractor) {
this.serverSpanNameExtractorTransformer = serverSpanNameExtractor;
return this;
}

/** Sets the {@code peer.service} attribute for http client spans. */
public void setPeerService(String peerService) {
public GrpcTracingBuilder setPeerService(String peerService) {
this.peerService = peerService;
return this;
}

/**
Expand All @@ -67,10 +94,22 @@ public GrpcTracingBuilder setCaptureExperimentalSpanAttributes(

/** Returns a new {@link GrpcTracing} with the settings of this {@link GrpcTracingBuilder}. */
public GrpcTracing build() {
SpanNameExtractor<GrpcRequest> originalSpanNameExtractor = new GrpcSpanNameExtractor();

SpanNameExtractor<? super GrpcRequest> clientSpanNameExtractor = originalSpanNameExtractor;
if (clientSpanNameExtractorTransformer != null) {
clientSpanNameExtractor = clientSpanNameExtractorTransformer.apply(originalSpanNameExtractor);
}

SpanNameExtractor<? super GrpcRequest> serverSpanNameExtractor = originalSpanNameExtractor;
if (serverSpanNameExtractorTransformer != null) {
serverSpanNameExtractor = serverSpanNameExtractorTransformer.apply(originalSpanNameExtractor);
}

InstrumenterBuilder<GrpcRequest, Status> clientInstrumenterBuilder =
Instrumenter.builder(openTelemetry, INSTRUMENTATION_NAME, new GrpcSpanNameExtractor());
Instrumenter.builder(openTelemetry, INSTRUMENTATION_NAME, clientSpanNameExtractor);
InstrumenterBuilder<GrpcRequest, Status> serverInstrumenterBuilder =
Instrumenter.builder(openTelemetry, INSTRUMENTATION_NAME, new GrpcSpanNameExtractor());
Instrumenter.builder(openTelemetry, INSTRUMENTATION_NAME, serverSpanNameExtractor);

Stream.of(clientInstrumenterBuilder, serverInstrumenterBuilder)
.forEach(
Expand Down

0 comments on commit bf16564

Please sign in to comment.