Skip to content

Commit 1438a0a

Browse files
crossoverJieasweet-confluent
authored andcommitted
fix with cr
1 parent 43054ad commit 1438a0a

File tree

14 files changed

+67
-134
lines changed

14 files changed

+67
-134
lines changed

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcAttributesGetter.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,11 @@ public interface RpcAttributesGetter<REQUEST> {
2525
@Nullable
2626
String getMethod(REQUEST request);
2727

28-
default int getClientRequestSize(REQUEST request) {
29-
return 0;
28+
default Long getRequestSize(REQUEST request) {
29+
return null;
3030
}
3131

32-
default int getClientResponseSize(REQUEST request) {
33-
return 0;
34-
}
35-
36-
default int getServerRequestSize(REQUEST request) {
37-
return 0;
38-
}
39-
40-
default int getServerResponseSize(REQUEST request) {
41-
return 0;
32+
default Long getResponseSize(REQUEST request) {
33+
return null;
4234
}
4335
}

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcClientMetrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {
100100

101101
Long rpcClientRequestBodySize =
102102
RpcMessageBodySizeUtil.getRpcClientRequestBodySize(endAttributes, state.startAttributes());
103-
if (rpcClientRequestBodySize != null && rpcClientRequestBodySize > 0) {
103+
if (rpcClientRequestBodySize != null) {
104104
clientRequestSize.record(rpcClientRequestBodySize, attributes, context);
105105
}
106106

107107
Long rpcClientResponseBodySize =
108108
RpcMessageBodySizeUtil.getRpcClientResponseBodySize(endAttributes, state.startAttributes());
109-
if (rpcClientResponseBodySize != null && rpcClientResponseBodySize > 0) {
109+
if (rpcClientResponseBodySize != null) {
110110
clientResponseSize.record(rpcClientResponseBodySize, attributes, context);
111111
}
112112
}

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcCommonAttributesExtractor.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,24 @@ public final void onEnd(
4949
REQUEST request,
5050
@Nullable RESPONSE response,
5151
@Nullable Throwable error) {
52-
int clientRequestSize = getter.getClientRequestSize(request);
53-
if (clientRequestSize > 0) {
54-
internalSet(attributes, RPC_CLIENT_REQUEST_BODY_SIZE, (long) clientRequestSize);
55-
}
56-
int clientResponseSize = getter.getClientResponseSize(request);
57-
if (clientResponseSize > 0) {
58-
internalSet(attributes, RPC_CLIENT_RESPONSE_BODY_SIZE, (long) clientResponseSize);
59-
}
60-
61-
int serverRequestSize = getter.getServerRequestSize(request);
62-
if (serverRequestSize > 0) {
63-
internalSet(attributes, RPC_SERVER_REQUEST_BODY_SIZE, (long) serverRequestSize);
52+
Long requestSize = getter.getRequestSize(request);
53+
Long responseSize = getter.getResponseSize(request);
54+
if (this instanceof RpcClientAttributesExtractor) {
55+
if (requestSize != null) {
56+
internalSet(attributes, RPC_CLIENT_REQUEST_BODY_SIZE, requestSize);
57+
}
58+
if (responseSize != null) {
59+
internalSet(attributes, RPC_CLIENT_RESPONSE_BODY_SIZE, responseSize);
60+
}
6461
}
6562

66-
int serverResponseSize = getter.getServerResponseSize(request);
67-
if (serverResponseSize > 0) {
68-
internalSet(attributes, RPC_SERVER_RESPONSE_BODY_SIZE, (long) serverResponseSize);
63+
if (this instanceof RpcServerAttributesExtractor) {
64+
if (requestSize != null) {
65+
internalSet(attributes, RPC_SERVER_REQUEST_BODY_SIZE, requestSize);
66+
}
67+
if (responseSize != null) {
68+
internalSet(attributes, RPC_SERVER_RESPONSE_BODY_SIZE, responseSize);
69+
}
6970
}
7071
}
7172
}

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcMetricsAdvice.java

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,31 @@
1414
import io.opentelemetry.api.metrics.LongHistogramBuilder;
1515
import io.opentelemetry.semconv.NetworkAttributes;
1616
import io.opentelemetry.semconv.ServerAttributes;
17+
import java.util.List;
1718

1819
final class RpcMetricsAdvice {
1920

2021
// copied from RpcIncubatingAttributes
2122
private static final AttributeKey<Long> RPC_GRPC_STATUS_CODE =
2223
AttributeKey.longKey("rpc.grpc.status_code");
24+
private static final List<AttributeKey<?>> RPC_METRICS_ATTRIBUTE_KEYS =
25+
asList(
26+
RpcCommonAttributesExtractor.RPC_SYSTEM,
27+
RpcCommonAttributesExtractor.RPC_SERVICE,
28+
RpcCommonAttributesExtractor.RPC_METHOD,
29+
RPC_GRPC_STATUS_CODE,
30+
NetworkAttributes.NETWORK_TYPE,
31+
NetworkAttributes.NETWORK_TRANSPORT,
32+
ServerAttributes.SERVER_ADDRESS,
33+
ServerAttributes.SERVER_PORT);
2334

2435
static void applyClientDurationAdvice(DoubleHistogramBuilder builder) {
2536
if (!(builder instanceof ExtendedDoubleHistogramBuilder)) {
2637
return;
2738
}
2839
// the list of recommended metrics attributes is from
2940
// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-metrics.md
30-
((ExtendedDoubleHistogramBuilder) builder)
31-
.setAttributesAdvice(
32-
asList(
33-
RpcCommonAttributesExtractor.RPC_SYSTEM,
34-
RpcCommonAttributesExtractor.RPC_SERVICE,
35-
RpcCommonAttributesExtractor.RPC_METHOD,
36-
RPC_GRPC_STATUS_CODE,
37-
NetworkAttributes.NETWORK_TYPE,
38-
NetworkAttributes.NETWORK_TRANSPORT,
39-
ServerAttributes.SERVER_ADDRESS,
40-
ServerAttributes.SERVER_PORT));
41+
((ExtendedDoubleHistogramBuilder) builder).setAttributesAdvice(RPC_METRICS_ATTRIBUTE_KEYS);
4142
}
4243

4344
static void applyServerDurationAdvice(DoubleHistogramBuilder builder) {
@@ -46,17 +47,7 @@ static void applyServerDurationAdvice(DoubleHistogramBuilder builder) {
4647
}
4748
// the list of recommended metrics attributes is from
4849
// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-metrics.md
49-
((ExtendedDoubleHistogramBuilder) builder)
50-
.setAttributesAdvice(
51-
asList(
52-
RpcCommonAttributesExtractor.RPC_SYSTEM,
53-
RpcCommonAttributesExtractor.RPC_SERVICE,
54-
RpcCommonAttributesExtractor.RPC_METHOD,
55-
RPC_GRPC_STATUS_CODE,
56-
NetworkAttributes.NETWORK_TYPE,
57-
NetworkAttributes.NETWORK_TRANSPORT,
58-
ServerAttributes.SERVER_ADDRESS,
59-
ServerAttributes.SERVER_PORT));
50+
((ExtendedDoubleHistogramBuilder) builder).setAttributesAdvice(RPC_METRICS_ATTRIBUTE_KEYS);
6051
}
6152

6253
static void applyClientRequestSizeAdvice(LongHistogramBuilder builder) {
@@ -65,17 +56,7 @@ static void applyClientRequestSizeAdvice(LongHistogramBuilder builder) {
6556
}
6657
// the list of recommended metrics attributes is from
6758
// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-metrics.md
68-
((ExtendedLongHistogramBuilder) builder)
69-
.setAttributesAdvice(
70-
asList(
71-
RpcCommonAttributesExtractor.RPC_SYSTEM,
72-
RpcCommonAttributesExtractor.RPC_SERVICE,
73-
RpcCommonAttributesExtractor.RPC_METHOD,
74-
RPC_GRPC_STATUS_CODE,
75-
NetworkAttributes.NETWORK_TYPE,
76-
NetworkAttributes.NETWORK_TRANSPORT,
77-
ServerAttributes.SERVER_ADDRESS,
78-
ServerAttributes.SERVER_PORT));
59+
((ExtendedLongHistogramBuilder) builder).setAttributesAdvice(RPC_METRICS_ATTRIBUTE_KEYS);
7960
}
8061

8162
static void applyServerRequestSizeAdvice(LongHistogramBuilder builder) {
@@ -84,17 +65,7 @@ static void applyServerRequestSizeAdvice(LongHistogramBuilder builder) {
8465
}
8566
// the list of recommended metrics attributes is from
8667
// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-metrics.md
87-
((ExtendedLongHistogramBuilder) builder)
88-
.setAttributesAdvice(
89-
asList(
90-
RpcCommonAttributesExtractor.RPC_SYSTEM,
91-
RpcCommonAttributesExtractor.RPC_SERVICE,
92-
RpcCommonAttributesExtractor.RPC_METHOD,
93-
RPC_GRPC_STATUS_CODE,
94-
NetworkAttributes.NETWORK_TYPE,
95-
NetworkAttributes.NETWORK_TRANSPORT,
96-
ServerAttributes.SERVER_ADDRESS,
97-
ServerAttributes.SERVER_PORT));
68+
((ExtendedLongHistogramBuilder) builder).setAttributesAdvice(RPC_METRICS_ATTRIBUTE_KEYS);
9869
}
9970

10071
private RpcMetricsAdvice() {}

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcServerMetrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {
100100

101101
Long rpcServerRequestBodySize =
102102
RpcMessageBodySizeUtil.getRpcServerRequestBodySize(endAttributes, state.startAttributes());
103-
if (rpcServerRequestBodySize != null && rpcServerRequestBodySize > 0) {
103+
if (rpcServerRequestBodySize != null) {
104104
serverRequestSize.record(rpcServerRequestBodySize, attributes, context);
105105
}
106106

107107
Long rpcServerResponseBodySize =
108108
RpcMessageBodySizeUtil.getRpcServerResponseBodySize(endAttributes, state.startAttributes());
109-
if (rpcServerResponseBodySize != null && rpcServerResponseBodySize > 0) {
109+
if (rpcServerResponseBodySize != null) {
110110
serverResponseSize.record(rpcServerResponseBodySize, attributes, context);
111111
}
112112
}

instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcClientMetricsTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import io.opentelemetry.context.Context;
1717
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
1818
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
19-
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
2019
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
2120
import io.opentelemetry.semconv.NetworkAttributes;
2221
import io.opentelemetry.semconv.ServerAttributes;
@@ -80,7 +79,7 @@ void collectsMetrics() {
8079
assertThat(metricReader.collectAllMetrics())
8180
.satisfiesExactlyInAnyOrder(
8281
metric ->
83-
OpenTelemetryAssertions.assertThat(metric)
82+
assertThat(metric)
8483
.hasName("rpc.client.response.size")
8584
.hasUnit("By")
8685
.hasDescription("Measures the size of RPC response messages (uncompressed).")
@@ -108,7 +107,7 @@ void collectsMetrics() {
108107
.hasTraceId("ff01020304050600ff0a0b0c0d0e0f00")
109108
.hasSpanId("090a0b0c0d0e0f00")))),
110109
metric ->
111-
OpenTelemetryAssertions.assertThat(metric)
110+
assertThat(metric)
112111
.hasName("rpc.client.request.size")
113112
.hasUnit("By")
114113
.hasDescription("Measures the size of RPC request messages (uncompressed).")

instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcServerMetricsTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import io.opentelemetry.context.Context;
1717
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
1818
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
19-
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
2019
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
2120
import io.opentelemetry.semconv.NetworkAttributes;
2221
import io.opentelemetry.semconv.ServerAttributes;
@@ -108,7 +107,7 @@ void collectsMetrics() {
108107
.hasTraceId("ff01020304050600ff0a0b0c0d0e0f00")
109108
.hasSpanId("090a0b0c0d0e0f00")))),
110109
metric ->
111-
OpenTelemetryAssertions.assertThat(metric)
110+
assertThat(metric)
112111
.hasName("rpc.server.response.size")
113112
.hasUnit("By")
114113
.hasDescription("Measures the size of RPC response messages (uncompressed).")
@@ -135,7 +134,7 @@ void collectsMetrics() {
135134
.hasTraceId("ff01020304050600ff0a0b0c0d0e0f00")
136135
.hasSpanId("090a0b0c0d0e0f00")))),
137136
metric ->
138-
OpenTelemetryAssertions.assertThat(metric)
137+
assertThat(metric)
139138
.hasName("rpc.server.request.size")
140139
.hasUnit("By")
141140
.hasDescription("Measures the size of RPC request messages (uncompressed).")

instrumentation/grpc-1.6/library/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ val grpcVersion = "1.6.0"
77

88
dependencies {
99
library("io.grpc:grpc-core:$grpcVersion")
10+
library("io.grpc:grpc-protobuf:$grpcVersion")
1011

1112
testLibrary("io.grpc:grpc-netty:$grpcVersion")
12-
library("io.grpc:grpc-protobuf:$grpcVersion")
1313
testLibrary("io.grpc:grpc-services:$grpcVersion")
1414
testLibrary("io.grpc:grpc-stub:$grpcVersion")
1515

instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/BodySizeUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
final class BodySizeUtil {
1111

12-
static <T> int getBodySize(T message) {
12+
static <T> Long getBodySize(T message) {
1313
if (message instanceof MessageLite) {
14-
return ((MessageLite) message).getSerializedSize();
14+
return (long) ((MessageLite) message).getSerializedSize();
1515
} else {
1616
// Message is not a protobuf message
17-
return 0;
17+
return null;
1818
}
1919
}
2020

instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequest.java

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ public final class GrpcRequest {
2020
private volatile int logicalPort = -1;
2121
@Nullable private volatile SocketAddress peerSocketAddress;
2222

23-
private int clientRequestSize;
24-
private int clientResponseSize;
25-
26-
private int serverRequestSize;
27-
private int serverResponseSize;
23+
private Long requestSize;
24+
private Long responseSize;
2825

2926
GrpcRequest(
3027
MethodDescriptor<?, ?> method,
@@ -85,35 +82,19 @@ void setPeerSocketAddress(SocketAddress peerSocketAddress) {
8582
this.peerSocketAddress = peerSocketAddress;
8683
}
8784

88-
public int getClientRequestSize() {
89-
return clientRequestSize;
90-
}
91-
92-
void setClientRequestSize(int clientRequestSize) {
93-
this.clientRequestSize = clientRequestSize;
94-
}
95-
96-
public int getClientResponseSize() {
97-
return clientResponseSize;
98-
}
99-
100-
void setClientResponseSize(int clientResponseSize) {
101-
this.clientResponseSize = clientResponseSize;
102-
}
103-
104-
public int getServerRequestSize() {
105-
return serverRequestSize;
85+
public Long getRequestSize() {
86+
return requestSize;
10687
}
10788

108-
public void setServerRequestSize(int serverRequestSize) {
109-
this.serverRequestSize = serverRequestSize;
89+
public void setRequestSize(Long requestSize) {
90+
this.requestSize = requestSize;
11091
}
11192

112-
public int getServerResponseSize() {
113-
return serverResponseSize;
93+
public Long getResponseSize() {
94+
return responseSize;
11495
}
11596

116-
public void setServerResponseSize(int serverResponseSize) {
117-
this.serverResponseSize = serverResponseSize;
97+
public void setResponseSize(Long responseSize) {
98+
this.responseSize = responseSize;
11899
}
119100
}

0 commit comments

Comments
 (0)