Skip to content

Commit

Permalink
Set custom gRPC client/server span name extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeniyk committed Jan 26, 2022
1 parent 953fd10 commit 461c870
Showing 1 changed file with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public final class GrpcTracingBuilder {

private final OpenTelemetry openTelemetry;
@Nullable private String peerService;
@Nullable private SpanNameExtractor<GrpcRequest> clientSpanNameExtractor;
@Nullable private SpanNameExtractor<GrpcRequest> serverSpanNameExtractor;

private final List<AttributesExtractor<? super GrpcRequest, ? super Status>>
additionalExtractors = new ArrayList<>();
Expand All @@ -49,9 +51,28 @@ public GrpcTracingBuilder addAttributeExtractor(
return this;
}

/**
* Sets custom client {@link SpanNameExtractor}
*/
public GrpcTracingBuilder setClientSpanNameExtractor(
SpanNameExtractor<? super GrpcRequest> clientSpanNameExtractor) {
this.clientSpanNameExtractor = clientSpanNameExtractor;
return this;
}

/**
* Sets custom server {@link SpanNameExtractor}
*/
public GrpcTracingBuilder setServerSpanNameExtractor(
SpanNameExtractor<? super GrpcRequest> serverSpanNameExtractor) {
this.serverSpanNameExtractor = 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 +88,20 @@ public GrpcTracingBuilder setCaptureExperimentalSpanAttributes(

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

SpanNameExtractor<GrpcRequest> serverSpanNameExtractor = this.serverSpanNameExtractor;
if (serverSpanNameExtractor == null) {
serverSpanNameExtractor = new GrpcSpanNameExtractor();
}

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 461c870

Please sign in to comment.