Skip to content

Commit d04693c

Browse files
authored
Merge pull request #4358 from hove-io/BO_4174_add_instance_id
Refs #NAV-3914 - Add new label of instance id.
2 parents 2ff5746 + 6b6c9c5 commit d04693c

File tree

1 file changed

+11
-3
lines changed
  • source/jormungandr/jormungandr

1 file changed

+11
-3
lines changed

source/jormungandr/jormungandr/otlp.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import os
3333
import logging
34-
from typing import Dict
34+
from typing import Dict, Optional
3535
from flask import request
3636

3737
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
@@ -49,9 +49,10 @@ class Otlp:
4949
__service_name: str = "jormungandr"
5050
__platform: str = "unknown"
5151
__account: str = "unknown"
52+
__instance_id: str = "unknown"
5253
__labels: Dict = {}
5354

54-
def __init__(self, platform: str, account: str) -> None:
55+
def __init__(self, platform: str, account: str, instance_id: Optional[str]) -> None:
5556
self.__log = logging.getLogger(__name__)
5657
self._tracer = None
5758
self._meter = None
@@ -64,6 +65,7 @@ def __init__(self, platform: str, account: str) -> None:
6465
try:
6566
self.__platform = platform + " (Python)"
6667
self.__account = account
68+
self.__instance_id = self.__get_task_id() if instance_id is None else instance_id
6769
self.__resource = Resource(
6870
attributes={
6971
SERVICE_NAME: self.__service_name,
@@ -79,6 +81,11 @@ def __init__(self, platform: str, account: str) -> None:
7981
self._tracer = None
8082
self._meter = None
8183

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+
8289
def __init_tracer(self):
8390
trace_exporter = OTLPSpanExporter()
8491
span_processor = BatchSpanProcessor(trace_exporter)
@@ -161,6 +168,7 @@ def __generate_default_labels(self) -> Dict:
161168
return {
162169
"coverage": "unknown",
163170
"api": "unknown",
171+
"instance_id": self.__instance_id,
164172
"platform": self.__platform,
165173
"account": self.__account,
166174
}
@@ -221,4 +229,4 @@ def send_event_metrics(self, event_type: str, params: Dict = {}) -> None:
221229
self.__jormungandr_event.add(1, labels)
222230

223231

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

Comments
 (0)