12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from logging import DEBUG , ERROR , basicConfig , getLogger
15
+ from logging import (
16
+ CRITICAL ,
17
+ DEBUG ,
18
+ ERROR ,
19
+ INFO ,
20
+ NOTSET ,
21
+ WARNING ,
22
+ basicConfig ,
23
+ getLevelName ,
24
+ getLogger ,
25
+ )
16
26
from typing import Optional
17
27
18
28
from environs import Env
31
41
from opentelemetry .trace import get_tracer_provider , set_tracer_provider
32
42
33
43
_env = Env ()
44
+ _logger = getLogger (__name__ )
34
45
35
46
_DEFAULT_OTEL_EXPORTER_OTLP_SPAN_ENDPOINT = "ingest.lightstep.com:443"
36
47
_DEFAULT_OTEL_EXPORTER_OTLP_METRIC_ENDPOINT = (
58
69
"telemetry.sdk.version" : __version__ ,
59
70
},
60
71
)
61
- _OTEL_LOG_LEVEL = _env .int ("OTEL_LOG_LEVEL" , ERROR )
72
+ _OTEL_LOG_LEVEL = _env .str ("OTEL_LOG_LEVEL" , " ERROR" )
62
73
_OTEL_EXPORTER_OTLP_SPAN_INSECURE = _env .bool (
63
74
"OTEL_EXPORTER_OTLP_SPAN_INSECURE" , False
64
75
)
65
76
_OTEL_EXPORTER_OTLP_METRIC_INSECURE = _env .bool (
66
77
"OTEL_EXPORTER_OTLP_METRIC_INSECURE" , False
67
78
)
68
79
69
- _logger = getLogger (__name__ )
70
-
71
80
72
81
class InvalidConfigurationError (Exception ):
73
82
"""
@@ -83,12 +92,13 @@ def configure_opentelemetry(
83
92
service_version : str = _LS_SERVICE_VERSION ,
84
93
propagator : list = _OTEL_PROPAGATORS ,
85
94
resource_labels : str = _OTEL_RESOURCE_LABELS ,
86
- log_level : int = _OTEL_LOG_LEVEL ,
95
+ log_level : str = _OTEL_LOG_LEVEL ,
87
96
span_exporter_endpoint_insecure : bool = _OTEL_EXPORTER_OTLP_SPAN_INSECURE ,
88
97
metric_exporter_endpoint_insecure : bool = (
89
98
_OTEL_EXPORTER_OTLP_METRIC_INSECURE
90
99
),
91
100
):
101
+ # pylint: disable=too-many-locals
92
102
"""
93
103
Configures OpenTelemetry with Lightstep environment variables
94
104
@@ -127,8 +137,16 @@ def configure_opentelemetry(
127
137
"telemetry.sdk.language": "python",
128
138
"telemetry.sdk.version": "0.9b0",
129
139
}`
130
- log_level (int): OTEL_LOG_LEVEL, an int value that indicates the log
131
- level. Defaults to `logging.ERROR`.
140
+ log_level (str): OTEL_LOG_LEVEL, one of:
141
+
142
+ - `NOTSET` (0)
143
+ - `DEBUG` (10)
144
+ - `INFO` (20)
145
+ - `WARNING` (30)
146
+ - `ERROR` (40)
147
+ - `CRITICAL` (50)
148
+
149
+ Defaults to `logging.ERROR`.
132
150
span_exporter_endpoint_insecure (bool):
133
151
OTEL_EXPORTER_OTLP_SPAN_INSECURE, a boolean value that indicates if
134
152
an insecure channel is to be used to send spans to the satellite.
@@ -139,6 +157,28 @@ def configure_opentelemetry(
139
157
satellite. Defaults to `False`.
140
158
"""
141
159
160
+ log_levels = {
161
+ "NOTSET" : NOTSET ,
162
+ "DEBUG" : DEBUG ,
163
+ "INFO" : INFO ,
164
+ "WARNING" : WARNING ,
165
+ "ERROR" : ERROR ,
166
+ "CRITICAL" : CRITICAL ,
167
+ }
168
+
169
+ log_level = log_level .upper ()
170
+
171
+ if log_level not in log_levels .keys ():
172
+
173
+ message = (
174
+ "Invalid configuration: invalid log_level value."
175
+ "It must be one of {}." .format (", " .join (log_levels .keys ()))
176
+ )
177
+ _logger .error (message )
178
+ raise InvalidConfigurationError (message )
179
+
180
+ log_level = log_levels [log_level ]
181
+
142
182
basicConfig (level = log_level )
143
183
144
184
_logger .debug ("configuration" )
@@ -151,7 +191,7 @@ def configure_opentelemetry(
151
191
"service_version" : service_version ,
152
192
"propagator" : propagator ,
153
193
"resource_labels" : resource_labels ,
154
- "log_level" : log_level ,
194
+ "log_level" : getLevelName ( log_level ) ,
155
195
"span_exporter_endpoint_insecure" : span_exporter_endpoint_insecure ,
156
196
"metric_exporter_endpoint_insecure" : metric_exporter_endpoint_insecure ,
157
197
}.items ():
@@ -230,7 +270,7 @@ def configure_opentelemetry(
230
270
231
271
get_tracer_provider ().resource = Resource (resource_labels )
232
272
233
- if log_level = = DEBUG :
273
+ if log_level > = DEBUG :
234
274
get_tracer_provider ().add_span_processor (
235
275
BatchExportSpanProcessor (ConsoleSpanExporter ())
236
276
)
0 commit comments