Skip to content

Commit 68967e7

Browse files
committed
Move attribute references to instrumentation api
1 parent 7720b8d commit 68967e7

File tree

7 files changed

+64
-45
lines changed

7 files changed

+64
-45
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import io.opentelemetry.sdk.common.CompletableResultCode;
1717
import io.opentelemetry.sdk.common.InternalTelemetrySchemaVersion;
1818
import io.opentelemetry.sdk.internal.ComponentId;
19-
import io.opentelemetry.sdk.internal.SemConvAttributes;
2019
import io.opentelemetry.sdk.internal.ThrottlingLogger;
2120
import java.util.concurrent.atomic.AtomicBoolean;
2221
import java.util.function.Supplier;
@@ -86,15 +85,15 @@ private void onResponse(
8685
GrpcResponse grpcResponse) {
8786
int statusCode = grpcResponse.grpcStatusValue();
8887

89-
Attributes requestAttributes = Attributes.of(SemConvAttributes.RPC_GRPC_STATUS_CODE, (long) statusCode);
88+
metricRecording.setGrpcStatusCode(statusCode);
9089

9190
if (statusCode == 0) {
92-
metricRecording.finishSuccessful(requestAttributes);
91+
metricRecording.finishSuccessful();
9392
result.succeed();
9493
return;
9594
}
9695

97-
metricRecording.finishFailed(String.valueOf(statusCode), requestAttributes);
96+
metricRecording.finishFailed(String.valueOf(statusCode));
9897
switch (statusCode) {
9998
case GRPC_STATUS_UNIMPLEMENTED:
10099
if (loggedUnimplemented.compareAndSet(false, true)) {
@@ -130,7 +129,7 @@ private void onError(
130129
CompletableResultCode result,
131130
ExporterInstrumentation.Recording metricRecording,
132131
Throwable e) {
133-
metricRecording.finishFailed(e, Attributes.empty());
132+
metricRecording.finishFailed(e);
134133
logger.log(
135134
Level.SEVERE,
136135
"Failed to export "

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import io.opentelemetry.sdk.common.CompletableResultCode;
1515
import io.opentelemetry.sdk.common.InternalTelemetrySchemaVersion;
1616
import io.opentelemetry.sdk.internal.ComponentId;
17-
import io.opentelemetry.sdk.internal.SemConvAttributes;
1817
import io.opentelemetry.sdk.internal.ThrottlingLogger;
1918
import java.io.IOException;
2019
import java.util.concurrent.atomic.AtomicBoolean;
@@ -84,15 +83,15 @@ private void onResponse(
8483
HttpSender.Response httpResponse) {
8584
int statusCode = httpResponse.statusCode();
8685

87-
Attributes requestAttributes = Attributes.of(SemConvAttributes.HTTP_RESPONSE_STATUS_CODE, (long) statusCode);
86+
metricRecording.setHttpStatusCode(statusCode);
8887

8988
if (statusCode >= 200 && statusCode < 300) {
90-
metricRecording.finishSuccessful(requestAttributes);
89+
metricRecording.finishSuccessful();
9190
result.succeed();
9291
return;
9392
}
9493

95-
metricRecording.finishFailed(String.valueOf(statusCode), requestAttributes);
94+
metricRecording.finishFailed(String.valueOf(statusCode));
9695

9796
byte[] body = null;
9897
try {
@@ -119,7 +118,7 @@ private void onError(
119118
CompletableResultCode result,
120119
ExporterInstrumentation.Recording metricRecording,
121120
Throwable e) {
122-
metricRecording.finishFailed(e, Attributes.empty());
121+
metricRecording.finishFailed(e);
123122
logger.log(
124123
Level.SEVERE,
125124
"Failed to export "

exporters/common/src/main/java/io/opentelemetry/exporter/internal/metrics/ExporterInstrumentation.java

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import io.opentelemetry.api.metrics.MeterProvider;
1010
import io.opentelemetry.sdk.common.InternalTelemetrySchemaVersion;
1111
import io.opentelemetry.sdk.internal.ComponentId;
12+
import io.opentelemetry.sdk.internal.SemConvAttributes;
1213
import java.util.function.Supplier;
1314
import javax.annotation.Nullable;
1415

1516
/**
1617
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
1718
* any time.
1819
*/
19-
public class ExporterInstrumentation implements ExporterMetrics {
20+
public class ExporterInstrumentation {
2021

2122
private final ExporterMetrics implementation;
2223

@@ -51,7 +52,6 @@ public ExporterInstrumentation(
5152
}
5253
}
5354

54-
@Override
5555
public Recording startRecordingExport(int itemCount) {
5656
return new Recording(implementation.startRecordingExport(itemCount));
5757
}
@@ -60,28 +60,64 @@ public Recording startRecordingExport(int itemCount) {
6060
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
6161
* any time.
6262
*/
63-
public static class Recording extends ExporterMetrics.Recording {
63+
public static class Recording {
6464

6565
private final ExporterMetrics.Recording delegate;
66+
@Nullable private Long httpStatusCode;
67+
@Nullable private Long grpcStatusCode;
6668

6769
private Recording(ExporterMetrics.Recording delegate) {
6870
this.delegate = delegate;
6971
}
7072

73+
public void setHttpStatusCode(long httpStatusCode) {
74+
if (grpcStatusCode != null) {
75+
throw new IllegalStateException(
76+
"gRPC status code already set, can only set either gRPC or HTTP");
77+
}
78+
this.httpStatusCode = httpStatusCode;
79+
}
80+
81+
public void setGrpcStatusCode(long grpcStatusCode) {
82+
if (httpStatusCode != null) {
83+
throw new IllegalStateException(
84+
"HTTP status code already set, can only set either gRPC or HTTP");
85+
}
86+
this.grpcStatusCode = grpcStatusCode;
87+
}
88+
89+
/** Callback to notify that the export was successful. */
90+
public void finishSuccessful() {
91+
delegate.finishSuccessful(buildRequestAttributes());
92+
}
93+
7194
/**
7295
* Callback to notify that the export has failed with the given {@link Throwable} as failure
7396
* cause.
7497
*
7598
* @param failureCause the cause of the failure
76-
* @param requestAttributes additional attributes to add to request metrics
7799
*/
78-
public final void finishFailed(Throwable failureCause, Attributes requestAttributes) {
79-
finishFailed(failureCause.getClass().getName(), requestAttributes);
100+
public void finishFailed(Throwable failureCause) {
101+
finishFailed(failureCause.getClass().getName());
102+
}
103+
104+
/**
105+
* Callback to notify that the export has failed.
106+
*
107+
* @param errorType a failure reason suitable for the error.type attribute
108+
*/
109+
public void finishFailed(String errorType) {
110+
delegate.finishFailed(errorType, buildRequestAttributes());
80111
}
81112

82-
@Override
83-
protected void doFinish(@Nullable String errorType, Attributes requestAttributes) {
84-
delegate.doFinish(errorType, requestAttributes);
113+
private Attributes buildRequestAttributes() {
114+
if (httpStatusCode != null) {
115+
return Attributes.of(SemConvAttributes.HTTP_RESPONSE_STATUS_CODE, httpStatusCode);
116+
}
117+
if (grpcStatusCode != null) {
118+
return Attributes.of(SemConvAttributes.RPC_GRPC_STATUS_CODE, grpcStatusCode);
119+
}
120+
return Attributes.empty();
85121
}
86122
}
87123
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,11 @@ abstract class Recording {
2626

2727
protected Recording() {}
2828

29-
/**
30-
* Callback to notify that the export was successful.
31-
*
32-
* @param requestAttributes additional attributes to add to request metrics
33-
*/
3429
public final void finishSuccessful(Attributes requestAttributes) {
3530
ensureEndedOnce();
3631
doFinish(null, requestAttributes);
3732
}
3833

39-
/**
40-
* Callback to notify that the export has failed.
41-
*
42-
* @param errorType a failure reason suitable for the error.type attribute
43-
* @param requestAttributes additional attributes to add to request metrics
44-
*/
4534
public final void finishFailed(String errorType, Attributes requestAttributes) {
4635
ensureEndedOnce();
4736
if (errorType == null || errorType.isEmpty()) {

exporters/common/src/main/java/io/opentelemetry/exporter/internal/metrics/NoopExporterMetrics.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ class NoopExporterMetrics implements ExporterMetrics {
1414

1515
@Override
1616
public Recording startRecordingExport(int itemCount) {
17-
return NoopRecording.INSTANCE;
17+
return new NoopRecording();
1818
}
1919

2020
private static class NoopRecording extends Recording {
2121

22-
private static final NoopRecording INSTANCE = new NoopRecording();
23-
2422
@Override
2523
protected void doFinish(@Nullable String errorType, Attributes requestAttributes) {}
2624
}

exporters/common/src/test/java/io/opentelemetry/exporter/internal/metrics/ExporterInstrumentationTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import static org.mockito.Mockito.verifyNoMoreInteractions;
1313
import static org.mockito.Mockito.when;
1414

15-
import io.opentelemetry.api.common.Attributes;
1615
import io.opentelemetry.api.metrics.MeterProvider;
1716
import io.opentelemetry.sdk.common.CompletableResultCode;
1817
import io.opentelemetry.sdk.common.InternalTelemetrySchemaVersion;
@@ -72,16 +71,16 @@ public AggregationTemporality getAggregationTemporality(
7271

7372
// Verify the supplier is only called once per underlying meter.
7473

75-
instrumentation.startRecordingExport(42).finishFailed("foo", Attributes.empty());
76-
instrumentation.startRecordingExport(42).finishSuccessful(Attributes.empty());
74+
instrumentation.startRecordingExport(42).finishFailed("foo");
75+
instrumentation.startRecordingExport(42).finishSuccessful();
7776
if (schemaVersion == InternalTelemetrySchemaVersion.DISABLED) {
7877
verifyNoInteractions(meterProviderSupplier);
7978
} else {
8079
verify(meterProviderSupplier, atLeastOnce()).get();
8180
}
8281

83-
instrumentation.startRecordingExport(42).finishFailed("foo", Attributes.empty());
84-
instrumentation.startRecordingExport(42).finishSuccessful(Attributes.empty());
82+
instrumentation.startRecordingExport(42).finishFailed("foo");
83+
instrumentation.startRecordingExport(42).finishSuccessful();
8584
verifyNoMoreInteractions(meterProviderSupplier);
8685
}
8786

@@ -103,13 +102,13 @@ void noopMeterProvider(InternalTelemetrySchemaVersion schemaVersion) {
103102
verifyNoInteractions(meterProviderSupplier); // Ensure lazy
104103

105104
// Verify the supplier is invoked multiple times since it returns a noop meter.
106-
instrumentation.startRecordingExport(42).finishFailed("foo", Attributes.empty());
107-
instrumentation.startRecordingExport(42).finishSuccessful(Attributes.empty());
105+
instrumentation.startRecordingExport(42).finishFailed("foo");
106+
instrumentation.startRecordingExport(42).finishSuccessful();
108107
verify(meterProviderSupplier, atLeastOnce()).get();
109108

110109
Mockito.clearInvocations((Object) meterProviderSupplier);
111-
instrumentation.startRecordingExport(42).finishFailed("foo", Attributes.empty());
112-
instrumentation.startRecordingExport(42).finishSuccessful(Attributes.empty());
110+
instrumentation.startRecordingExport(42).finishFailed("foo");
111+
instrumentation.startRecordingExport(42).finishSuccessful();
113112
verify(meterProviderSupplier, atLeastOnce()).get();
114113
}
115114
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public final class ZipkinSpanExporter implements SpanExporter {
6767
} else {
6868
exporterType = ComponentId.StandardExporterType.ZIPKIN_HTTP_SPAN_EXPORTER;
6969
}
70-
// TODO: add server address and port attributes
7170
this.exporterMetrics =
7271
new ExporterInstrumentation(
7372
internalTelemetrySchemaVersion,
@@ -98,10 +97,10 @@ public CompletableResultCode export(Collection<SpanData> spanDataList) {
9897
() -> {
9998
try {
10099
sender.send(encodedSpans);
101-
metricRecording.finishSuccessful(Attributes.empty());
100+
metricRecording.finishSuccessful();
102101
resultCode.succeed();
103102
} catch (IOException | RuntimeException e) {
104-
metricRecording.finishFailed(e, Attributes.empty());
103+
metricRecording.finishFailed(e);
105104
logger.log(Level.WARNING, "Failed to export spans", e);
106105
resultCode.fail();
107106
}

0 commit comments

Comments
 (0)