Skip to content

Commit

Permalink
Update semconv (#10272)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Jan 30, 2024
1 parent 0337158 commit b57c1a0
Show file tree
Hide file tree
Showing 89 changed files with 858 additions and 304 deletions.
2 changes: 1 addition & 1 deletion dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ val mockitoVersion = "4.11.0"
val slf4jVersion = "2.0.11"

val CORE_DEPENDENCIES = listOf(
"io.opentelemetry.semconv:opentelemetry-semconv:1.21.0-alpha",
"io.opentelemetry.semconv:opentelemetry-semconv:1.23.1-alpha",
"com.google.auto.service:auto-service:${autoServiceVersion}",
"com.google.auto.service:auto-service-annotations:${autoServiceVersion}",
"com.google.auto.value:auto-value:${autoValueVersion}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import io.opentelemetry.api.metrics.LongUpDownCounterBuilder;
import io.opentelemetry.extension.incubator.metrics.ExtendedLongHistogramBuilder;
import io.opentelemetry.extension.incubator.metrics.ExtendedLongUpDownCounterBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.internal.HttpAttributes;
import io.opentelemetry.semconv.SemanticAttributes;

final class HttpExperimentalMetricsAdvice {
Expand All @@ -25,7 +24,7 @@ static void applyClientRequestSizeAdvice(LongHistogramBuilder builder) {
asList(
SemanticAttributes.HTTP_REQUEST_METHOD,
SemanticAttributes.HTTP_RESPONSE_STATUS_CODE,
HttpAttributes.ERROR_TYPE,
SemanticAttributes.ERROR_TYPE,
SemanticAttributes.NETWORK_PROTOCOL_NAME,
SemanticAttributes.NETWORK_PROTOCOL_VERSION,
SemanticAttributes.SERVER_ADDRESS,
Expand All @@ -43,7 +42,7 @@ static void applyServerRequestSizeAdvice(LongHistogramBuilder builder) {
SemanticAttributes.HTTP_ROUTE,
SemanticAttributes.HTTP_REQUEST_METHOD,
SemanticAttributes.HTTP_RESPONSE_STATUS_CODE,
HttpAttributes.ERROR_TYPE,
SemanticAttributes.ERROR_TYPE,
SemanticAttributes.NETWORK_PROTOCOL_NAME,
SemanticAttributes.NETWORK_PROTOCOL_VERSION,
SemanticAttributes.URL_SCHEME));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,28 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
attributes,
SemanticAttributes.MESSAGING_DESTINATION_NAME,
getter.getDestination(request));
internalSet(
attributes,
SemanticAttributes.MESSAGING_DESTINATION_TEMPLATE,
getter.getDestinationTemplate(request));
}
boolean isAnonymousDestination = getter.isAnonymousDestination(request);
if (isAnonymousDestination) {
internalSet(attributes, SemanticAttributes.MESSAGING_DESTINATION_ANONYMOUS, true);
}
internalSet(
attributes,
SemanticAttributes.MESSAGING_MESSAGE_CONVERSATION_ID,
getter.getConversationId(request));
internalSet(
attributes,
SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES,
getter.getMessagePayloadSize(request));
SemanticAttributes.MESSAGING_MESSAGE_BODY_SIZE,
getter.getMessageBodySize(request));
internalSet(
attributes,
SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_COMPRESSED_SIZE_BYTES,
getter.getMessagePayloadCompressedSize(request));
SemanticAttributes.MESSAGING_MESSAGE_ENVELOPE_SIZE,
getter.getMessageEnvelopeSize(request));
internalSet(attributes, SemanticAttributes.MESSAGING_CLIENT_ID, getter.getClientId(request));
if (operation != null) {
internalSet(attributes, SemanticAttributes.MESSAGING_OPERATION, operation.operationName());
}
Expand All @@ -101,6 +110,10 @@ public void onEnd(
attributes,
SemanticAttributes.MESSAGING_MESSAGE_ID,
getter.getMessageId(request, response));
internalSet(
attributes,
SemanticAttributes.MESSAGING_BATCH_MESSAGE_COUNT,
getter.getBatchMessageCount(request, response));

for (String name : capturedHeaders) {
List<String> values = getter.getMessageHeader(request, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,43 @@ public interface MessagingAttributesGetter<REQUEST, RESPONSE> {
@Nullable
String getDestination(REQUEST request);

@Nullable
String getDestinationTemplate(REQUEST request);

boolean isTemporaryDestination(REQUEST request);

boolean isAnonymousDestination(REQUEST request);

@Nullable
String getConversationId(REQUEST request);

@Nullable
Long getMessagePayloadSize(REQUEST request);
@Deprecated
default Long getMessagePayloadSize(REQUEST request) {
return null;
}

@Nullable
@Deprecated
default Long getMessagePayloadCompressedSize(REQUEST request) {
return null;
}

@Nullable
Long getMessagePayloadCompressedSize(REQUEST request);
Long getMessageBodySize(REQUEST request);

@Nullable
Long getMessageEnvelopeSize(REQUEST request);

@Nullable
String getMessageId(REQUEST request, @Nullable RESPONSE response);

@Nullable
String getClientId(REQUEST request);

@Nullable
Long getBatchMessageCount(REQUEST request, @Nullable RESPONSE response);

/**
* Extracts all values of header named {@code name} from the request, or an empty list if there
* were none.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
import io.opentelemetry.instrumentation.api.semconv.http.internal.HttpAttributes;
import io.opentelemetry.instrumentation.api.semconv.network.internal.NetworkAttributes;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
Expand Down Expand Up @@ -47,7 +46,7 @@ void collectsMetrics() {
Attributes responseAttributes =
Attributes.builder()
.put(SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 200)
.put(HttpAttributes.ERROR_TYPE, "400")
.put(SemanticAttributes.ERROR_TYPE, "400")
.put(SemanticAttributes.HTTP_REQUEST_BODY_SIZE, 100)
.put(SemanticAttributes.HTTP_RESPONSE_BODY_SIZE, 200)
.put(SemanticAttributes.NETWORK_PROTOCOL_NAME, "http")
Expand Down Expand Up @@ -93,7 +92,7 @@ void collectsMetrics() {
equalTo(SemanticAttributes.HTTP_REQUEST_METHOD, "GET"),
equalTo(
SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 200),
equalTo(HttpAttributes.ERROR_TYPE, "400"),
equalTo(SemanticAttributes.ERROR_TYPE, "400"),
equalTo(
SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"),
equalTo(
Expand All @@ -120,7 +119,7 @@ void collectsMetrics() {
equalTo(SemanticAttributes.HTTP_REQUEST_METHOD, "GET"),
equalTo(
SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 200),
equalTo(HttpAttributes.ERROR_TYPE, "400"),
equalTo(SemanticAttributes.ERROR_TYPE, "400"),
equalTo(
SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"),
equalTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
import io.opentelemetry.instrumentation.api.semconv.http.internal.HttpAttributes;
import io.opentelemetry.instrumentation.api.semconv.network.internal.NetworkAttributes;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
Expand Down Expand Up @@ -51,7 +50,7 @@ void collectsMetrics() {
Attributes responseAttributes =
Attributes.builder()
.put(SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 200)
.put(HttpAttributes.ERROR_TYPE, "500")
.put(SemanticAttributes.ERROR_TYPE, "500")
.put(SemanticAttributes.HTTP_REQUEST_BODY_SIZE, 100)
.put(SemanticAttributes.HTTP_RESPONSE_BODY_SIZE, 200)
.put(NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4")
Expand Down Expand Up @@ -157,7 +156,7 @@ void collectsMetrics() {
equalTo(SemanticAttributes.HTTP_REQUEST_METHOD, "GET"),
equalTo(
SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 200),
equalTo(HttpAttributes.ERROR_TYPE, "500"),
equalTo(SemanticAttributes.ERROR_TYPE, "500"),
equalTo(
SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"),
equalTo(
Expand All @@ -183,7 +182,7 @@ void collectsMetrics() {
equalTo(SemanticAttributes.HTTP_REQUEST_METHOD, "GET"),
equalTo(
SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 200),
equalTo(HttpAttributes.ERROR_TYPE, "500"),
equalTo(SemanticAttributes.ERROR_TYPE, "500"),
equalTo(
SemanticAttributes.NETWORK_PROTOCOL_NAME, "http"),
equalTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.assertj.core.data.MapEntry;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -32,6 +33,7 @@ class MessagingAttributesExtractorTest {
@MethodSource("destinations")
void shouldExtractAllAvailableAttributes(
boolean temporary,
boolean anonymous,
String destination,
MessageOperation operation,
String expectedDestination) {
Expand All @@ -40,13 +42,19 @@ void shouldExtractAllAvailableAttributes(
request.put("system", "myQueue");
request.put("destinationKind", "topic");
request.put("destination", destination);
request.put("destinationTemplate", destination);
if (temporary) {
request.put("temporaryDestination", "y");
}
if (anonymous) {
request.put("anonymousDestination", "y");
}
request.put("url", "http://broker/topic");
request.put("conversationId", "42");
request.put("payloadSize", "100");
request.put("payloadCompressedSize", "10");
request.put("bodySize", "100");
request.put("envelopeSize", "120");
request.put("clientId", "43");
request.put("batchMessageCount", "2");

AttributesExtractor<Map<String, String>, String> underTest =
MessagingAttributesExtractor.create(TestGetter.INSTANCE, operation);
Expand All @@ -66,11 +74,17 @@ void shouldExtractAllAvailableAttributes(
expectedEntries.add(entry(SemanticAttributes.MESSAGING_DESTINATION_NAME, expectedDestination));
if (temporary) {
expectedEntries.add(entry(SemanticAttributes.MESSAGING_DESTINATION_TEMPORARY, true));
} else {
expectedEntries.add(
entry(SemanticAttributes.MESSAGING_DESTINATION_TEMPLATE, expectedDestination));
}
if (anonymous) {
expectedEntries.add(entry(SemanticAttributes.MESSAGING_DESTINATION_ANONYMOUS, true));
}
expectedEntries.add(entry(SemanticAttributes.MESSAGING_MESSAGE_CONVERSATION_ID, "42"));
expectedEntries.add(entry(SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES, 100L));
expectedEntries.add(
entry(SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_COMPRESSED_SIZE_BYTES, 10L));
expectedEntries.add(entry(SemanticAttributes.MESSAGING_MESSAGE_BODY_SIZE, 100L));
expectedEntries.add(entry(SemanticAttributes.MESSAGING_MESSAGE_ENVELOPE_SIZE, 120L));
expectedEntries.add(entry(SemanticAttributes.MESSAGING_CLIENT_ID, "43"));
expectedEntries.add(entry(SemanticAttributes.MESSAGING_OPERATION, operation.operationName()));

@SuppressWarnings({"unchecked", "rawtypes"})
Expand All @@ -79,13 +93,15 @@ void shouldExtractAllAvailableAttributes(
assertThat(startAttributes.build()).containsOnly(expectedEntriesArr);

assertThat(endAttributes.build())
.containsOnly(entry(SemanticAttributes.MESSAGING_MESSAGE_ID, "42"));
.containsOnly(
entry(SemanticAttributes.MESSAGING_MESSAGE_ID, "42"),
entry(SemanticAttributes.MESSAGING_BATCH_MESSAGE_COUNT, 2L));
}

static Stream<Arguments> destinations() {
return Stream.of(
Arguments.of(false, "destination", MessageOperation.RECEIVE, "destination"),
Arguments.of(true, null, MessageOperation.PROCESS, "(temporary)"));
Arguments.of(false, false, "destination", MessageOperation.RECEIVE, "destination"),
Arguments.of(true, true, null, MessageOperation.PROCESS, "(temporary)"));
}

@Test
Expand Down Expand Up @@ -122,31 +138,57 @@ public String getDestination(Map<String, String> request) {
return request.get("destination");
}

@Nullable
@Override
public String getDestinationTemplate(Map<String, String> request) {
return request.get("destinationTemplate");
}

@Override
public boolean isTemporaryDestination(Map<String, String> request) {
return request.containsKey("temporaryDestination");
}

@Override
public boolean isAnonymousDestination(Map<String, String> request) {
return request.containsKey("anonymousDestination");
}

@Override
public String getConversationId(Map<String, String> request) {
return request.get("conversationId");
}

@Nullable
@Override
public Long getMessagePayloadSize(Map<String, String> request) {
String payloadSize = request.get("payloadSize");
public Long getMessageBodySize(Map<String, String> request) {
String payloadSize = request.get("bodySize");
return payloadSize == null ? null : Long.valueOf(payloadSize);
}

@Nullable
@Override
public Long getMessagePayloadCompressedSize(Map<String, String> request) {
String payloadSize = request.get("payloadCompressedSize");
public Long getMessageEnvelopeSize(Map<String, String> request) {
String payloadSize = request.get("envelopeSize");
return payloadSize == null ? null : Long.valueOf(payloadSize);
}

@Override
public String getMessageId(Map<String, String> request, String response) {
return response;
}

@Nullable
@Override
public String getClientId(Map<String, String> request) {
return request.get("clientId");
}

@Nullable
@Override
public Long getBatchMessageCount(Map<String, String> request, @Nullable String response) {
String payloadSize = request.get("batchMessageCount");
return payloadSize == null ? null : Long.valueOf(payloadSize);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.internal.SpanKey;
import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider;
import io.opentelemetry.instrumentation.api.semconv.http.internal.HttpAttributes;
import io.opentelemetry.instrumentation.api.semconv.network.internal.InternalNetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.internal.InternalServerAttributesExtractor;
import io.opentelemetry.semconv.SemanticAttributes;
Expand Down Expand Up @@ -78,7 +77,7 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST

int resendCount = resendCountIncrementer.applyAsInt(parentContext);
if (resendCount > 0) {
attributes.put(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, resendCount);
attributes.put(SemanticAttributes.HTTP_REQUEST_RESEND_COUNT, resendCount);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.internal.HttpAttributes;
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesGetter;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.HashSet;
Expand Down Expand Up @@ -111,7 +110,7 @@ public void onEnd(
errorType = _OTHER;
}
}
internalSet(attributes, HttpAttributes.ERROR_TYPE, errorType);
internalSet(attributes, SemanticAttributes.ERROR_TYPE, errorType);

String protocolName = lowercaseStr(getter.getNetworkProtocolName(request, response));
String protocolVersion = lowercaseStr(getter.getNetworkProtocolVersion(request, response));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleHistogramBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.internal.HttpAttributes;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.List;

Expand All @@ -29,7 +28,7 @@ static void applyClientDurationAdvice(DoubleHistogramBuilder builder) {
asList(
SemanticAttributes.HTTP_REQUEST_METHOD,
SemanticAttributes.HTTP_RESPONSE_STATUS_CODE,
HttpAttributes.ERROR_TYPE,
SemanticAttributes.ERROR_TYPE,
SemanticAttributes.NETWORK_PROTOCOL_NAME,
SemanticAttributes.NETWORK_PROTOCOL_VERSION,
SemanticAttributes.SERVER_ADDRESS,
Expand All @@ -46,7 +45,7 @@ static void applyServerDurationAdvice(DoubleHistogramBuilder builder) {
SemanticAttributes.HTTP_ROUTE,
SemanticAttributes.HTTP_REQUEST_METHOD,
SemanticAttributes.HTTP_RESPONSE_STATUS_CODE,
HttpAttributes.ERROR_TYPE,
SemanticAttributes.ERROR_TYPE,
SemanticAttributes.NETWORK_PROTOCOL_NAME,
SemanticAttributes.NETWORK_PROTOCOL_VERSION,
SemanticAttributes.URL_SCHEME));
Expand Down
Loading

0 comments on commit b57c1a0

Please sign in to comment.