-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathAwsApplicationSignalsCustomizerProvider.java
More file actions
692 lines (614 loc) · 30.7 KB
/
AwsApplicationSignalsCustomizerProvider.java
File metadata and controls
692 lines (614 loc) · 30.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
* Copyright Amazon.com, Inc. or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.opentelemetry.javaagent.providers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.contrib.awsxray.AwsXrayAdaptiveSamplingConfig;
import io.opentelemetry.contrib.awsxray.AwsXrayRemoteSampler;
import io.opentelemetry.contrib.awsxray.ResourceHolder;
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter;
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
import io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil;
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.metrics.Aggregation;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.concurrent.Immutable;
import software.amazon.opentelemetry.javaagent.providers.exporter.aws.logs.CompactConsoleLogRecordExporter;
import software.amazon.opentelemetry.javaagent.providers.exporter.aws.metrics.AwsCloudWatchEmfExporter;
import software.amazon.opentelemetry.javaagent.providers.exporter.aws.metrics.ConsoleEmfExporter;
import software.amazon.opentelemetry.javaagent.providers.exporter.otlp.aws.logs.OtlpAwsLogRecordExporterBuilder;
import software.amazon.opentelemetry.javaagent.providers.exporter.otlp.aws.traces.OtlpAwsSpanExporterBuilder;
/**
* This customizer performs the following customizations:
*
* <ul>
* <li>Use AlwaysRecordSampler to record all spans.
* <li>Add SpanMetricsProcessor to create RED metrics.
* <li>Add AttributePropagatingSpanProcessor to propagate span attributes from parent to child
* spans.
* <li>Add AwsAttributeGeneratingSpanProcessor to add more attributes to all spans.
* </ul>
*
* <p>You can control when these customizations are applied using the property
* otel.aws.application.signals.enabled or the environment variable
* OTEL_AWS_APPLICATION_SIGNALS_ENABLED. This flag is disabled by default.
*/
@Immutable
public final class AwsApplicationSignalsCustomizerProvider
implements AutoConfigurationCustomizerProvider {
// https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-envvars.html
static final String AWS_REGION = "aws.region";
static final String AWS_DEFAULT_REGION = "aws.default.region";
// TODO: We should clean up and get rid of using AWS_LAMBDA_FUNCTION_NAME and default to
// upstream config property implementation.
static final String AWS_LAMBDA_FUNCTION_NAME_CONFIG = "AWS_LAMBDA_FUNCTION_NAME";
static final String AWS_LAMBDA_FUNCTION_NAME_PROP_CONFIG = "aws.lambda.function.name";
static final String LAMBDA_APPLICATION_SIGNALS_REMOTE_ENVIRONMENT =
"LAMBDA_APPLICATION_SIGNALS_REMOTE_ENVIRONMENT";
private static final Duration DEFAULT_METRIC_EXPORT_INTERVAL = Duration.ofMinutes(1);
private static final Logger logger =
Logger.getLogger(AwsApplicationSignalsCustomizerProvider.class.getName());
static final String AWS_OTLP_TRACES_ENDPOINT_PATTERN =
"^https://xray\\.([a-z0-9-]+)\\.amazonaws\\.com/v1/traces$";
static final String AWS_OTLP_LOGS_ENDPOINT_PATTERN =
"^https://logs\\.([a-z0-9-]+)\\.amazonaws\\.com/v1/logs$";
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-OTLPEndpoint.html#CloudWatch-LogsEndpoint
static final String AWS_OTLP_LOGS_GROUP_HEADER = "x-aws-log-group";
static final String AWS_OTLP_LOGS_STREAM_HEADER = "x-aws-log-stream";
static final String AWS_EMF_METRICS_NAMESPACE = "x-aws-metric-namespace";
private static final String DEPRECATED_SMP_ENABLED_CONFIG = "otel.smp.enabled";
private static final String DEPRECATED_APP_SIGNALS_ENABLED_CONFIG =
"otel.aws.app.signals.enabled";
static final String APPLICATION_SIGNALS_ENABLED_CONFIG = "otel.aws.application.signals.enabled";
static final String OTEL_METRICS_ADD_APPLICATION_SIGNALS_DIMENSIONS =
"otel.metrics.add.application.signals.dimensions";
private static final String OTEL_RESOURCE_PROVIDERS_AWS_ENABLED =
"otel.resource.providers.aws.enabled";
private static final String APPLICATION_SIGNALS_RUNTIME_ENABLED_CONFIG =
"otel.aws.application.signals.runtime.enabled";
private static final String DEPRECATED_SMP_EXPORTER_ENDPOINT_CONFIG =
"otel.aws.smp.exporter.endpoint";
private static final String DEPRECATED_APP_SIGNALS_EXPORTER_ENDPOINT_CONFIG =
"otel.aws.app.signals.exporter.endpoint";
private static final String APPLICATION_SIGNALS_EXPORTER_ENDPOINT_CONFIG =
"otel.aws.application.signals.exporter.endpoint";
private static final String OTEL_JMX_TARGET_SYSTEM_CONFIG = "otel.jmx.target.system";
private static final String OTEL_EXPORTER_OTLP_TRACES_ENDPOINT_CONFIG =
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT";
static final String OTEL_EXPORTER_HTTP_PROTOBUF_PROTOCOL = "http/protobuf";
private static final String AWS_XRAY_DAEMON_ADDRESS_CONFIG = "AWS_XRAY_DAEMON_ADDRESS";
private static final String DEFAULT_UDP_ENDPOINT = "127.0.0.1:2000";
private static final String OTEL_DISABLED_RESOURCE_PROVIDERS_CONFIG =
"otel.java.disabled.resource.providers";
private static final String OTEL_BSP_MAX_EXPORT_BATCH_SIZE_CONFIG =
"otel.bsp.max.export.batch.size";
static final String OTEL_METRICS_EXPORTER = "otel.metrics.exporter";
static final String OTEL_LOGS_EXPORTER = "otel.logs.exporter";
static final String OTEL_TRACES_EXPORTER = "otel.traces.exporter";
static final String OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = "otel.exporter.otlp.traces.protocol";
static final String OTEL_EXPORTER_OTLP_LOGS_PROTOCOL = "otel.exporter.otlp.logs.protocol";
private static final String OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT =
"otel.aws.application.signals.exporter.endpoint";
private static final String OTEL_EXPORTER_OTLP_PROTOCOL = "otel.exporter.otlp.protocol";
static final String OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = "otel.exporter.otlp.traces.endpoint";
static final String OTEL_EXPORTER_OTLP_LOGS_ENDPOINT = "otel.exporter.otlp.logs.endpoint";
private static final String OTEL_TRACES_SAMPLER = "otel.traces.sampler";
private static final String OTEL_TRACES_SAMPLER_ARG = "otel.traces.sampler.arg";
static final String OTEL_EXPORTER_OTLP_LOGS_HEADERS = "otel.exporter.otlp.logs.headers";
private static final String OTEL_EXPORTER_OTLP_COMPRESSION_CONFIG =
"otel.exporter.otlp.compression";
private static final String OTEL_EXPORTER_OTLP_TRACES_COMPRESSION_CONFIG =
"otel.exporter.otlp.traces.compression";
private static final String OTEL_EXPORTER_OTLP_LOGS_COMPRESSION_CONFIG =
"otel.exporter.otlp.logs.compression";
private static final String AWS_XRAY_ADAPTIVE_SAMPLING_CONFIG =
"aws.xray.adaptive.sampling.config";
// UDP packet can be upto 64KB. To limit the packet size, we limit the exported batch size.
// This is a bit of a magic number, as there is no simple way to tell how many spans can make a
// 64KB batch since spans can vary in size.
private static final int LAMBDA_SPAN_EXPORT_BATCH_SIZE = 10;
private Sampler sampler;
private boolean isEmfExporterEnabled = false;
public void customize(AutoConfigurationCustomizer autoConfiguration) {
autoConfiguration.addPropertiesCustomizer(this::customizeProperties);
autoConfiguration.addPropertiesCustomizer(this::customizeLambdaEnvProperties);
autoConfiguration.addResourceCustomizer(this::customizeResource);
autoConfiguration.addSamplerCustomizer(this::customizeSampler);
autoConfiguration.addTracerProviderCustomizer(this::customizeTracerProviderBuilder);
autoConfiguration.addMeterProviderCustomizer(this::customizeMeterProvider);
autoConfiguration.addSpanExporterCustomizer(this::customizeSpanExporter);
autoConfiguration.addLogRecordExporterCustomizer(this::customizeLogsExporter);
autoConfiguration.addMetricExporterCustomizer(this::customizeMetricExporter);
}
static boolean shouldAddApplicationSignalsDimensionsEnabled(ConfigProperties props) {
return props.getBoolean(OTEL_METRICS_ADD_APPLICATION_SIGNALS_DIMENSIONS, true);
}
private static Optional<String> getAwsRegionFromConfig(ConfigProperties configProps) {
String region = configProps.getString(AWS_REGION);
if (region != null) {
return Optional.of(region);
}
return Optional.ofNullable(configProps.getString(AWS_DEFAULT_REGION));
}
static boolean isLambdaEnvironment(ConfigProperties props) {
return props.getString(AWS_LAMBDA_FUNCTION_NAME_PROP_CONFIG) != null;
}
static boolean isLambdaEnvironment() {
return System.getenv(AWS_LAMBDA_FUNCTION_NAME_CONFIG) != null;
}
private boolean isApplicationSignalsEnabled(ConfigProperties configProps) {
return configProps.getBoolean(
APPLICATION_SIGNALS_ENABLED_CONFIG,
configProps.getBoolean(
DEPRECATED_APP_SIGNALS_ENABLED_CONFIG,
configProps.getBoolean(DEPRECATED_SMP_ENABLED_CONFIG, false)));
}
private boolean isApplicationSignalsRuntimeEnabled(ConfigProperties configProps) {
return isApplicationSignalsEnabled(configProps)
&& configProps.getBoolean(APPLICATION_SIGNALS_RUNTIME_ENABLED_CONFIG, true);
}
Map<String, String> customizeProperties(ConfigProperties configProps) {
Map<String, String> propsOverride = new HashMap<>();
boolean isLambdaEnvironment = isLambdaEnvironment();
// Check if awsemf was specified and remove it from OTEL_METRICS_EXPORTER
Optional<String> filteredExporters =
AwsApplicationSignalsConfigUtils.removeEmfExporterIfEnabled(configProps);
if (filteredExporters.isPresent()) {
this.isEmfExporterEnabled = true;
propsOverride.put(OTEL_METRICS_EXPORTER, filteredExporters.get());
}
// Enable AWS Resource Providers
propsOverride.put(OTEL_RESOURCE_PROVIDERS_AWS_ENABLED, "true");
if (!isLambdaEnvironment) {
propsOverride.put(
OTEL_DISABLED_RESOURCE_PROVIDERS_CONFIG,
this.disableResourceProvider(
configProps,
Collections.singletonList(
"io.opentelemetry.contrib.aws.resource.LambdaResourceProvider")));
}
if (isApplicationSignalsEnabled(configProps)) {
if (!isLambdaEnvironment) {
// Check if properties exist in `configProps`, and only set if missing
if (configProps.getString(OTEL_METRICS_EXPORTER) == null) {
propsOverride.put(OTEL_METRICS_EXPORTER, "none");
}
if (configProps.getString(OTEL_LOGS_EXPORTER) == null) {
propsOverride.put(OTEL_LOGS_EXPORTER, "none");
}
if (configProps.getString(OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT) == null) {
propsOverride.put(
OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT, "http://localhost:4316/v1/metrics");
}
if (configProps.getString(OTEL_EXPORTER_OTLP_PROTOCOL) == null) {
propsOverride.put(OTEL_EXPORTER_OTLP_PROTOCOL, "http/protobuf");
}
if (configProps.getString(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) == null) {
propsOverride.put(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, "http://localhost:4316/v1/traces");
}
if (configProps.getString(OTEL_TRACES_SAMPLER) == null) {
propsOverride.put(OTEL_TRACES_SAMPLER, "xray");
}
if (configProps.getString(OTEL_TRACES_SAMPLER_ARG) == null) {
propsOverride.put(OTEL_TRACES_SAMPLER_ARG, "endpoint=http://localhost:2000");
}
}
if (isApplicationSignalsRuntimeEnabled(configProps)) {
List<String> list = configProps.getList(OTEL_JMX_TARGET_SYSTEM_CONFIG);
if (list.contains("jvm")) {
logger.log(Level.INFO, "Found jmx in {0}", OTEL_JMX_TARGET_SYSTEM_CONFIG);
} else {
logger.log(Level.INFO, "Configure jmx in {0}", OTEL_JMX_TARGET_SYSTEM_CONFIG);
List<String> jmxTargets = new ArrayList<>(list);
jmxTargets.add("jvm");
propsOverride.put(OTEL_JMX_TARGET_SYSTEM_CONFIG, String.join(",", jmxTargets));
}
}
}
return propsOverride;
}
private Map<String, String> customizeLambdaEnvProperties(ConfigProperties configProperties) {
if (isLambdaEnvironment()) {
Map<String, String> propsOverride = new HashMap<>(2);
// Disable other AWS Resource Providers
List<String> disabledResourceProviders =
Arrays.asList(
"io.opentelemetry.contrib.aws.resource.BeanstalkResourceProvider",
"io.opentelemetry.contrib.aws.resource.Ec2ResourceProvider",
"io.opentelemetry.contrib.aws.resource.EcsResourceProvider",
"io.opentelemetry.contrib.aws.resource.EksResourceProvider");
propsOverride.put(
OTEL_DISABLED_RESOURCE_PROVIDERS_CONFIG,
this.disableResourceProvider(configProperties, disabledResourceProviders));
// Set the max export batch size for BatchSpanProcessors
propsOverride.put(
OTEL_BSP_MAX_EXPORT_BATCH_SIZE_CONFIG, String.valueOf(LAMBDA_SPAN_EXPORT_BATCH_SIZE));
return propsOverride;
}
return Collections.emptyMap();
}
private String disableResourceProvider(
ConfigProperties oldProps, List<String> resourceProviders) {
List<String> list = oldProps.getList(OTEL_DISABLED_RESOURCE_PROVIDERS_CONFIG);
Set<String> disabledResourceProviders = new HashSet<>(list);
disabledResourceProviders.addAll(resourceProviders);
return String.join(",", disabledResourceProviders);
}
private Resource customizeResource(Resource resource, ConfigProperties configProps) {
if (isApplicationSignalsEnabled(configProps)) {
AttributesBuilder builder = Attributes.builder();
AwsResourceAttributeConfigurator.setServiceAttribute(
resource,
builder,
() -> logger.log(Level.WARNING, "Service name is undefined, use UnknownService instead"));
Resource additionalResource = Resource.create((builder.build()));
return resource.merge(additionalResource);
}
return resource;
}
private Sampler customizeSampler(Sampler sampler, ConfigProperties configProps) {
if (sampler instanceof AwsXrayRemoteSampler) {
String config = configProps.getString(AWS_XRAY_ADAPTIVE_SAMPLING_CONFIG);
AwsXrayAdaptiveSamplingConfig parsedConfig = null;
try {
parsedConfig = parseConfigString(config);
} catch (Exception e) {
logger.log(
Level.WARNING, "Failed to parse adaptive sampling configuration: {0}", e.getMessage());
}
if (parsedConfig != null) {
try {
((AwsXrayRemoteSampler) sampler).setAdaptiveSamplingConfig(parsedConfig);
} catch (Exception e) {
logger.log(
Level.WARNING, "Error processing adaptive sampling config: {0}", e.getMessage());
}
}
this.sampler = sampler;
}
if (isApplicationSignalsEnabled(configProps)) {
return AlwaysRecordSampler.create(sampler);
}
return sampler;
}
private SdkTracerProviderBuilder customizeTracerProviderBuilder(
SdkTracerProviderBuilder tracerProviderBuilder, ConfigProperties configProps) {
if (isLambdaEnvironment()) {
tracerProviderBuilder.addSpanProcessor(new AwsLambdaSpanProcessor());
}
if (isApplicationSignalsEnabled(configProps)) {
logger.info("AWS Application Signals enabled");
Duration exportInterval =
SDKMeterProviderBuilder.getMetricExportInterval(
configProps, DEFAULT_METRIC_EXPORT_INTERVAL, logger);
// Construct and set local and remote attributes span processor
tracerProviderBuilder.addSpanProcessor(
AttributePropagatingSpanProcessorBuilder.create().build());
// Construct and set AWS Attribute Generating Span Processor
tracerProviderBuilder.addSpanProcessor(
AwsAttributeGeneratingSpanProcessorBuilder.create(ResourceHolder.getResource()).build());
// If running on Lambda, we just need to export 100% spans and skip generating any Application
// Signals metrics.
if (isLambdaEnvironment()
&& System.getenv(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT_CONFIG) == null) {
String tracesEndpoint =
Optional.ofNullable(System.getenv(AWS_XRAY_DAEMON_ADDRESS_CONFIG))
.orElse(DEFAULT_UDP_ENDPOINT);
SpanExporter spanExporter =
new OtlpUdpSpanExporterBuilder()
.setPayloadSampleDecision(TracePayloadSampleDecision.UNSAMPLED)
.setEndpoint(tracesEndpoint)
.build();
tracerProviderBuilder.addSpanProcessor(
AwsUnsampledOnlySpanProcessorBuilder.create()
.setSpanExporter(spanExporter)
.setMaxExportBatchSize(LAMBDA_SPAN_EXPORT_BATCH_SIZE)
.build());
return tracerProviderBuilder;
}
// Construct meterProvider
MetricExporter metricsExporter =
ApplicationSignalsExporterProvider.INSTANCE.createExporter(configProps);
MetricReader metricReader =
PeriodicMetricReader.builder(metricsExporter).setInterval(exportInterval).build();
SdkMeterProvider meterProvider =
SdkMeterProvider.builder()
.setResource(ResourceHolder.getResource())
.registerMetricReader(metricReader)
.build();
// Construct and set application signals metrics processor
AwsSpanMetricsProcessorBuilder awsSpanMetricsProcessorBuilder =
AwsSpanMetricsProcessorBuilder.create(
meterProvider, ResourceHolder.getResource(), meterProvider::forceFlush);
if (this.sampler != null) {
awsSpanMetricsProcessorBuilder.setSampler(this.sampler);
}
SpanProcessor spanMetricsProcessor = awsSpanMetricsProcessorBuilder.build();
tracerProviderBuilder.addSpanProcessor(spanMetricsProcessor);
}
return tracerProviderBuilder;
}
private SdkMeterProviderBuilder customizeMeterProvider(
SdkMeterProviderBuilder sdkMeterProviderBuilder, ConfigProperties configProps) {
if (isApplicationSignalsRuntimeEnabled(configProps)) {
Set<String> registeredScopeNames = new HashSet<>(1);
String jmxRuntimeScopeName = "io.opentelemetry.jmx";
registeredScopeNames.add(jmxRuntimeScopeName);
SDKMeterProviderBuilder.configureMetricFilter(
configProps, sdkMeterProviderBuilder, registeredScopeNames, logger);
MetricExporter metricsExporter =
ApplicationSignalsExporterProvider.INSTANCE.createExporter(configProps);
MetricReader metricReader =
ScopeBasedPeriodicMetricReader.create(metricsExporter, registeredScopeNames)
.setInterval(
SDKMeterProviderBuilder.getMetricExportInterval(
configProps, DEFAULT_METRIC_EXPORT_INTERVAL, logger))
.build();
sdkMeterProviderBuilder.registerMetricReader(metricReader);
logger.info("AWS Application Signals runtime metric collection enabled");
}
return sdkMeterProviderBuilder;
}
SpanExporter customizeSpanExporter(SpanExporter spanExporter, ConfigProperties configProps) {
// When running in Lambda, override the default OTLP exporter with UDP exporter
if (isLambdaEnvironment()) {
if (isOtlpSpanExporter(spanExporter)
&& System.getenv(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT_CONFIG) == null) {
String tracesEndpoint =
Optional.ofNullable(System.getenv(AWS_XRAY_DAEMON_ADDRESS_CONFIG))
.orElse(DEFAULT_UDP_ENDPOINT);
spanExporter =
new OtlpUdpSpanExporterBuilder()
.setPayloadSampleDecision(TracePayloadSampleDecision.SAMPLED)
.setEndpoint(tracesEndpoint)
.build();
}
}
if (AwsApplicationSignalsConfigUtils.isSigV4EnabledTraces(configProps)) {
// can cast here since we've checked that the configuration for OTEL_TRACES_EXPORTER is otlp
// and OTEL_EXPORTER_OTLP_TRACES_PROTOCOL is http/protobuf
// so the given spanExporter will be an instance of OtlpHttpSpanExporter
// get compression method from environment
String compression =
configProps.getString(
OTEL_EXPORTER_OTLP_TRACES_COMPRESSION_CONFIG,
configProps.getString(OTEL_EXPORTER_OTLP_COMPRESSION_CONFIG, "none"));
try {
spanExporter =
OtlpAwsSpanExporterBuilder.create(
(OtlpHttpSpanExporter) spanExporter,
configProps.getString(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT))
.setCompression(compression)
.build();
} catch (Exception e) {
// This technically should never happen as the validator checks for the correct env
// variables
throw new IllegalStateException(
"Given SpanExporter is not an instance of OtlpHttpSpanExporter, please check that you have the correct environment variables: ",
e);
}
}
if (this.sampler instanceof AwsXrayRemoteSampler) {
((AwsXrayRemoteSampler) this.sampler).setSpanExporter(spanExporter);
}
return spanExporter;
}
private boolean isOtlpSpanExporter(SpanExporter spanExporter) {
return spanExporter instanceof OtlpGrpcSpanExporter
|| spanExporter instanceof OtlpHttpSpanExporter;
}
LogRecordExporter customizeLogsExporter(
LogRecordExporter logsExporter, ConfigProperties configProps) {
if (AwsApplicationSignalsConfigUtils.isSigV4EnabledLogs(configProps)) {
// can cast here since we've checked that the configuration for OTEL_LOGS_EXPORTER is otlp and
// OTEL_EXPORTER_OTLP_LOGS_PROTOCOL is http/protobuf
// so the given logsExporter will be an instance of OtlpHttpLogRecorderExporter
// get compression method from environment
String compression =
configProps.getString(
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION_CONFIG,
configProps.getString(OTEL_EXPORTER_OTLP_COMPRESSION_CONFIG, "none"));
try {
return OtlpAwsLogRecordExporterBuilder.create(
(OtlpHttpLogRecordExporter) logsExporter,
configProps.getString(OTEL_EXPORTER_OTLP_LOGS_ENDPOINT))
.setCompression(compression)
.build();
} catch (Exception e) {
// This technically should never happen as the validator checks for the correct env
// variables
throw new IllegalStateException(
"Given LogsExporter is not an instance of OtlpHttpLogRecordExporter, please check that you have the correct environment variables: ",
e);
}
}
String logsExporterConfig = configProps.getString(OTEL_LOGS_EXPORTER);
if (isLambdaEnvironment(configProps)
&& logsExporterConfig != null
&& logsExporterConfig.equals("console")) {
return new CompactConsoleLogRecordExporter();
}
return logsExporter;
}
MetricExporter customizeMetricExporter(
MetricExporter metricExporter, ConfigProperties configProps) {
if (isEmfExporterEnabled) {
boolean shouldAddApplicationSignalsDimensions =
AwsApplicationSignalsCustomizerProvider.shouldAddApplicationSignalsDimensionsEnabled(
configProps);
Map<String, String> headers =
AwsApplicationSignalsConfigUtils.parseOtlpHeaders(
configProps.getString(OTEL_EXPORTER_OTLP_LOGS_HEADERS));
Optional<String> awsRegion = getAwsRegionFromConfig(configProps);
String namespace = headers.get(AWS_EMF_METRICS_NAMESPACE);
if (awsRegion.isPresent()) {
if (headers.containsKey(AWS_OTLP_LOGS_GROUP_HEADER)
&& headers.containsKey(AWS_OTLP_LOGS_STREAM_HEADER)) {
String logGroup = headers.get(AWS_OTLP_LOGS_GROUP_HEADER);
String logStream = headers.get(AWS_OTLP_LOGS_STREAM_HEADER);
return AwsCloudWatchEmfExporter.builder()
.setNamespace(namespace)
.setLogGroupName(logGroup)
.setLogStreamName(logStream)
.setAwsRegion(awsRegion.get())
.setShouldAddApplicationSignalsDimensions(shouldAddApplicationSignalsDimensions)
.build();
}
if (isLambdaEnvironment(configProps)) {
return ConsoleEmfExporter.builder()
.setNamespace(namespace)
.setShouldAddApplicationSignalsDimensions(shouldAddApplicationSignalsDimensions)
.build();
}
logger.warning(
String.format(
"Improper EMF Exporter configuration: Please configure the environment variable OTEL_EXPORTER_OTLP_LOGS_HEADERS to have values for %s, %s, and %s",
AWS_OTLP_LOGS_GROUP_HEADER,
AWS_OTLP_LOGS_STREAM_HEADER,
AWS_EMF_METRICS_NAMESPACE));
} else {
logger.warning(
String.format(
"Improper EMF Exporter configuration: AWS region not found in environment variables please set %s or %s",
AWS_REGION, AWS_DEFAULT_REGION));
}
}
return metricExporter;
}
static AwsXrayAdaptiveSamplingConfig parseConfigString(String config)
throws JsonProcessingException {
if (config == null) {
return null;
}
// Check if the config is a file path and the file exists
Path path = Paths.get(config);
if (Files.exists(path)) {
try {
config = String.join("\n", Files.readAllLines(path, StandardCharsets.UTF_8));
} catch (IOException e) {
throw new IllegalArgumentException(
"Failed to read adaptive sampling configuration file: " + e.getMessage(), e);
}
}
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
Map<String, Object> configMap =
yamlMapper.readValue(config, new TypeReference<Map<String, Object>>() {});
Object versionObj = configMap.get("version");
if (versionObj == null) {
throw new IllegalArgumentException(
"Missing required 'version' field in adaptive sampling configuration");
}
double version = ((Number) versionObj).doubleValue();
if (version >= 2L) {
throw new IllegalArgumentException(
"Incompatible adaptive sampling config version: "
+ version
+ ". This version of the AWS X-Ray remote sampler only supports versions strictly below 2.0.");
}
return yamlMapper.readValue(config, AwsXrayAdaptiveSamplingConfig.class);
}
private enum ApplicationSignalsExporterProvider {
INSTANCE;
public MetricExporter createExporter(ConfigProperties configProps) {
String protocol =
OtlpConfigUtil.getOtlpProtocol(OtlpConfigUtil.DATA_TYPE_METRICS, configProps);
logger.log(
Level.FINE, String.format("AWS Application Signals export protocol: %s", protocol));
String applicationSignalsEndpoint;
if (protocol.equals(OtlpConfigUtil.PROTOCOL_HTTP_PROTOBUF)) {
applicationSignalsEndpoint =
configProps.getString(
APPLICATION_SIGNALS_EXPORTER_ENDPOINT_CONFIG,
configProps.getString(
DEPRECATED_APP_SIGNALS_EXPORTER_ENDPOINT_CONFIG,
configProps.getString(
DEPRECATED_SMP_EXPORTER_ENDPOINT_CONFIG,
"http://localhost:4316/v1/metrics")));
logger.log(
Level.FINE,
String.format(
"AWS Application Signals export endpoint: %s", applicationSignalsEndpoint));
return OtlpHttpMetricExporter.builder()
.setEndpoint(applicationSignalsEndpoint)
.setDefaultAggregationSelector(this::getAggregation)
.setAggregationTemporalitySelector(CloudWatchTemporalitySelector.alwaysDelta())
.build();
} else if (protocol.equals(OtlpConfigUtil.PROTOCOL_GRPC)) {
applicationSignalsEndpoint =
configProps.getString(
APPLICATION_SIGNALS_EXPORTER_ENDPOINT_CONFIG,
configProps.getString(
DEPRECATED_APP_SIGNALS_EXPORTER_ENDPOINT_CONFIG,
configProps.getString(
DEPRECATED_SMP_EXPORTER_ENDPOINT_CONFIG, "http://localhost:4315")));
logger.log(
Level.FINE,
String.format(
"AWS Application Signals export endpoint: %s", applicationSignalsEndpoint));
return OtlpGrpcMetricExporter.builder()
.setEndpoint(applicationSignalsEndpoint)
.setDefaultAggregationSelector(this::getAggregation)
.setAggregationTemporalitySelector(CloudWatchTemporalitySelector.alwaysDelta())
.build();
}
throw new ConfigurationException(
"Unsupported AWS Application Signals export protocol: " + protocol);
}
private Aggregation getAggregation(InstrumentType instrumentType) {
if (instrumentType == InstrumentType.HISTOGRAM) {
return Aggregation.base2ExponentialBucketHistogram();
}
return Aggregation.defaultAggregation();
}
}
}