4
4
5
5
6
6
class Config :
7
- def __init__ (self ):
7
+ def get_config (self , section , key , default ):
8
+ # Allowing OTEL level overrides at top priority
9
+ if key in self .otel_config_binding :
10
+ otel_env_value = os .environ .get (self .otel_config_binding [key ], None )
11
+ if otel_env_value is not None and otel_env_value != "" :
12
+ return otel_env_value
13
+
14
+ # Allowing MW level ENV overrides
15
+ if key in self .config_binding :
16
+ env_value = os .environ .get (self .config_binding [key ], None )
17
+ if env_value is not None and env_value != "" :
18
+ return env_value
19
+
20
+ return self .config .get (section , key , fallback = default )
21
+
22
+ def str_to_bool (self , value , default ):
23
+ if isinstance (value , str ):
24
+ value = value .strip ().lower ()
25
+ if value in {"true" , "1" , "yes" , "y" }:
26
+ return True
27
+ elif value in {"false" , "0" , "no" , "n" }:
28
+ return False
29
+ return default # Default value if the string cannot be converted
30
+
31
+ def get_config_boolean (self , section , key , default ):
32
+ # Allowing OTEL level overrides at top priority
33
+ if key in self .otel_config_binding :
34
+ otel_env_value = os .environ .get (self .otel_config_binding [key ], None )
35
+ if otel_env_value is not None and otel_env_value != "" :
36
+ return self .str_to_bool (otel_env_value , default )
37
+
38
+ # Allowing MW level ENV overrides
39
+ if key in self .config_binding :
40
+ env_value = os .environ .get (self .config_binding [key ], None )
41
+ if env_value is not None and env_value != "" :
42
+ return self .str_to_bool (env_value , default )
43
+
44
+ return self .config .getboolean (section , key , fallback = default )
45
+
46
+ def __init__ (self ):
8
47
if len (sys .argv ) > 1 and sys .argv [1 ] == "help" :
9
48
return
49
+
50
+ self .config_binding = {
51
+ "project_name" : "MW_PROJECT_NAME" ,
52
+ "service_name" : "MW_SERVICE_NAME" ,
53
+ "access_token" : "MW_API_KEY" ,
54
+ "collect_traces" : "MW_APM_COLLECT_TRACES" ,
55
+ "collect_metrics" : "MW_APM_COLLECT_METRICS" ,
56
+ "collect_logs" : "MW_APM_COLLECT_LOGS" ,
57
+ "collect_profiling" : "MW_APM_COLLECT_PROFILING" ,
58
+ "otel_propagators" : "MW_PROPAGATORS" ,
59
+ "mw_agent_service" : "MW_AGENT_SERVICE" ,
60
+ "target" : "MW_TARGET" ,
61
+ "custom_resource_attributes" : "MW_CUSTOM_RESOURCE_ATTRIBUTES" ,
62
+ "log_level" : "MW_LOG_LEVEL" ,
63
+ }
64
+
65
+ self .otel_config_binding = {
66
+ "service_name" : "OTEL_SERVICE_NAME" ,
67
+ "otel_propagators" : "OTEL_PROPAGATORS" ,
68
+ "target" : "OTEL_EXPORTER_OTLP_ENDPOINT"
69
+ }
10
70
11
71
config_file = os .environ .get ("MIDDLEWARE_CONFIG_FILE" , os .path .join (os .getcwd (), 'middleware.ini' ))
12
72
if not os .path .exists (config_file ):
@@ -21,48 +81,43 @@ def __init__(self):
21
81
pid = os .getpid ()
22
82
self .project_name = self .get_config ("middleware.common" , "project_name" , None )
23
83
self .service_name = self .get_config ("middleware.common" , "service_name" , f"Service-{ pid } " )
84
+ self .mw_agent_service = self .get_config ("middleware.common" , "mw_agent_service" , "localhost" )
85
+ self .target = self .get_config ("middleware.common" , "target" , "" )
24
86
self .access_token = self .get_config ("middleware.common" , "access_token" , "" )
25
87
self .collect_traces = self .get_config_boolean ("middleware.common" , "collect_traces" , True )
26
88
self .collect_metrics = self .get_config_boolean ("middleware.common" , "collect_metrics" , False )
27
89
self .collect_logs = self .get_config_boolean ("middleware.common" , "collect_logs" , False )
28
90
self .collect_profiling = self .get_config_boolean ("middleware.common" , "collect_profiling" , False )
29
91
self .otel_propagators = self .get_config ("middleware.common" , "otel_propagators" , "b3" )
92
+ self .custom_resource_attributes = self .get_config ("middleware.common" , "custom_resource_attributes" , "" )
93
+ self .log_level = self .get_config ("middleware.common" , "log_level" , "FATAL" )
30
94
31
- project_name_attr = f"project.name={ self .project_name } ," if self .project_name else ""
32
- source_service_url = self .get_config ("middleware.common" , "mw_agent_service" , "localhost" )
33
-
34
- mw_agent_service = os .environ .get ("MW_AGENT_SERVICE" , None )
35
- if mw_agent_service is not None and mw_agent_service != "" :
36
- source_service_url = mw_agent_service
37
-
38
- self .exporter_otlp_endpoint = f"http://{ source_service_url } :9319"
39
- self .resource_attributes = f"{ project_name_attr } mw.app.lang=python,runtime.metrics.python=true"
95
+ # target will have more priority over mw_agent_service
96
+ self .exporter_otlp_endpoint = f"http://{ self .mw_agent_service } :9319"
97
+ if self .target is not None and self .target != "" :
98
+ self .exporter_otlp_endpoint = self .target
40
99
41
- # Allowing users to override full OTLP endpoint
42
- # Priority OTEL_EXPORTER_OTLP_ENDPOINT > MW_TARGET
43
- mw_target = os .environ .get ("MW_TARGET" , None )
44
- if mw_target is not None and mw_target != "" :
45
- self .exporter_otlp_endpoint = mw_target
46
-
47
- exporter_otlp_endpoint = os .environ .get ("OTEL_EXPORTER_OTLP_ENDPOINT" , None )
48
- if exporter_otlp_endpoint is not None and exporter_otlp_endpoint != "" :
49
- self .exporter_otlp_endpoint = exporter_otlp_endpoint
50
100
51
- # Allowing users to pass Middleware API Key via ENV variable
52
- mw_api_key = os .environ .get ("MW_API_KEY" , None )
53
- if mw_api_key is not None and mw_api_key != "" :
54
- self .access_token = mw_api_key
101
+ self .resource_attributes = "mw.app.lang=python,runtime.metrics.python=true"
102
+
103
+ # Add `mw_serverless` resource attribute if target is not "localhost"
104
+ if "localhost" not in self .exporter_otlp_endpoint and "127.0.0.1" not in self .exporter_otlp_endpoint :
105
+ self .resource_attributes = f"{ self .resource_attributes } ,mw_serverless=true"
106
+
107
+ # Passing Project name as a resource attribute
108
+ if self .project_name is not None and self .project_name != "" :
109
+ self .resource_attributes = f"{ self .resource_attributes } ,project.name={ self .project_name } "
55
110
56
111
# Passing Middleware API Key as a resource attribute, to validate ingestion requests in serverless setup
57
112
if self .access_token is not None and self .access_token != "" :
58
113
self .resource_attributes = f"{ self .resource_attributes } ,mw.account_key={ self .access_token } "
59
-
60
- def get_config ( self , section , key , default ):
61
- return self .config . get ( section , key , fallback = default )
62
-
63
- def get_config_boolean ( self , section , key , default ):
64
- return self . config . getboolean ( section , key , fallback = default )
65
-
114
+
115
+ # Appending Custom Resource Attributes, if any
116
+ if self .custom_resource_attributes is not None and self . custom_resource_attributes != "" :
117
+ self . resource_attributes = f" { self . resource_attributes } , { self . custom_resource_attributes } "
118
+
119
+
120
+
66
121
67
122
def exit_with_error (message ):
68
123
print (message )
0 commit comments