Skip to content
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 @@ -59,8 +59,21 @@ afterEvaluate {
}
}

tasks.test {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
tasks {
withType<Test>().configureEach {
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
}

val testStableSemconv by registering(Test::class) {
testClassesDirs = sourceSets.test.get().output.classesDirs
classpath = sourceSets.test.get().runtimeClasspath
jvmArgs("-Dotel.semconv-stability.opt-in=rpc")
systemProperty("metadataConfig", "otel.semconv-stability.opt-in=rpc")
}

check {
dependsOn(testStableSemconv)
}
}

if (findProperty("denyUnsafe") as Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@

package io.opentelemetry.javaagent.instrumentation.armeria.grpc.v1_14;

import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitOldRpcSemconv;
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableRpcSemconv;
import static io.opentelemetry.instrumentation.testing.junit.rpc.SemconvRpcStabilityUtil.maybeStable;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
import static io.opentelemetry.semconv.incubating.MessageIncubatingAttributes.MESSAGE_ID;
import static io.opentelemetry.semconv.incubating.MessageIncubatingAttributes.MESSAGE_TYPE;
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_GRPC_STATUS_CODE;
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_METHOD;
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_RESPONSE_STATUS_CODE;
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_SERVICE;
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_SYSTEM;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -78,10 +82,17 @@ void grpcInstrumentation() {
.hasKind(SpanKind.CLIENT)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(RPC_SYSTEM, "grpc"),
equalTo(RPC_SERVICE, "example.Greeter"),
equalTo(RPC_METHOD, "SayHello"),
equalTo(RPC_GRPC_STATUS_CODE, (long) Status.Code.OK.value()),
equalTo(maybeStable(RPC_SYSTEM), "grpc"),
equalTo(RPC_SERVICE, emitOldRpcSemconv() ? "example.Greeter" : null),
equalTo(
RPC_METHOD,
emitStableRpcSemconv() ? "example.Greeter/SayHello" : "SayHello"),
equalTo(
RPC_GRPC_STATUS_CODE,
emitOldRpcSemconv() ? (long) Status.Code.OK.value() : null),
equalTo(
RPC_RESPONSE_STATUS_CODE,
emitStableRpcSemconv() ? Status.Code.OK.name() : null),
equalTo(SERVER_ADDRESS, "127.0.0.1"),
equalTo(SERVER_PORT, (long) server.httpPort()))
.hasEventsSatisfyingExactly(
Expand All @@ -101,10 +112,17 @@ void grpcInstrumentation() {
.hasKind(SpanKind.SERVER)
.hasParent(trace.getSpan(1))
.hasAttributesSatisfyingExactly(
equalTo(RPC_SYSTEM, "grpc"),
equalTo(RPC_SERVICE, "example.Greeter"),
equalTo(RPC_METHOD, "SayHello"),
equalTo(RPC_GRPC_STATUS_CODE, (long) Status.Code.OK.value()),
equalTo(maybeStable(RPC_SYSTEM), "grpc"),
equalTo(RPC_SERVICE, emitOldRpcSemconv() ? "example.Greeter" : null),
equalTo(
RPC_METHOD,
emitStableRpcSemconv() ? "example.Greeter/SayHello" : "SayHello"),
equalTo(
RPC_GRPC_STATUS_CODE,
emitOldRpcSemconv() ? (long) Status.Code.OK.value() : null),
equalTo(
RPC_RESPONSE_STATUS_CODE,
emitStableRpcSemconv() ? Status.Code.OK.name() : null),
equalTo(SERVER_ADDRESS, "127.0.0.1"),
equalTo(SERVER_PORT, server.httpPort()))
.hasEventsSatisfyingExactly(
Expand Down
40 changes: 30 additions & 10 deletions instrumentation/grpc-1.6/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {
val collectMetadata = findProperty("collectMetadata")?.toString() ?: "false"

tasks {
test {
withType<Test>().configureEach {
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)
// The agent context debug mechanism isn't compatible with the bridge approach which may add a
// gRPC context to the root.
Expand All @@ -56,26 +56,46 @@ tasks {
testClassesDirs = sourceSets.test.get().output.classesDirs
classpath = sourceSets.test.get().runtimeClasspath

// replicated base config from standard test task
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)
jvmArgs("-Dotel.javaagent.experimental.thread-propagation-debugger.enabled=false")
jvmArgs("-Dotel.instrumentation.grpc.capture-metadata.client.request=some-client-key")
jvmArgs("-Dotel.instrumentation.grpc.capture-metadata.server.request=some-server-key")
jvmArgs("-Dotel.instrumentation.common.experimental.controller-telemetry.enabled=true")

// exclude our grpc library instrumentation, the ContextStorageOverride contained within it
// breaks the tests
classpath = classpath.filter {
!it.absolutePath.contains("opentelemetry-grpc-1.6")
}

systemProperty("collectMetadata", collectMetadata)
systemProperty("metadataConfig", "otel.instrumentation.grpc.experimental-span-attributes=true")
jvmArgs("-Dotel.instrumentation.grpc.experimental-span-attributes=true")
}

val testStableSemconv by registering(Test::class) {
testClassesDirs = sourceSets.test.get().output.classesDirs
classpath = sourceSets.test.get().runtimeClasspath

// exclude our grpc library instrumentation, the ContextStorageOverride contained within it
// breaks the tests
classpath = classpath.filter {
!it.absolutePath.contains("opentelemetry-grpc-1.6")
}

jvmArgs("-Dotel.semconv-stability.opt-in=rpc")
systemProperty("metadataConfig", "otel.semconv-stability.opt-in=rpc")
}

val testBothSemconv by registering(Test::class) {
testClassesDirs = sourceSets.test.get().output.classesDirs
classpath = sourceSets.test.get().runtimeClasspath

// exclude our grpc library instrumentation, the ContextStorageOverride contained within it
// breaks the tests
classpath = classpath.filter {
!it.absolutePath.contains("opentelemetry-grpc-1.6")
}

jvmArgs("-Dotel.semconv-stability.opt-in=rpc/dup")
systemProperty("metadataConfig", "otel.semconv-stability.opt-in=rpc/dup")
}

check {
dependsOn(testExperimental)
dependsOn(testExperimental, testStableSemconv, testBothSemconv)
}
}

Expand Down
25 changes: 23 additions & 2 deletions instrumentation/grpc-1.6/library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,34 @@ dependencies {
}

tasks {
test {
withType<Test>().configureEach {
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)
jvmArgs("-Dotel.instrumentation.common.experimental.controller-telemetry.enabled=true")
// latest dep test occasionally fails because network type is ipv6 instead of the expected ipv4
// and peer address is 0:0:0:0:0:0:0:1 instead of 127.0.0.1
jvmArgs("-Djava.net.preferIPv4Stack=true")
}

test {
jvmArgs("-Dotel.instrumentation.common.experimental.controller-telemetry.enabled=true")
}

val testStableSemconv by registering(Test::class) {
testClassesDirs = sourceSets.test.get().output.classesDirs
classpath = sourceSets.test.get().runtimeClasspath

jvmArgs("-Dotel.semconv-stability.opt-in=rpc")
}

val testBothSemconv by registering(Test::class) {
testClassesDirs = sourceSets.test.get().output.classesDirs
classpath = sourceSets.test.get().runtimeClasspath

jvmArgs("-Dotel.semconv-stability.opt-in=rpc/dup")
}

check {
dependsOn(testStableSemconv, testBothSemconv)
}
}

if (!(findProperty("testLatestDeps") as Boolean)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
final class CapturedGrpcMetadataUtil {
private static final String RPC_REQUEST_METADATA_KEY_ATTRIBUTE_PREFIX =
"rpc.grpc.request.metadata.";
private static final String RPC_STABLE_REQUEST_METADATA_KEY_ATTRIBUTE_PREFIX =
"rpc.request.metadata.";
private static final ConcurrentMap<String, AttributeKey<List<String>>> requestKeysCache =
new ConcurrentHashMap<>();
private static final ConcurrentMap<String, AttributeKey<List<String>>> stableRequestKeysCache =
new ConcurrentHashMap<>();

static List<String> lowercase(List<String> names) {
return unmodifiableList(names.stream().map(s -> s.toLowerCase(Locale.ROOT)).collect(toList()));
Expand All @@ -29,9 +33,19 @@ static AttributeKey<List<String>> requestAttributeKey(String metadataKey) {
metadataKey, CapturedGrpcMetadataUtil::createRequestKey);
}

static AttributeKey<List<String>> stableRequestAttributeKey(String metadataKey) {
return stableRequestKeysCache.computeIfAbsent(
metadataKey, CapturedGrpcMetadataUtil::createStableRequestKey);
}

private static AttributeKey<List<String>> createRequestKey(String metadataKey) {
return AttributeKey.stringArrayKey(RPC_REQUEST_METADATA_KEY_ATTRIBUTE_PREFIX + metadataKey);
}

private static AttributeKey<List<String>> createStableRequestKey(String metadataKey) {
return AttributeKey.stringArrayKey(
RPC_STABLE_REQUEST_METADATA_KEY_ATTRIBUTE_PREFIX + metadataKey);
}

private CapturedGrpcMetadataUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

package io.opentelemetry.instrumentation.grpc.v1_6;

import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitOldRpcSemconv;
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableRpcSemconv;
import static io.opentelemetry.instrumentation.grpc.v1_6.CapturedGrpcMetadataUtil.lowercase;
import static io.opentelemetry.instrumentation.grpc.v1_6.CapturedGrpcMetadataUtil.requestAttributeKey;
import static io.opentelemetry.instrumentation.grpc.v1_6.CapturedGrpcMetadataUtil.stableRequestAttributeKey;

import io.grpc.Status;
import io.opentelemetry.api.common.AttributeKey;
Expand All @@ -21,6 +24,8 @@ final class GrpcAttributesExtractor implements AttributesExtractor<GrpcRequest,
// copied from RpcIncubatingAttributes
private static final AttributeKey<Long> RPC_GRPC_STATUS_CODE =
AttributeKey.longKey("rpc.grpc.status_code");
private static final AttributeKey<String> RPC_RESPONSE_STATUS_CODE =
AttributeKey.stringKey("rpc.response.status_code");

private final GrpcRpcAttributesGetter getter;
private final List<String> capturedRequestMetadata;
Expand All @@ -44,12 +49,22 @@ public void onEnd(
@Nullable Status status,
@Nullable Throwable error) {
if (status != null) {
attributes.put(RPC_GRPC_STATUS_CODE, status.getCode().value());
if (emitOldRpcSemconv()) {
attributes.put(RPC_GRPC_STATUS_CODE, status.getCode().value());
}
if (emitStableRpcSemconv()) {
attributes.put(RPC_RESPONSE_STATUS_CODE, status.getCode().name());
}
}
for (String key : capturedRequestMetadata) {
List<String> value = getter.metadataValue(request, key);
if (!value.isEmpty()) {
attributes.put(requestAttributeKey(key), value);
if (emitOldRpcSemconv()) {
attributes.put(requestAttributeKey(key), value);
}
if (emitStableRpcSemconv()) {
attributes.put(stableRequestAttributeKey(key), value);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public String getMethod(GrpcRequest request) {
return fullMethodName.substring(slashIndex + 1);
}

@Override
public String getRpcMethod(GrpcRequest request) {
return request.getMethod().getFullMethodName();
}

@Override
@Nullable
public Long getRequestSize(GrpcRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.rpc.RpcClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.rpc.RpcClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.rpc.RpcMetricsContextCustomizers;
import io.opentelemetry.instrumentation.api.incubator.semconv.rpc.RpcServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.rpc.RpcServerMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.rpc.RpcSizeAttributesExtractor;
Expand Down Expand Up @@ -151,6 +152,7 @@ public GrpcTelemetryBuilder setCapturedServerRequestMetadata(
}

/** Returns a new {@link GrpcTelemetry} with the settings of this {@link GrpcTelemetryBuilder}. */
@SuppressWarnings("deprecation") // RpcMetricsContextCustomizers is deprecated for removal in 3.0
Copy link
Contributor

Choose a reason for hiding this comment

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

the classes used for transition could just be internal classes

Copy link
Member Author

Choose a reason for hiding this comment

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

I kind of like to make them public to support anyone outside of this repo going through the transition, although maybe that's not too realistic since they'd have to stick to our timeline as we'll be removing them in 3.0

public GrpcTelemetry build() {
SpanNameExtractor<GrpcRequest> originalSpanNameExtractor = new GrpcSpanNameExtractor();
SpanNameExtractor<? super GrpcRequest> clientSpanNameExtractor =
Expand Down Expand Up @@ -179,7 +181,9 @@ public GrpcTelemetry build() {
.addAttributesExtractor(
new GrpcAttributesExtractor(
GrpcRpcAttributesGetter.INSTANCE, capturedClientRequestMetadata))
.addOperationMetrics(RpcClientMetrics.get());
.addOperationMetrics(RpcClientMetrics.get())
.addContextCustomizer(
RpcMetricsContextCustomizers.dualEmitContextCustomizer(rpcAttributesGetter));
Experimental.addOperationListenerAttributesExtractor(
clientInstrumenterBuilder, RpcSizeAttributesExtractor.create(rpcAttributesGetter));
serverInstrumenterBuilder
Expand All @@ -192,7 +196,9 @@ public GrpcTelemetry build() {
new GrpcAttributesExtractor(
GrpcRpcAttributesGetter.INSTANCE, capturedServerRequestMetadata))
.addAttributesExtractors(additionalServerExtractors)
.addOperationMetrics(RpcServerMetrics.get());
.addOperationMetrics(RpcServerMetrics.get())
.addContextCustomizer(
RpcMetricsContextCustomizers.dualEmitContextCustomizer(rpcAttributesGetter));
Experimental.addOperationListenerAttributesExtractor(
serverInstrumenterBuilder, RpcSizeAttributesExtractor.create(rpcAttributesGetter));

Expand Down
Loading
Loading