Skip to content

Commit 676b66c

Browse files
committed
Implement status code attributes
1 parent 9adacd0 commit 676b66c

File tree

8 files changed

+67
-43
lines changed

8 files changed

+67
-43
lines changed

exporters/common/src/main/java/io/opentelemetry/exporter/internal/ExporterMetrics.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,29 @@ private void incrementExported(long count, @Nullable String errorType) {
189189
if (!enabled) {
190190
return;
191191
}
192-
exported().add(count, getAttributesWithPotentialError(errorType));
192+
exported().add(count, getAttributesWithPotentialError(errorType, Attributes.empty()));
193193
}
194194

195-
private Attributes getAttributesWithPotentialError(@Nullable String errorType) {
195+
private Attributes getAttributesWithPotentialError(
196+
@Nullable String errorType, Attributes additionalAttributes) {
196197
Attributes attributes = allAttributes();
197-
if (errorType != null && !errorType.isEmpty()) {
198-
attributes = attributes.toBuilder().put(SemConvAttributes.ERROR_TYPE, errorType).build();
198+
boolean errorPresent = errorType != null && !errorType.isEmpty();
199+
if (errorPresent || !additionalAttributes.isEmpty()) {
200+
AttributesBuilder builder = attributes.toBuilder();
201+
if (errorPresent) {
202+
builder.put(SemConvAttributes.ERROR_TYPE, errorType);
203+
}
204+
attributes = builder.putAll(additionalAttributes).build();
199205
}
200206
return attributes;
201207
}
202208

203-
private void recordDuration(double seconds, @Nullable String errorType) {
209+
private void recordDuration(
210+
double seconds, @Nullable String errorType, Attributes requestAttributes) {
204211
if (!enabled) {
205212
return;
206213
}
207-
duration().record(seconds, getAttributesWithPotentialError(errorType));
214+
duration().record(seconds, getAttributesWithPotentialError(errorType, requestAttributes));
208215
}
209216

210217
/**
@@ -225,19 +232,15 @@ private Recording(int itemCount) {
225232
incrementInflight(itemCount);
226233
}
227234

228-
public void finishSuccessful() {
229-
finish(0, null);
230-
}
231-
232-
public void finishPartialSuccess(int rejectedCount) {
233-
finish(rejectedCount, "rejected");
235+
public void finishSuccessful(Attributes requestAttributes) {
236+
finish(0, null, requestAttributes);
234237
}
235238

236-
public void finishFailed(String errorReason) {
237-
finish(itemCount, errorReason);
239+
public void finishFailed(String errorReason, Attributes requestAttributes) {
240+
finish(itemCount, errorReason, requestAttributes);
238241
}
239242

240-
private void finish(int failedCount, @Nullable String errorType) {
243+
private void finish(int failedCount, @Nullable String errorType, Attributes requestAttributes) {
241244
if (alreadyEnded) {
242245
throw new IllegalStateException("Recording already ended");
243246
}
@@ -257,7 +260,7 @@ private void finish(int failedCount, @Nullable String errorType) {
257260
incrementExported(successCount, null);
258261
}
259262
long durationNanos = CLOCK.nanoTime() - startNanoTime;
260-
recordDuration(durationNanos / 1_000_000_000.0, errorType);
263+
recordDuration(durationNanos / 1_000_000_000.0, errorType, requestAttributes);
261264
}
262265
}
263266
}

exporters/common/src/main/java/io/opentelemetry/exporter/internal/ExporterMetricsAdapter.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,23 @@ private Recording(int itemCount) {
8888
}
8989
}
9090

91-
public void finishSuccessful() {
91+
public void finishSuccessful(Attributes operationAttributes) {
9292
if (delegate == null) {
9393
finishLegacy(0);
9494
} else {
95-
delegate.finishSuccessful();
95+
delegate.finishSuccessful(operationAttributes);
9696
}
9797
}
9898

99-
public void finishPartialSuccess(int rejectedCount) {
100-
if (delegate == null) {
101-
finishLegacy(rejectedCount);
102-
} else {
103-
delegate.finishPartialSuccess(rejectedCount);
104-
}
105-
}
106-
107-
public void finishFailed(Throwable e) {
108-
// TODO: check in java instrumentation if this is correct
109-
finishFailed(e.getClass().getName());
99+
public void finishFailed(Throwable e, Attributes operationAttributes) {
100+
finishFailed(e.getClass().getName(), operationAttributes);
110101
}
111102

112-
public void finishFailed(String errorReason) {
103+
public void finishFailed(String errorReason, Attributes requestAttributes) {
113104
if (delegate == null) {
114105
finishLegacy(itemCount);
115106
} else {
116-
delegate.finishFailed(errorReason);
107+
delegate.finishFailed(errorReason, requestAttributes);
117108
}
118109
}
119110

exporters/common/src/main/java/io/opentelemetry/exporter/internal/grpc/GrpcExporter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.opentelemetry.sdk.common.CompletableResultCode;
1818
import io.opentelemetry.sdk.common.HealthMetricLevel;
1919
import io.opentelemetry.sdk.internal.ComponentId;
20+
import io.opentelemetry.sdk.internal.SemConvAttributes;
2021
import io.opentelemetry.sdk.internal.ThrottlingLogger;
2122
import java.util.concurrent.atomic.AtomicBoolean;
2223
import java.util.function.Supplier;
@@ -89,13 +90,16 @@ private void onResponse(
8990
GrpcResponse grpcResponse) {
9091
int statusCode = grpcResponse.grpcStatusValue();
9192

93+
Attributes requestAttributes =
94+
Attributes.builder().put(SemConvAttributes.RPC_GRPC_STATUS_CODE, statusCode).build();
95+
9296
if (statusCode == 0) {
93-
metricRecording.finishSuccessful();
97+
metricRecording.finishSuccessful(requestAttributes);
9498
result.succeed();
9599
return;
96100
}
97101

98-
metricRecording.finishFailed("" + statusCode);
102+
metricRecording.finishFailed("" + statusCode, requestAttributes);
99103
switch (statusCode) {
100104
case GRPC_STATUS_UNIMPLEMENTED:
101105
if (loggedUnimplemented.compareAndSet(false, true)) {
@@ -129,7 +133,7 @@ private void onResponse(
129133

130134
private void onError(
131135
CompletableResultCode result, ExporterMetricsAdapter.Recording metricRecording, Throwable e) {
132-
metricRecording.finishFailed(e);
136+
metricRecording.finishFailed(e, Attributes.empty());
133137
logger.log(
134138
Level.SEVERE,
135139
"Failed to export "

exporters/common/src/main/java/io/opentelemetry/exporter/internal/http/HttpExporter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.opentelemetry.sdk.common.CompletableResultCode;
1616
import io.opentelemetry.sdk.common.HealthMetricLevel;
1717
import io.opentelemetry.sdk.internal.ComponentId;
18+
import io.opentelemetry.sdk.internal.SemConvAttributes;
1819
import io.opentelemetry.sdk.internal.ThrottlingLogger;
1920
import java.io.IOException;
2021
import java.util.concurrent.atomic.AtomicBoolean;
@@ -89,13 +90,16 @@ private void onResponse(
8990
HttpSender.Response httpResponse) {
9091
int statusCode = httpResponse.statusCode();
9192

93+
Attributes requestAttributes =
94+
Attributes.builder().put(SemConvAttributes.HTTP_RESPONSE_STATUS_CODE, statusCode).build();
95+
9296
if (statusCode >= 200 && statusCode < 300) {
93-
metricRecording.finishSuccessful();
97+
metricRecording.finishSuccessful(requestAttributes);
9498
result.succeed();
9599
return;
96100
}
97101

98-
metricRecording.finishFailed("" + statusCode);
102+
metricRecording.finishFailed("" + statusCode, requestAttributes);
99103

100104
byte[] body = null;
101105
try {
@@ -120,7 +124,7 @@ private void onResponse(
120124

121125
private void onError(
122126
CompletableResultCode result, ExporterMetricsAdapter.Recording metricRecording, Throwable e) {
123-
metricRecording.finishFailed(e);
127+
metricRecording.finishFailed(e, Attributes.empty());
124128
logger.log(
125129
Level.SEVERE,
126130
"Failed to export "

exporters/common/src/test/java/io/opentelemetry/exporter/internal/grpc/GrpcExporterTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,21 @@ void testHealthMetrics(ExporterMetrics.Signal signal) {
189189
.hasHistogramSatisfying(
190190
ma ->
191191
ma.hasPointsSatisfying(
192-
pa -> pa.hasAttributes(expectedAttributes).hasBucketCounts(1),
192+
pa ->
193+
pa.hasAttributes(
194+
expectedAttributes.toBuilder()
195+
.put(SemConvAttributes.RPC_GRPC_STATUS_CODE, 0)
196+
.build())
197+
.hasBucketCounts(1),
193198
pa ->
194199
pa.hasAttributes(
195200
expectedAttributes.toBuilder()
196201
.put(
197202
SemConvAttributes.ERROR_TYPE,
198203
"" + GrpcExporterUtil.GRPC_STATUS_UNAVAILABLE)
204+
.put(
205+
SemConvAttributes.RPC_GRPC_STATUS_CODE,
206+
GrpcExporterUtil.GRPC_STATUS_UNAVAILABLE)
199207
.build())
200208
.hasBucketCounts(1),
201209
pa ->

exporters/common/src/test/java/io/opentelemetry/exporter/internal/http/HttpExporterTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,21 @@ void testHealthMetrics(ExporterMetrics.Signal signal) {
179179
.hasHistogramSatisfying(
180180
ma ->
181181
ma.hasPointsSatisfying(
182-
pa -> pa.hasAttributes(expectedAttributes).hasBucketCounts(1),
182+
pa ->
183+
pa.hasAttributes(
184+
expectedAttributes.toBuilder()
185+
.put(
186+
SemConvAttributes.HTTP_RESPONSE_STATUS_CODE,
187+
200)
188+
.build())
189+
.hasBucketCounts(1),
183190
pa ->
184191
pa.hasAttributes(
185192
expectedAttributes.toBuilder()
186193
.put(SemConvAttributes.ERROR_TYPE, "404")
194+
.put(
195+
SemConvAttributes.HTTP_RESPONSE_STATUS_CODE,
196+
404)
187197
.build())
188198
.hasBucketCounts(1),
189199
pa ->

exporters/zipkin/src/main/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ public CompletableResultCode export(Collection<SpanData> spanDataList) {
104104
() -> {
105105
try {
106106
sender.send(encodedSpans);
107-
metricRecording.finishSuccessful();
107+
metricRecording.finishSuccessful(Attributes.empty());
108108
resultCode.succeed();
109109
} catch (IOException | RuntimeException e) {
110-
metricRecording.finishFailed(e);
110+
metricRecording.finishFailed(e, Attributes.empty());
111111
logger.log(Level.WARNING, "Failed to export spans", e);
112112
resultCode.fail();
113113
}

sdk/common/src/main/java/io/opentelemetry/sdk/internal/SemConvAttributes.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ public class SemConvAttributes {
1919

2020
private SemConvAttributes() {}
2121

22-
// TODO: add tests against semconv
22+
// TODO: add test all attributes against semconv dependency
2323
public static final AttributeKey<String> OTEL_COMPONENT_TYPE =
2424
AttributeKey.stringKey("otel.component.type");
2525
public static final AttributeKey<String> OTEL_COMPONENT_NAME =
2626
AttributeKey.stringKey("otel.component.name");
27-
// TODO: add semconv test
2827
public static final AttributeKey<String> ERROR_TYPE = AttributeKey.stringKey("error.type");
2928

3029
public static final AttributeKey<String> SERVER_ADDRESS =
3130
AttributeKey.stringKey("server.address");
3231
public static final AttributeKey<Long> SERVER_PORT = AttributeKey.longKey("server.port");
32+
33+
public static final AttributeKey<Long> RPC_GRPC_STATUS_CODE =
34+
AttributeKey.longKey("rpc.grpc.status_code");
35+
public static final AttributeKey<Long> HTTP_RESPONSE_STATUS_CODE =
36+
AttributeKey.longKey("http.response.status_code");
3337
}

0 commit comments

Comments
 (0)