18
18
import io .opentelemetry .api .metrics .MeterProvider ;
19
19
import io .opentelemetry .contrib .awsxray .AlwaysRecordSampler ;
20
20
import io .opentelemetry .contrib .awsxray .ResourceHolder ;
21
+ import io .opentelemetry .exporter .otlp .http .metrics .OtlpHttpMetricExporter ;
22
+ import io .opentelemetry .exporter .otlp .internal .OtlpConfigUtil ;
21
23
import io .opentelemetry .exporter .otlp .metrics .OtlpGrpcMetricExporter ;
22
24
import io .opentelemetry .sdk .autoconfigure .spi .AutoConfigurationCustomizer ;
23
25
import io .opentelemetry .sdk .autoconfigure .spi .AutoConfigurationCustomizerProvider ;
24
26
import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
27
+ import io .opentelemetry .sdk .autoconfigure .spi .ConfigurationException ;
25
28
import io .opentelemetry .sdk .metrics .Aggregation ;
26
29
import io .opentelemetry .sdk .metrics .InstrumentType ;
27
30
import io .opentelemetry .sdk .metrics .SdkMeterProvider ;
@@ -63,30 +66,26 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
63
66
autoConfiguration .addSpanExporterCustomizer (this ::customizeSpanExporter );
64
67
}
65
68
66
- private boolean isSmpEnabled (ConfigProperties configProps ) {
69
+ private boolean isAppSignalsEnabled (ConfigProperties configProps ) {
67
70
return configProps .getBoolean (
68
71
"otel.aws.app.signals.enabled" , configProps .getBoolean ("otel.smp.enabled" , false ));
69
72
}
70
73
71
74
private Sampler customizeSampler (Sampler sampler , ConfigProperties configProps ) {
72
- if (isSmpEnabled (configProps )) {
75
+ if (isAppSignalsEnabled (configProps )) {
73
76
return AlwaysRecordSampler .create (sampler );
74
77
}
75
78
return sampler ;
76
79
}
77
80
78
81
private SdkTracerProviderBuilder customizeTracerProviderBuilder (
79
82
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" );
86
85
Duration exportInterval =
87
86
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 ));
90
89
// Cap export interval to 60 seconds. This is currently required for metrics-trace correlation
91
90
// to work correctly.
92
91
if (exportInterval .compareTo (DEFAULT_METRIC_EXPORT_INTERVAL ) > 0 ) {
@@ -100,17 +99,8 @@ private SdkTracerProviderBuilder customizeTracerProviderBuilder(
100
99
AttributePropagatingSpanProcessorBuilder .create ().build ());
101
100
// Construct meterProvider
102
101
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
+
114
104
MetricReader metricReader =
115
105
PeriodicMetricReader .builder (metricsExporter ).setInterval (exportInterval ).build ();
116
106
@@ -119,7 +109,7 @@ private SdkTracerProviderBuilder customizeTracerProviderBuilder(
119
109
.setResource (ResourceHolder .getResource ())
120
110
.registerMetricReader (metricReader )
121
111
.build ();
122
- // Construct and set span metrics processor
112
+ // Construct and set AppSignals metrics processor
123
113
SpanProcessor spanMetricsProcessor =
124
114
AwsSpanMetricsProcessorBuilder .create (meterProvider , ResourceHolder .getResource ())
125
115
.build ();
@@ -130,12 +120,50 @@ private SdkTracerProviderBuilder customizeTracerProviderBuilder(
130
120
131
121
private SpanExporter customizeSpanExporter (
132
122
SpanExporter spanExporter , ConfigProperties configProps ) {
133
- if (isSmpEnabled (configProps )) {
123
+ if (isAppSignalsEnabled (configProps )) {
134
124
return AwsMetricAttributesSpanExporterBuilder .create (
135
125
spanExporter , ResourceHolder .getResource ())
136
126
.build ();
137
127
}
138
128
139
129
return spanExporter ;
140
130
}
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
+ }
141
169
}
0 commit comments