31
31
32
32
import os
33
33
import logging
34
- from typing import Dict
34
+ from typing import Dict , Optional
35
35
from flask import request
36
36
37
37
from opentelemetry .sdk .resources import SERVICE_NAME , Resource
@@ -49,9 +49,10 @@ class Otlp:
49
49
__service_name : str = "jormungandr"
50
50
__platform : str = "unknown"
51
51
__account : str = "unknown"
52
+ __instance_id : str = "unknown"
52
53
__labels : Dict = {}
53
54
54
- def __init__ (self , platform : str , account : str ) -> None :
55
+ def __init__ (self , platform : str , account : str , instance_id : Optional [ str ] ) -> None :
55
56
self .__log = logging .getLogger (__name__ )
56
57
self ._tracer = None
57
58
self ._meter = None
@@ -64,6 +65,7 @@ def __init__(self, platform: str, account: str) -> None:
64
65
try :
65
66
self .__platform = platform + " (Python)"
66
67
self .__account = account
68
+ self .__instance_id = self .__get_task_id () if instance_id is None else instance_id
67
69
self .__resource = Resource (
68
70
attributes = {
69
71
SERVICE_NAME : self .__service_name ,
@@ -79,6 +81,11 @@ def __init__(self, platform: str, account: str) -> None:
79
81
self ._tracer = None
80
82
self ._meter = None
81
83
84
+ def __get_task_id (self ) -> str :
85
+ ecs_container_metadata_uri_v4 = os .getenv ("ECS_CONTAINER_METADATA_URI_V4" , "unknown" )
86
+
87
+ return ecs_container_metadata_uri_v4 .split ("/" )[- 1 ].split ("-" )[0 ]
88
+
82
89
def __init_tracer (self ):
83
90
trace_exporter = OTLPSpanExporter ()
84
91
span_processor = BatchSpanProcessor (trace_exporter )
@@ -161,6 +168,7 @@ def __generate_default_labels(self) -> Dict:
161
168
return {
162
169
"coverage" : "unknown" ,
163
170
"api" : "unknown" ,
171
+ "instance_id" : self .__instance_id ,
164
172
"platform" : self .__platform ,
165
173
"account" : self .__account ,
166
174
}
@@ -221,4 +229,4 @@ def send_event_metrics(self, event_type: str, params: Dict = {}) -> None:
221
229
self .__jormungandr_event .add (1 , labels )
222
230
223
231
224
- otlp_instance = Otlp (os .getenv ("OTEL_PLATFORM" ), os .getenv ("OTEL_ACCOUNT" ))
232
+ otlp_instance = Otlp (os .getenv ("OTEL_PLATFORM" ), os .getenv ("OTEL_ACCOUNT" ), os . getenv ( "OTEL_INSTANCE_ID" ) )
0 commit comments