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

Add code attributes to several instrumentations #6365

Merged
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,11 +5,13 @@

package io.opentelemetry.javaagent.instrumentation.otelannotations;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry;
import static io.opentelemetry.api.common.AttributeKey.booleanKey;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.CODE_FUNCTION;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.CODE_NAMESPACE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
Expand Down Expand Up @@ -42,7 +44,8 @@ protected final InstrumentationExtension testing() {

@Test
void success() {
T future = newTraced().completable();
AbstractTraced<T, U> traced = newTraced();
T future = traced.completable();
complete(future, AbstractTraced.SUCCESS_VALUE);

assertThat(getCompleted(future)).isEqualTo(AbstractTraced.SUCCESS_VALUE);
Expand All @@ -54,12 +57,15 @@ void success() {
span.hasName("Traced.completable")
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasAttributes(Attributes.empty())));
.hasAttributesSatisfyingExactly(
equalTo(CODE_NAMESPACE, traced.getClass().getName()),
equalTo(CODE_FUNCTION, "completable"))));
}

@Test
void failure() {
T future = newTraced().completable();
AbstractTraced<T, U> traced = newTraced();
T future = traced.completable();
fail(future, AbstractTraced.FAILURE);

Throwable thrown = catchThrowable(() -> getCompleted(future));
Expand All @@ -74,12 +80,15 @@ void failure() {
.hasNoParent()
.hasStatus(StatusData.error())
.hasException(AbstractTraced.FAILURE)
.hasAttributes(Attributes.empty())));
.hasAttributesSatisfyingExactly(
equalTo(CODE_NAMESPACE, traced.getClass().getName()),
equalTo(CODE_FUNCTION, "completable"))));
}

@Test
void canceled() {
T future = newTraced().completable();
AbstractTraced<T, U> traced = newTraced();
T future = traced.completable();
cancel(future);

testing.waitAndAssertTraces(
Expand All @@ -89,13 +98,16 @@ void canceled() {
span.hasName("Traced.completable")
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasAttributes(attributeEntry(canceledKey(), true))));
.hasAttributesSatisfyingExactly(
equalTo(CODE_NAMESPACE, traced.getClass().getName()),
equalTo(CODE_FUNCTION, "completable"),
equalTo(booleanKey(canceledKey()), true))));
}

@Test
void immediateSuccess() {
assertThat(getCompleted(newTraced().alreadySucceeded()))
.isEqualTo(AbstractTraced.SUCCESS_VALUE);
AbstractTraced<T, U> traced = newTraced();
assertThat(getCompleted(traced.alreadySucceeded())).isEqualTo(AbstractTraced.SUCCESS_VALUE);

testing.waitAndAssertTraces(
trace ->
Expand All @@ -104,12 +116,15 @@ void immediateSuccess() {
span.hasName("Traced.alreadySucceeded")
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasAttributes(Attributes.empty())));
.hasAttributesSatisfyingExactly(
equalTo(CODE_NAMESPACE, traced.getClass().getName()),
equalTo(CODE_FUNCTION, "alreadySucceeded"))));
}

@Test
void immediateFailure() {
Throwable error = catchThrowable(() -> getCompleted(newTraced().alreadyFailed()));
AbstractTraced<T, U> traced = newTraced();
Throwable error = catchThrowable(() -> getCompleted(traced.alreadyFailed()));
assertThat(unwrapError(error)).isEqualTo(AbstractTraced.FAILURE);

testing.waitAndAssertTraces(
Expand All @@ -121,6 +136,8 @@ void immediateFailure() {
.hasNoParent()
.hasStatus(StatusData.error())
.hasException(AbstractTraced.FAILURE)
.hasAttributes(Attributes.empty())));
.hasAttributesSatisfyingExactly(
equalTo(CODE_NAMESPACE, traced.getClass().getName()),
equalTo(CODE_FUNCTION, "alreadyFailed"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.api.util;

import io.opentelemetry.instrumentation.api.instrumenter.code.CodeSpanNameExtractor;
import io.opentelemetry.instrumentation.api.internal.cache.Cache;
import java.lang.reflect.Method;
import java.util.Map;
Expand All @@ -26,15 +27,22 @@ public static String fromMethod(Method method) {
/**
* This method is used to generate a span name based on a method. Anonymous classes are named
* based on their parent.
*
* @deprecated Use {@link #fromMethod(Class, String)} instead.
*/
@Deprecated
public static String fromMethod(Class<?> clazz, @Nullable Method method) {
return fromMethod(clazz, method == null ? "<unknown>" : method.getName());
}

/**
* This method is used to generate a span name based on a method. Anonymous classes are named
* based on their parent.
*
* @deprecated Use {@link ClassAndMethod#codeAttributesGetter()} and {@link CodeSpanNameExtractor}
* instead.
*/
@Deprecated
public static String fromMethod(ClassAndMethod classAndMethod) {
return fromMethod(classAndMethod.declaringClass(), classAndMethod.methodName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
name "DocRepository.findAll"
kind INTERNAL
attributes {
"$SemanticAttributes.CODE_NAMESPACE" DocRepository.name
"$SemanticAttributes.CODE_FUNCTION" "findAll"
}
}
span(1) {
Expand Down Expand Up @@ -120,6 +122,8 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
name "DocRepository.index"
kind INTERNAL
attributes {
"$SemanticAttributes.CODE_NAMESPACE" DocRepository.name
"$SemanticAttributes.CODE_FUNCTION" "index"
}
}
span(1) {
Expand Down Expand Up @@ -169,6 +173,8 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
name "DocRepository.findById"
kind INTERNAL
attributes {
"$SemanticAttributes.CODE_NAMESPACE" DocRepository.name
"$SemanticAttributes.CODE_FUNCTION" "findById"
}
}
span(1) {
Expand Down Expand Up @@ -204,6 +210,8 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
name "DocRepository.index"
kind INTERNAL
attributes {
"$SemanticAttributes.CODE_NAMESPACE" DocRepository.name
"$SemanticAttributes.CODE_FUNCTION" "index"
}
}
span(1) {
Expand Down Expand Up @@ -245,6 +253,8 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
name "DocRepository.findById"
kind INTERNAL
attributes {
"$SemanticAttributes.CODE_NAMESPACE" DocRepository.name
"$SemanticAttributes.CODE_FUNCTION" "findById"
}
}
span(1) {
Expand Down Expand Up @@ -279,6 +289,8 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
name "DocRepository.deleteById"
kind INTERNAL
attributes {
"$SemanticAttributes.CODE_NAMESPACE" DocRepository.name
"$SemanticAttributes.CODE_FUNCTION" "deleteById"
}
}
span(1) {
Expand Down Expand Up @@ -320,6 +332,8 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
name "DocRepository.findAll"
kind INTERNAL
attributes {
"$SemanticAttributes.CODE_NAMESPACE" DocRepository.name
"$SemanticAttributes.CODE_FUNCTION" "findAll"
}
}
span(1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesGetter;
import io.opentelemetry.instrumentation.api.instrumenter.code.CodeSpanNameExtractor;
import io.opentelemetry.instrumentation.api.util.ClassAndMethod;
import io.opentelemetry.instrumentation.api.util.SpanNames;

public final class MethodSingletons {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.methods";

private static final Instrumenter<ClassAndMethod, Void> INSTRUMENTER;

static {
SpanNameExtractor<ClassAndMethod> spanName = SpanNames::fromMethod;
CodeAttributesGetter<ClassAndMethod> codeAttributesGetter =
ClassAndMethod.codeAttributesGetter();

INSTRUMENTER =
Instrumenter.<ClassAndMethod, Void>builder(
GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, spanName)
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
CodeSpanNameExtractor.create(codeAttributesGetter))
.addAttributesExtractor(CodeAttributesExtractor.create(codeAttributesGetter))
.buildInstrumenter(SpanKindExtractor.alwaysInternal());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
package io.opentelemetry.javaagent.instrumentation.methods;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.CODE_FUNCTION;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.CODE_NAMESPACE;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
Expand All @@ -33,7 +35,9 @@ void methodTraced() {
span ->
span.hasName("ConfigTracedCallable.call")
.hasKind(SpanKind.INTERNAL)
.hasAttributes(Attributes.empty())));
.hasAttributesSatisfyingExactly(
equalTo(CODE_NAMESPACE, ConfigTracedCallable.class.getName()),
equalTo(CODE_FUNCTION, "call"))));
}

static class ConfigTracedCallable implements Callable<String> {
Expand Down Expand Up @@ -62,7 +66,9 @@ void methodTracedWithAsyncStop() throws Exception {
span ->
span.hasName("ConfigTracedCompletableFuture.getResult")
.hasKind(SpanKind.INTERNAL)
.hasAttributes(Attributes.empty())));
.hasAttributesSatisfyingExactly(
equalTo(CODE_NAMESPACE, ConfigTracedCompletableFuture.class.getName()),
equalTo(CODE_FUNCTION, "getResult"))));
}

static class ConfigTracedCompletableFuture {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.opentelemetry.context.Context
import io.opentelemetry.context.ContextKey
import io.opentelemetry.extension.annotations.WithSpan
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes

import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
Expand All @@ -34,8 +35,8 @@ class ContextBridgeTest extends AgentInstrumentationSpecification {
}

def "application propagates agent's context"() {
when:
new Runnable() {
given:
def runnable = new Runnable() {
@WithSpan("test")
@Override
void run() {
Expand All @@ -49,7 +50,10 @@ class ContextBridgeTest extends AgentInstrumentationSpecification {
}
}
}
}.run()
}

when:
runnable.run()

then:
assertTraces(1) {
Expand All @@ -58,6 +62,8 @@ class ContextBridgeTest extends AgentInstrumentationSpecification {
name "test"
hasNoParent()
attributes {
"$SemanticAttributes.CODE_NAMESPACE" runnable.class.name
"$SemanticAttributes.CODE_FUNCTION" "run"
"cat" "yes"
}
}
Expand Down Expand Up @@ -92,8 +98,8 @@ class ContextBridgeTest extends AgentInstrumentationSpecification {
}

def "application propagates agent's span"() {
when:
new Runnable() {
given:
def runnable = new Runnable() {
@WithSpan("test")
@Override
void run() {
Expand All @@ -107,7 +113,10 @@ class ContextBridgeTest extends AgentInstrumentationSpecification {
}
}
}
}.run()
}

when:
runnable.run()

then:
assertTraces(1) {
Expand All @@ -116,6 +125,8 @@ class ContextBridgeTest extends AgentInstrumentationSpecification {
name "test"
hasNoParent()
attributes {
"$SemanticAttributes.CODE_NAMESPACE" runnable.class.name
"$SemanticAttributes.CODE_FUNCTION" "run"
"cat" "yes"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.extensionannotations;

import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesGetter;
import java.lang.reflect.Method;

enum MethodCodeAttributesGetter implements CodeAttributesGetter<Method> {
INSTANCE;

@Override
public Class<?> codeClass(Method method) {
return method.getDeclaringClass();
}

@Override
public String methodName(Method method) {
return method.getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.extensionannotations;

import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesGetter;

enum MethodRequestCodeAttributesGetter implements CodeAttributesGetter<MethodRequest> {
INSTANCE;

@Override
public Class<?> codeClass(MethodRequest methodRequest) {
return methodRequest.method().getDeclaringClass();
}

@Override
public String methodName(MethodRequest methodRequest) {
return methodRequest.method().getName();
}
}
Loading