3
3
# Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.
4
4
import os
5
5
from logging import Logger , getLogger
6
- from typing import ClassVar , Dict , Type
6
+ from typing import ClassVar , Dict , Type , Union
7
7
8
+ from amazon .opentelemetry .distro ._aws_resource_attribute_configurator import get_service_attribute
8
9
from importlib_metadata import version
10
+ from opentelemetry .metrics import set_meter_provider
11
+ from opentelemetry .sdk .metrics .view import View , DefaultAggregation , DropAggregation
9
12
from typing_extensions import override
10
-
13
+ from amazon . opentelemetry . distro . _aws_attribute_keys import AWS_LOCAL_SERVICE
11
14
from amazon .opentelemetry .distro .always_record_sampler import AlwaysRecordSampler
12
15
from amazon .opentelemetry .distro .attribute_propagating_span_processor_builder import (
13
16
AttributePropagatingSpanProcessorBuilder ,
14
17
)
15
18
from amazon .opentelemetry .distro .aws_metric_attributes_span_exporter_builder import (
16
19
AwsMetricAttributesSpanExporterBuilder ,
17
20
)
21
+ from amazon .opentelemetry .distro .scope_based_exporter import ScopeBasedPeriodicExportingMetricReader
18
22
from amazon .opentelemetry .distro .aws_span_metrics_processor_builder import AwsSpanMetricsProcessorBuilder
19
23
from amazon .opentelemetry .distro .otlp_udp_exporter import OTLPUdpMetricExporter , OTLPUdpSpanExporter
20
24
from amazon .opentelemetry .distro .sampler .aws_xray_remote_sampler import AwsXRayRemoteSampler
27
31
_import_id_generator ,
28
32
_import_sampler ,
29
33
_init_logging ,
30
- _init_metrics ,
31
34
_OTelSDKConfigurator ,
32
35
)
33
36
from opentelemetry .sdk .environment_variables import (
48
51
ObservableUpDownCounter ,
49
52
UpDownCounter ,
50
53
)
51
- from opentelemetry .sdk .metrics .export import AggregationTemporality , PeriodicExportingMetricReader
52
- from opentelemetry .sdk .resources import Resource , get_aggregated_resources
54
+ from opentelemetry .sdk .metrics .export import MetricExporter , MetricReader , AggregationTemporality , PeriodicExportingMetricReader
55
+ from opentelemetry .sdk .resources import Resource , get_aggregated_resources , SERVICE_NAME
53
56
from opentelemetry .sdk .trace import TracerProvider
54
57
from opentelemetry .sdk .trace .export import BatchSpanProcessor , SpanExporter
55
58
from opentelemetry .sdk .trace .id_generator import IdGenerator
56
59
from opentelemetry .sdk .trace .sampling import Sampler
57
60
from opentelemetry .semconv .resource import ResourceAttributes
58
61
from opentelemetry .trace import set_tracer_provider
59
62
60
- APP_SIGNALS_ENABLED_CONFIG = "OTEL_AWS_APP_SIGNALS_ENABLED"
63
+ DEPRECATED_APP_SIGNALS_ENABLED_CONFIG = "OTEL_AWS_APP_SIGNALS_ENABLED"
61
64
APPLICATION_SIGNALS_ENABLED_CONFIG = "OTEL_AWS_APPLICATION_SIGNALS_ENABLED"
62
- APP_SIGNALS_EXPORTER_ENDPOINT_CONFIG = "OTEL_AWS_APP_SIGNALS_EXPORTER_ENDPOINT"
65
+ APPLICATION_SIGNALS_RUNTIME_ENABLED_CONFIG = "OTEL_AWS_APPLICATION_SIGNALS_RUNTIME_ENABLED"
66
+ DEPRECATED_APP_SIGNALS_EXPORTER_ENDPOINT_CONFIG = "OTEL_AWS_APP_SIGNALS_EXPORTER_ENDPOINT"
63
67
APPLICATION_SIGNALS_EXPORTER_ENDPOINT_CONFIG = "OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT"
64
68
METRIC_EXPORT_INTERVAL_CONFIG = "OTEL_METRIC_EXPORT_INTERVAL"
65
69
DEFAULT_METRIC_EXPORT_INTERVAL = 60000.0
@@ -105,13 +109,15 @@ def _initialize_components():
105
109
106
110
auto_resource : Dict [str , any ] = {}
107
111
auto_resource = _customize_versions (auto_resource )
108
- resource = get_aggregated_resources (
109
- [
110
- AwsEc2ResourceDetector (),
111
- AwsEksResourceDetector (),
112
- AwsEcsResourceDetector (),
113
- ]
114
- ).merge (Resource .create (auto_resource ))
112
+ # auto_resource = _set_aws_attributes(auto_resource)
113
+ resource = _customize_resource (
114
+ get_aggregated_resources (
115
+ [
116
+ AwsEc2ResourceDetector (),
117
+ AwsEksResourceDetector (),
118
+ AwsEcsResourceDetector (),
119
+ ]
120
+ ).merge (Resource .create (auto_resource )))
115
121
116
122
sampler_name = _get_sampler ()
117
123
sampler = _custom_import_sampler (sampler_name , resource )
@@ -153,6 +159,49 @@ def _init_tracing(
153
159
set_tracer_provider (trace_provider )
154
160
155
161
162
+ def _init_metrics (
163
+ exporters_or_readers : Dict [
164
+ str , Union [Type [MetricExporter ], Type [MetricReader ]]
165
+ ],
166
+ resource : Resource = None ,
167
+ ):
168
+ metric_readers = []
169
+ views = []
170
+
171
+ for _ , exporter_or_reader_class in exporters_or_readers .items ():
172
+ exporter_args = {}
173
+
174
+ if issubclass (exporter_or_reader_class , MetricReader ):
175
+ metric_readers .append (exporter_or_reader_class (** exporter_args ))
176
+ else :
177
+ metric_readers .append (
178
+ PeriodicExportingMetricReader (
179
+ exporter_or_reader_class (** exporter_args )
180
+ )
181
+ )
182
+
183
+ if _is_application_signals_runtime_enabled ():
184
+ system_metrics_scope_name = "opentelemetry.instrumentation.system_metrics"
185
+ if 0 == len (metric_readers ):
186
+ _logger .info ("Registered scope %s" , system_metrics_scope_name )
187
+ views .append (View (
188
+ meter_name = system_metrics_scope_name , aggregation = DefaultAggregation ()
189
+ ))
190
+ views .append (View (
191
+ instrument_name = "*" ,aggregation = DropAggregation ()
192
+ ))
193
+
194
+ otel_metric_exporter = ApplicationSignalsExporterProvider ().create_exporter ()
195
+ scope_based_periodic_exporting_metric_reader = ScopeBasedPeriodicExportingMetricReader (
196
+ exporter = otel_metric_exporter , export_interval_millis = _get_metric_export_interval (),
197
+ registered_scope_names = {system_metrics_scope_name },
198
+ )
199
+ metric_readers .append (scope_based_periodic_exporting_metric_reader )
200
+
201
+ provider = MeterProvider (resource = resource , metric_readers = metric_readers , views = views )
202
+ set_meter_provider (provider )
203
+
204
+
156
205
# END The OpenTelemetry Authors code
157
206
158
207
@@ -237,14 +286,9 @@ def _customize_span_processors(provider: TracerProvider, resource: Resource) ->
237
286
# Construct meterProvider
238
287
_logger .info ("AWS Application Signals enabled" )
239
288
otel_metric_exporter = ApplicationSignalsExporterProvider ().create_exporter ()
240
- export_interval_millis = float (os .environ .get (METRIC_EXPORT_INTERVAL_CONFIG , DEFAULT_METRIC_EXPORT_INTERVAL ))
241
- _logger .debug ("Span Metrics export interval: %s" , export_interval_millis )
242
- # Cap export interval to 60 seconds. This is currently required for metrics-trace correlation to work correctly.
243
- if export_interval_millis > DEFAULT_METRIC_EXPORT_INTERVAL :
244
- export_interval_millis = DEFAULT_METRIC_EXPORT_INTERVAL
245
- _logger .info ("AWS Application Signals metrics export interval capped to %s" , export_interval_millis )
289
+
246
290
periodic_exporting_metric_reader = PeriodicExportingMetricReader (
247
- exporter = otel_metric_exporter , export_interval_millis = export_interval_millis
291
+ exporter = otel_metric_exporter , export_interval_millis = _get_metric_export_interval ()
248
292
)
249
293
meter_provider : MeterProvider = MeterProvider (resource = resource , metric_readers = [periodic_exporting_metric_reader ])
250
294
# Construct and set application signals metrics processor
@@ -260,9 +304,25 @@ def _customize_versions(auto_resource: Dict[str, any]) -> Dict[str, any]:
260
304
return auto_resource
261
305
262
306
307
+ def _customize_resource (resource : Resource ) -> Resource :
308
+ service_name , is_unknown = get_service_attribute (resource )
309
+ if is_unknown :
310
+ _logger .debug ("No valid service name found" )
311
+
312
+ return resource .merge (Resource .create ({AWS_LOCAL_SERVICE : service_name }))
313
+
314
+
263
315
def _is_application_signals_enabled ():
264
316
return (
265
- os .environ .get (APPLICATION_SIGNALS_ENABLED_CONFIG , os .environ .get (APP_SIGNALS_ENABLED_CONFIG , "false" )).lower ()
317
+ os .environ .get (APPLICATION_SIGNALS_ENABLED_CONFIG , os .environ .get (DEPRECATED_APP_SIGNALS_ENABLED_CONFIG , "false" )).lower ()
318
+ == "true"
319
+ )
320
+
321
+
322
+
323
+ def _is_application_signals_runtime_enabled ():
324
+ return _is_application_signals_enabled and (
325
+ os .environ .get (APPLICATION_SIGNALS_RUNTIME_ENABLED_CONFIG , "true" ).lower ()
266
326
== "true"
267
327
)
268
328
@@ -272,6 +332,16 @@ def _is_lambda_environment():
272
332
return AWS_LAMBDA_FUNCTION_NAME_CONFIG in os .environ
273
333
274
334
335
+ def _get_metric_export_interval ():
336
+ export_interval_millis = float (os .environ .get (METRIC_EXPORT_INTERVAL_CONFIG , DEFAULT_METRIC_EXPORT_INTERVAL ))
337
+ _logger .debug ("Span Metrics export interval: %s" , export_interval_millis )
338
+ # Cap export interval to 60 seconds. This is currently required for metrics-trace correlation to work correctly.
339
+ if export_interval_millis > DEFAULT_METRIC_EXPORT_INTERVAL :
340
+ export_interval_millis = DEFAULT_METRIC_EXPORT_INTERVAL
341
+ _logger .info ("AWS Application Signals metrics export interval capped to %s" , export_interval_millis )
342
+ return export_interval_millis
343
+
344
+
275
345
class ApplicationSignalsExporterProvider :
276
346
_instance : ClassVar ["ApplicationSignalsExporterProvider" ] = None
277
347
@@ -307,7 +377,7 @@ def create_exporter(self):
307
377
if protocol == "http/protobuf" :
308
378
application_signals_endpoint = os .environ .get (
309
379
APPLICATION_SIGNALS_EXPORTER_ENDPOINT_CONFIG ,
310
- os .environ .get (APP_SIGNALS_EXPORTER_ENDPOINT_CONFIG , "http://localhost:4316/v1/metrics" ),
380
+ os .environ .get (DEPRECATED_APP_SIGNALS_EXPORTER_ENDPOINT_CONFIG , "http://localhost:4316/v1/metrics" ),
311
381
)
312
382
_logger .debug ("AWS Application Signals export endpoint: %s" , application_signals_endpoint )
313
383
return OTLPHttpOTLPMetricExporter (
@@ -323,7 +393,7 @@ def create_exporter(self):
323
393
324
394
application_signals_endpoint = os .environ .get (
325
395
APPLICATION_SIGNALS_EXPORTER_ENDPOINT_CONFIG ,
326
- os .environ .get (APP_SIGNALS_EXPORTER_ENDPOINT_CONFIG , "localhost:4315" ),
396
+ os .environ .get (DEPRECATED_APP_SIGNALS_EXPORTER_ENDPOINT_CONFIG , "localhost:4315" ),
327
397
)
328
398
_logger .debug ("AWS Application Signals export endpoint: %s" , application_signals_endpoint )
329
399
return OTLPGrpcOTLPMetricExporter (
0 commit comments