Skip to content

Commit 56c20f2

Browse files
authored
Support AppSignals metrics transport protocol configuration (#752)
* Support AppSignals metrics transport protocol configuration * rename SMP to AppSignals * rename Span Metrics to AppSignals * run gradlew :awsagentprovider:spotlessApply
1 parent 34a2f17 commit 56c20f2

File tree

1 file changed

+51
-23
lines changed

1 file changed

+51
-23
lines changed

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/AwsAppSignalsCustomizerProvider.java

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
import io.opentelemetry.api.metrics.MeterProvider;
1919
import io.opentelemetry.contrib.awsxray.AlwaysRecordSampler;
2020
import io.opentelemetry.contrib.awsxray.ResourceHolder;
21+
import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter;
22+
import io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil;
2123
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
2224
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
2325
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
2426
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
27+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
2528
import io.opentelemetry.sdk.metrics.Aggregation;
2629
import io.opentelemetry.sdk.metrics.InstrumentType;
2730
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
@@ -63,30 +66,26 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
6366
autoConfiguration.addSpanExporterCustomizer(this::customizeSpanExporter);
6467
}
6568

66-
private boolean isSmpEnabled(ConfigProperties configProps) {
69+
private boolean isAppSignalsEnabled(ConfigProperties configProps) {
6770
return configProps.getBoolean(
6871
"otel.aws.app.signals.enabled", configProps.getBoolean("otel.smp.enabled", false));
6972
}
7073

7174
private Sampler customizeSampler(Sampler sampler, ConfigProperties configProps) {
72-
if (isSmpEnabled(configProps)) {
75+
if (isAppSignalsEnabled(configProps)) {
7376
return AlwaysRecordSampler.create(sampler);
7477
}
7578
return sampler;
7679
}
7780

7881
private SdkTracerProviderBuilder customizeTracerProviderBuilder(
7982
SdkTracerProviderBuilder tracerProviderBuilder, ConfigProperties configProps) {
80-
if (isSmpEnabled(configProps)) {
81-
logger.info("Span Metrics Processor enabled");
82-
String smpEndpoint =
83-
configProps.getString(
84-
"otel.aws.app.signals.exporter.endpoint",
85-
configProps.getString("otel.aws.smp.exporter.endpoint", "http://localhost:4315"));
83+
if (isAppSignalsEnabled(configProps)) {
84+
logger.info("AWS AppSignals enabled");
8685
Duration exportInterval =
8786
configProps.getDuration("otel.metric.export.interval", DEFAULT_METRIC_EXPORT_INTERVAL);
88-
logger.log(Level.FINE, String.format("Span Metrics endpoint: %s", smpEndpoint));
89-
logger.log(Level.FINE, String.format("Span Metrics export interval: %s", exportInterval));
87+
logger.log(
88+
Level.FINE, String.format("AppSignals Metrics export interval: %s", exportInterval));
9089
// Cap export interval to 60 seconds. This is currently required for metrics-trace correlation
9190
// to work correctly.
9291
if (exportInterval.compareTo(DEFAULT_METRIC_EXPORT_INTERVAL) > 0) {
@@ -100,17 +99,8 @@ private SdkTracerProviderBuilder customizeTracerProviderBuilder(
10099
AttributePropagatingSpanProcessorBuilder.create().build());
101100
// Construct meterProvider
102101
MetricExporter metricsExporter =
103-
OtlpGrpcMetricExporter.builder()
104-
.setEndpoint(smpEndpoint)
105-
.setDefaultAggregationSelector(
106-
instrumentType -> {
107-
if (instrumentType == InstrumentType.HISTOGRAM) {
108-
return Aggregation.base2ExponentialBucketHistogram();
109-
}
110-
return Aggregation.defaultAggregation();
111-
})
112-
.setAggregationTemporalitySelector(AggregationTemporalitySelector.deltaPreferred())
113-
.build();
102+
AppSignalsExporterProvider.INSTANCE.createExporter(configProps);
103+
114104
MetricReader metricReader =
115105
PeriodicMetricReader.builder(metricsExporter).setInterval(exportInterval).build();
116106

@@ -119,7 +109,7 @@ private SdkTracerProviderBuilder customizeTracerProviderBuilder(
119109
.setResource(ResourceHolder.getResource())
120110
.registerMetricReader(metricReader)
121111
.build();
122-
// Construct and set span metrics processor
112+
// Construct and set AppSignals metrics processor
123113
SpanProcessor spanMetricsProcessor =
124114
AwsSpanMetricsProcessorBuilder.create(meterProvider, ResourceHolder.getResource())
125115
.build();
@@ -130,12 +120,50 @@ private SdkTracerProviderBuilder customizeTracerProviderBuilder(
130120

131121
private SpanExporter customizeSpanExporter(
132122
SpanExporter spanExporter, ConfigProperties configProps) {
133-
if (isSmpEnabled(configProps)) {
123+
if (isAppSignalsEnabled(configProps)) {
134124
return AwsMetricAttributesSpanExporterBuilder.create(
135125
spanExporter, ResourceHolder.getResource())
136126
.build();
137127
}
138128

139129
return spanExporter;
140130
}
131+
132+
private enum AppSignalsExporterProvider {
133+
INSTANCE;
134+
135+
public MetricExporter createExporter(ConfigProperties configProps) {
136+
String protocol =
137+
OtlpConfigUtil.getOtlpProtocol(OtlpConfigUtil.DATA_TYPE_METRICS, configProps);
138+
logger.log(Level.FINE, String.format("AppSignals export protocol: %s", protocol));
139+
140+
String appSignalsEndpoint =
141+
configProps.getString(
142+
"otel.aws.app.signals.exporter.endpoint",
143+
configProps.getString("otel.aws.smp.exporter.endpoint", "http://localhost:4315"));
144+
logger.log(Level.FINE, String.format("AppSignals export endpoint: %s", appSignalsEndpoint));
145+
146+
if (protocol.equals(OtlpConfigUtil.PROTOCOL_HTTP_PROTOBUF)) {
147+
return OtlpHttpMetricExporter.builder()
148+
.setEndpoint(appSignalsEndpoint)
149+
.setDefaultAggregationSelector(this::getAggregation)
150+
.setAggregationTemporalitySelector(AggregationTemporalitySelector.deltaPreferred())
151+
.build();
152+
} else if (protocol.equals(OtlpConfigUtil.PROTOCOL_GRPC)) {
153+
return OtlpGrpcMetricExporter.builder()
154+
.setEndpoint(appSignalsEndpoint)
155+
.setDefaultAggregationSelector(this::getAggregation)
156+
.setAggregationTemporalitySelector(AggregationTemporalitySelector.deltaPreferred())
157+
.build();
158+
}
159+
throw new ConfigurationException("Unsupported AppSignals export protocol: " + protocol);
160+
}
161+
162+
private Aggregation getAggregation(InstrumentType instrumentType) {
163+
if (instrumentType == InstrumentType.HISTOGRAM) {
164+
return Aggregation.base2ExponentialBucketHistogram();
165+
}
166+
return Aggregation.defaultAggregation();
167+
}
168+
}
141169
}

0 commit comments

Comments
 (0)