Skip to content
Open
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
@@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.ratpack;

import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesGetter;
import javax.annotation.Nullable;
import ratpack.handling.Handler;

public class RatpackCodeAttributesGetter implements CodeAttributesGetter<Handler> {

@Nullable
@Override
public Class<?> getCodeClass(Handler handler) {
return handler.getClass();
}

@Nullable
@Override
public String getMethodName(Handler handler) {
return "handle";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,34 @@
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.ErrorCauseExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource;
import io.opentelemetry.javaagent.bootstrap.internal.ExperimentalConfig;
import ratpack.handling.Context;
import ratpack.handling.Handler;

public final class RatpackSingletons {

private static final Instrumenter<String, Void> INSTRUMENTER =
Instrumenter.<String, Void>builder(
GlobalOpenTelemetry.get(), "io.opentelemetry.ratpack-1.4", s -> s)
.setEnabled(ExperimentalConfig.get().controllerTelemetryEnabled())
.buildInstrumenter();
private static final Instrumenter<Handler, Void> INSTRUMENTER;

public static Instrumenter<String, Void> instrumenter() {
static {
RatpackCodeAttributesGetter codeAttributesGetter = new RatpackCodeAttributesGetter();

INSTRUMENTER =
Instrumenter.<Handler, Void>builder(
GlobalOpenTelemetry.get(),
"io.opentelemetry.ratpack-1.4",
CodeSpanNameExtractor.create(codeAttributesGetter))
.addAttributesExtractor(CodeAttributesExtractor.create(codeAttributesGetter))
.setEnabled(ExperimentalConfig.get().controllerTelemetryEnabled())
.buildInstrumenter();
}

public static Instrumenter<Handler, Void> instrumenter() {
return INSTRUMENTER;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

public final class TracingHandler implements Handler {

private static final String INITIAL_SPAN_NAME = "ratpack.handler";

public static final Handler INSTANCE = new TracingHandler();

@Override
Expand All @@ -34,15 +32,15 @@ public void handle(Context ctx) {
serverContext != null ? serverContext.context() : Java8BytecodeBridge.currentContext();
io.opentelemetry.context.Context callbackContext;

if (instrumenter().shouldStart(parentOtelContext, INITIAL_SPAN_NAME)) {
if (instrumenter().shouldStart(parentOtelContext, INSTANCE)) {
io.opentelemetry.context.Context otelContext =
instrumenter().start(parentOtelContext, INITIAL_SPAN_NAME);
instrumenter().start(parentOtelContext, INSTANCE);
ctx.getExecution().add(otelContext);
ctx.getResponse()
.beforeSend(
response -> {
updateSpanNames(otelContext, ctx);
instrumenter().end(otelContext, INITIAL_SPAN_NAME, null, null);
instrumenter().end(otelContext, INSTANCE, null, null);
});
callbackContext = otelContext;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.ratpack.server;

import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeFunctionAssertions;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.ERROR;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION;
Expand Down Expand Up @@ -315,7 +316,11 @@ protected SpanDataAssert assertHandlerSpan(
} else {
spanName = endpoint.getPath();
}
span.hasName(spanName).hasKind(SpanKind.INTERNAL);
span.hasName(spanName)
.hasKind(SpanKind.INTERNAL)
.hasAttributesSatisfyingExactly(
codeFunctionAssertions(
"io.opentelemetry.javaagent.instrumentation.ratpack.TracingHandler", "handle"));
Comment on lines +319 to +323
Copy link
Contributor

Choose a reason for hiding this comment

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

I remember we had a similar discussion a while ago (in the context of #7345), but do you think we could capture the user/application code that implements the handler instead of the ratpack implementation class ?

While it is not written explicitly in semantic conventions, giving priority to classes/methods of the application is expected to provide "more value" in the sense that the consumer of this data has more leverage on the application implementation than on the underlying framework.

We could also implement a few alternatives:

  • capture two spans: one for the framework, one for the "user/application code", the downside is that they would always have similar duration, but it could allow to compare usages of the same framework feature across applications for example
  • extend semantic conventions to provide a clear split between "technical" or "framework" code attributes and the "application".

In think that having a common strategy to capture it would probably be beneficial, especially if we want to move forward the work on #7345.

Copy link
Contributor

Choose a reason for hiding this comment

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

I totally agree that adding TracingHandler here isn't useful and using the actual user class that handles the request would be much better. The only problem is that getting that class isn't easy.

if (endpoint == EXCEPTION) {
span.hasStatus(StatusData.error())
.hasException(new IllegalStateException(EXCEPTION.getBody()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.ratpack.server;

import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeFunctionAssertions;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
import static io.opentelemetry.semconv.ClientAttributes.CLIENT_ADDRESS;
Expand All @@ -22,7 +23,6 @@
import static io.opentelemetry.semconv.UserAgentAttributes.USER_AGENT_ORIGINAL;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.test.utils.PortUtils;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
Expand Down Expand Up @@ -162,7 +162,10 @@ void bindingsForPath(String path, String route) {
span.hasName("/" + route)
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.hasAttributes(Attributes.empty()));
.hasAttributesSatisfyingExactly(
codeFunctionAssertions(
"io.opentelemetry.javaagent.instrumentation.ratpack.TracingHandler",
"handle")));
}

trace.hasSpansSatisfyingExactly(assertions);
Expand Down