Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Instrumenter in JMS instrumentation #2803

Merged
merged 7 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,19 @@

package io.opentelemetry.instrumentation.api.instrumenter;

import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapSetter;
import java.util.List;

final class ClientInstrumenter<REQUEST, RESPONSE> extends Instrumenter<REQUEST, RESPONSE> {

private final ContextPropagators propagators;
private final TextMapSetter<REQUEST> setter;

ClientInstrumenter(
String instrumentationName,
Tracer tracer,
SpanNameExtractor<? super REQUEST> spanNameExtractor,
SpanKindExtractor<? super REQUEST> spanKindExtractor,
SpanStatusExtractor<? super REQUEST, ? super RESPONSE> spanStatusExtractor,
List<? extends AttributesExtractor<? super REQUEST, ? super RESPONSE>> attributesExtractors,
ErrorCauseExtractor errorCauseExtractor,
ContextPropagators propagators,
TextMapSetter<REQUEST> setter) {
super(
instrumentationName,
tracer,
spanNameExtractor,
spanKindExtractor,
spanStatusExtractor,
attributesExtractors,
errorCauseExtractor);
this.propagators = propagators;
public ClientInstrumenter(
InstrumenterBuilder<REQUEST, RESPONSE> builder, TextMapSetter<REQUEST> setter) {
super(builder);
this.propagators = builder.openTelemetry.getPropagators();
this.setter = setter;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter;

import java.time.Instant;

/**
* Extractor of the end time of response processing. An {@link EndTimeExtractor} should always use
* the same timestamp source as the corresponding {@link StartTimeExtractor} - extracted timestamps
* must be comparable.
*/
@FunctionalInterface
public interface EndTimeExtractor<RESPONSE> {

/** Returns the timestamp marking the end of the response processing. */
Instant extract(RESPONSE response);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.InstrumentationVersion;
import io.opentelemetry.instrumentation.api.internal.SupportabilityMetrics;
import io.opentelemetry.instrumentation.api.tracer.ClientSpan;
import io.opentelemetry.instrumentation.api.tracer.ServerSpan;
import java.util.ArrayList;
import java.util.List;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -56,22 +58,20 @@ public static <REQUEST, RESPONSE> InstrumenterBuilder<REQUEST, RESPONSE> newBuil
private final SpanStatusExtractor<? super REQUEST, ? super RESPONSE> spanStatusExtractor;
private final List<? extends AttributesExtractor<? super REQUEST, ? super RESPONSE>> extractors;
private final ErrorCauseExtractor errorCauseExtractor;

Instrumenter(
String instrumentationName,
Tracer tracer,
SpanNameExtractor<? super REQUEST> spanNameExtractor,
SpanKindExtractor<? super REQUEST> spanKindExtractor,
SpanStatusExtractor<? super REQUEST, ? super RESPONSE> spanStatusExtractor,
List<? extends AttributesExtractor<? super REQUEST, ? super RESPONSE>> extractors,
ErrorCauseExtractor errorCauseExtractor) {
this.instrumentationName = instrumentationName;
this.tracer = tracer;
this.spanNameExtractor = spanNameExtractor;
this.spanKindExtractor = spanKindExtractor;
this.spanStatusExtractor = spanStatusExtractor;
this.extractors = extractors;
this.errorCauseExtractor = errorCauseExtractor;
private final StartTimeExtractor<REQUEST> startTimeExtractor;
private final EndTimeExtractor<RESPONSE> endTimeExtractor;

Instrumenter(InstrumenterBuilder<REQUEST, RESPONSE> builder) {
this.instrumentationName = builder.instrumentationName;
this.tracer =
builder.openTelemetry.getTracer(instrumentationName, InstrumentationVersion.VERSION);
this.spanNameExtractor = builder.spanNameExtractor;
this.spanKindExtractor = builder.spanKindExtractor;
this.spanStatusExtractor = builder.spanStatusExtractor;
this.extractors = new ArrayList<>(builder.attributesExtractors);
this.errorCauseExtractor = builder.errorCauseExtractor;
this.startTimeExtractor = builder.startTimeExtractor;
this.endTimeExtractor = builder.endTimeExtractor;
}

/**
Expand Down Expand Up @@ -114,6 +114,10 @@ public Context start(Context parentContext, REQUEST request) {
.setSpanKind(spanKind)
.setParent(parentContext);

if (startTimeExtractor != null) {
spanBuilder.setStartTimestamp(startTimeExtractor.extract(request));
}

AttributesBuilder attributes = Attributes.builder();
for (AttributesExtractor<? super REQUEST, ? super RESPONSE> extractor : extractors) {
extractor.onStart(attributes, request);
Expand Down Expand Up @@ -154,6 +158,10 @@ public void end(Context context, REQUEST request, RESPONSE response, @Nullable T

span.setStatus(spanStatusExtractor.extract(request, response, error));

span.end();
if (endTimeExtractor != null) {
span.end(endTimeExtractor.extract(response));
} else {
span.end();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

package io.opentelemetry.instrumentation.api.instrumenter;

import static java.util.Objects.requireNonNull;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.instrumentation.api.InstrumentationVersion;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -22,16 +21,19 @@
* {@link Instrumenter}.
*/
public final class InstrumenterBuilder<REQUEST, RESPONSE> {
private final OpenTelemetry openTelemetry;
private final String instrumentationName;
private final SpanNameExtractor<? super REQUEST> spanNameExtractor;
final OpenTelemetry openTelemetry;
final String instrumentationName;
final SpanNameExtractor<? super REQUEST> spanNameExtractor;

private final List<AttributesExtractor<? super REQUEST, ? super RESPONSE>> attributesExtractors =
final List<AttributesExtractor<? super REQUEST, ? super RESPONSE>> attributesExtractors =
new ArrayList<>();

private SpanStatusExtractor<? super REQUEST, ? super RESPONSE> spanStatusExtractor =
SpanKindExtractor<? super REQUEST> spanKindExtractor;
SpanStatusExtractor<? super REQUEST, ? super RESPONSE> spanStatusExtractor =
SpanStatusExtractor.getDefault();
private ErrorCauseExtractor errorCauseExtractor = ErrorCauseExtractor.jdk();
ErrorCauseExtractor errorCauseExtractor = ErrorCauseExtractor.jdk();
StartTimeExtractor<REQUEST> startTimeExtractor = null;
EndTimeExtractor<RESPONSE> endTimeExtractor = null;

InstrumenterBuilder(
OpenTelemetry openTelemetry,
Expand Down Expand Up @@ -83,23 +85,53 @@ public InstrumenterBuilder<REQUEST, RESPONSE> setErrorCauseExtractor(
}

/**
* Returns a new client {@link Instrumenter} which will create client spans and inject context
* into requests.
* Sets the {@link StartTimeExtractor} and the {@link EndTimeExtractor} to extract the timestamp
* marking the start and end of processing. If unset, the constructed instrumenter will defer
* determining start and end timestamps to the OpenTelemetry SDK.
*/
public InstrumenterBuilder<REQUEST, RESPONSE> setTimeExtractors(
StartTimeExtractor<REQUEST> startTimeExtractor, EndTimeExtractor<RESPONSE> endTimeExtractor) {
Comment on lines +92 to +93
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two should probably be always set together - start & end timestamps need to be comparable, so it does not make any sense to set just one and compare with the SDK timestamp.

this.startTimeExtractor = requireNonNull(startTimeExtractor);
this.endTimeExtractor = requireNonNull(endTimeExtractor);
return this;
}

/**
* Returns a new {@link Instrumenter} which will create client spans and inject context into
* requests.
*/
public Instrumenter<REQUEST, RESPONSE> newClientInstrumenter(TextMapSetter<REQUEST> setter) {
return newInstrumenter(
InstrumenterConstructor.propagatingToDownstream(openTelemetry.getPropagators(), setter),
SpanKindExtractor.alwaysClient());
InstrumenterConstructor.propagatingToDownstream(setter), SpanKindExtractor.alwaysClient());
}

/**
* Returns a new server {@link Instrumenter} which will create server spans and extract context
* from requests.
* Returns a new {@link Instrumenter} which will create server spans and extract context from
* requests.
*/
public Instrumenter<REQUEST, RESPONSE> newServerInstrumenter(TextMapGetter<REQUEST> getter) {
return newInstrumenter(
InstrumenterConstructor.propagatingFromUpstream(openTelemetry.getPropagators(), getter),
SpanKindExtractor.alwaysServer());
InstrumenterConstructor.propagatingFromUpstream(getter), SpanKindExtractor.alwaysServer());
}

/**
* Returns a new {@link Instrumenter} which will create producer spans and inject context into
* requests.
*/
public Instrumenter<REQUEST, RESPONSE> newProducerInstrumenter(TextMapSetter<REQUEST> setter) {
return newInstrumenter(
InstrumenterConstructor.propagatingToDownstream(setter),
SpanKindExtractor.alwaysProducer());
}

/**
* Returns a new {@link Instrumenter} which will create consumer spans and extract context from
* requests.
*/
public Instrumenter<REQUEST, RESPONSE> newConsumerInstrumenter(TextMapGetter<REQUEST> getter) {
return newInstrumenter(
InstrumenterConstructor.propagatingFromUpstream(getter),
SpanKindExtractor.alwaysConsumer());
}

/**
Expand All @@ -122,70 +154,25 @@ public Instrumenter<REQUEST, RESPONSE> newInstrumenter(
private Instrumenter<REQUEST, RESPONSE> newInstrumenter(
InstrumenterConstructor<REQUEST, RESPONSE> constructor,
SpanKindExtractor<? super REQUEST> spanKindExtractor) {
return constructor.create(
instrumentationName,
openTelemetry.getTracer(instrumentationName, InstrumentationVersion.VERSION),
spanNameExtractor,
spanKindExtractor,
spanStatusExtractor,
new ArrayList<>(attributesExtractors),
errorCauseExtractor);
this.spanKindExtractor = spanKindExtractor;
return constructor.create(this);
}

private interface InstrumenterConstructor<RQ, RS> {
Instrumenter<RQ, RS> create(
String instrumentationName,
Tracer tracer,
SpanNameExtractor<? super RQ> spanNameExtractor,
SpanKindExtractor<? super RQ> spanKindExtractor,
SpanStatusExtractor<? super RQ, ? super RS> spanStatusExtractor,
List<? extends AttributesExtractor<? super RQ, ? super RS>> extractors,
ErrorCauseExtractor errorCauseExtractor);
Instrumenter<RQ, RS> create(InstrumenterBuilder<RQ, RS> builder);

static <RQ, RS> InstrumenterConstructor<RQ, RS> internal() {
return Instrumenter::new;
}

static <RQ, RS> InstrumenterConstructor<RQ, RS> propagatingToDownstream(
ContextPropagators propagators, TextMapSetter<RQ> setter) {
return (instrumentationName,
tracer,
spanName,
spanKind,
spanStatus,
attributes,
errorCauseExtractor) ->
new ClientInstrumenter<>(
instrumentationName,
tracer,
spanName,
spanKind,
spanStatus,
attributes,
errorCauseExtractor,
propagators,
setter);
TextMapSetter<RQ> setter) {
return builder -> new ClientInstrumenter<>(builder, setter);
}

static <RQ, RS> InstrumenterConstructor<RQ, RS> propagatingFromUpstream(
ContextPropagators propagators, TextMapGetter<RQ> getter) {
return (instrumentationName,
tracer,
spanName,
spanKind,
spanStatus,
attributes,
errorCauseExtractor) ->
new ServerInstrumenter<>(
instrumentationName,
tracer,
spanName,
spanKind,
spanStatus,
attributes,
errorCauseExtractor,
propagators,
getter);
TextMapGetter<RQ> getter) {
return builder -> new ServerInstrumenter<>(builder, getter);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,20 @@

package io.opentelemetry.instrumentation.api.instrumenter;

import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.instrumentation.api.internal.ContextPropagationDebug;
import java.util.List;

final class ServerInstrumenter<REQUEST, RESPONSE> extends Instrumenter<REQUEST, RESPONSE> {

private final ContextPropagators propagators;
private final TextMapGetter<REQUEST> getter;

ServerInstrumenter(
String instrumentationName,
Tracer tracer,
SpanNameExtractor<? super REQUEST> spanNameExtractor,
SpanKindExtractor<? super REQUEST> spanKindExtractor,
SpanStatusExtractor<? super REQUEST, ? super RESPONSE> spanStatusExtractor,
List<? extends AttributesExtractor<? super REQUEST, ? super RESPONSE>> attributesExtractors,
ErrorCauseExtractor errorCauseExtractor,
ContextPropagators propagators,
TextMapGetter<REQUEST> getter) {
super(
instrumentationName,
tracer,
spanNameExtractor,
spanKindExtractor,
spanStatusExtractor,
attributesExtractors,
errorCauseExtractor);
this.propagators = propagators;
public ServerInstrumenter(
InstrumenterBuilder<REQUEST, RESPONSE> builder, TextMapGetter<REQUEST> getter) {
super(builder);
this.propagators = builder.openTelemetry.getPropagators();
this.getter = getter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ static <REQUEST> SpanKindExtractor<REQUEST> alwaysServer() {
return request -> SpanKind.SERVER;
}

/** Returns a {@link SpanNameExtractor} which always returns {@link SpanKind#PRODUCER}. */
static <REQUEST> SpanKindExtractor<REQUEST> alwaysProducer() {
return request -> SpanKind.PRODUCER;
}

/** Returns a {@link SpanNameExtractor} which always returns {@link SpanKind#CONSUMER}. */
static <REQUEST> SpanKindExtractor<REQUEST> alwaysConsumer() {
return request -> SpanKind.CONSUMER;
}

/** Returns the {@link SpanKind} corresponding to the {@link REQUEST}. */
SpanKind extract(REQUEST request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter;

import java.time.Instant;

/**
* Extractor of the start time of request processing. A {@link StartTimeExtractor} should always use
* the same timestamp source as the corresponding {@link EndTimeExtractor} - extracted timestamps
* must be comparable.
*/
@FunctionalInterface
public interface StartTimeExtractor<REQUEST> {

/** Returns the timestamp marking the start of the request processing. */
Instant extract(REQUEST request);
}
Loading