-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_metrics.py
55 lines (41 loc) · 1.79 KB
/
setup_metrics.py
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import logging
import os
from opentelemetry import metrics
from opentelemetry.exporter.otlp.metrics_exporter import OTLPMetricsExporter
from opentelemetry.sdk.metrics import MeterProvider
logger = logging.getLogger(__name__)
# Setup Global Metric Provider
# Sets up the Global MeterProvider instance
# TODO: Once https://github.com/open-telemetry/opentelemetry-python/issues/1444
# is resolved, move this line to only exist in `manual-instrumentation` apps.
metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter("aws-otel", "1.0")
# Setup Metric Components
apiBytesSentMetricName = "apiBytesSent"
latencyMetricName = "latency"
if "INSTANCE_ID" in os.environ:
instanceId = os.environ["INSTANCE_ID"]
if not instanceId.strip() == "":
latencyMetricName += "_" + instanceId
apiBytesSentMetricName += "_" + instanceId
apiBytesSentCounter = meter.create_counter(
apiBytesSentMetricName, "API request load sent in bytes", "one", int
)
apiLatencyRecorder = meter.create_valuerecorder(
latencyMetricName, "API latency time", "ms", int
)
# Start Metric Pipeline
# Exporter to export metrics to the console
exporter = OTLPMetricsExporter(insecure=True)
# start_pipeline will notify the MeterProvider to begin collecting/exporting
# metrics with the given meter, exporter and interval in seconds
# TODO: Auto Instrumentation will instantiate the MeterProvider before this
# file has a chance to instantiate it. Once https://github.com/open-telemetry/opentelemetry-python/issues/1444
# is resolved, this try except block can be removed.
try:
metrics.get_meter_provider().start_pipeline(meter, exporter, 5)
except:
logger.warning("Metrics Pipeline was not started.")
pass