Skip to content

Commit f57e972

Browse files
Convert IIB_OTEL_TRACING to an env variable
1 parent 25580ab commit f57e972

8 files changed

Lines changed: 42 additions & 71 deletions

File tree

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,6 @@ The custom configuration options for the REST API are listed below:
208208
* `IIB_LOG_LEVEL` - the Python log level of the REST API (Flask). This defaults to `INFO`.
209209
* `IIB_MAX_PER_PAGE` - the maximum number of build requests that can be shown on a single page.
210210
This defaults to `20`.
211-
* `IIB_OTEL_TRACING` - the boolean value which indicates if OpenTelemetry tracing is enabled in IIB.
212-
This defaults to `False`.
213211
* `IIB_REQUEST_DATA_DAYS_TO_LIVE` - the amount of days after which per request temmporary data is
214212
considered to be expired and may be removed. This defaults to `3`.
215213
* `IIB_REQUEST_LOGS_DIR` - the directory to load the request specific log files. If `None`, per
@@ -271,8 +269,12 @@ More info on these environment variables can be found in the [AWS User Guide](ht
271269

272270
### Opentelemetry Environment Variable
273271

274-
To integrate IIB with Opentelemetery tracing, we will need the below two parameters.
272+
To integrate IIB with Opentelemetery tracing, we will need the following parameters as
273+
environment variables.
275274

275+
* `IIB_OTEL_TRACING` - the boolean value which indicates if OpenTelemetry tracing is enabled in IIB.
276+
This defaults to `False`. If this is set to `True`, `OTEL_EXPORTER_OTLP_ENDPOINT` and `OTEL_SERVICE_NAME`
277+
must be set.
276278
* `OTEL_EXPORTER_OTLP_ENDPOINT` - The endpoint for the OpenTelemetry exporter.
277279
* `OTEL_SERVICE_NAME` - "iib-api"
278280

@@ -397,8 +399,6 @@ The custom configuration options for the Celery workers are listed below:
397399
}
398400
```
399401

400-
* `iib_otel_tracing` - the boolean value which indicates if OpenTelemetry tracing is enabled in IIB.
401-
This defaults to `False`.
402402
* `iib_request_related_bundles_dir` - the directory to write the request specific related bundles
403403
file. If `None`, per request related bundles files are not created. This defaults to `None`.
404404
* `iib_request_logs_dir` - the directory to write the request specific log files. If `None`, per
@@ -433,8 +433,12 @@ More info on these environment variables can be found in the [AWS User Guide](ht
433433

434434
### Opentelemetry Environment Variable
435435

436-
To integrate IIB with Opentelemetery tracing, we will need the below two parameters.
436+
To integrate IIB with Opentelemetery tracing, we will need the following parameters
437+
as environment variables.
437438

439+
* `IIB_OTEL_TRACING` - the boolean value which indicates if OpenTelemetry tracing is enabled in IIB.
440+
This defaults to `False`. If this is set to `True`, `OTEL_EXPORTER_OTLP_ENDPOINT` and `OTEL_SERVICE_NAME`
441+
must be set.
438442
* `OTEL_EXPORTER_OTLP_ENDPOINT` - The endpoint for the OpenTelemetry exporter.
439443
* `OTEL_SERVICE_NAME` - "iib-workers"
440444

iib/common/tracing.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def func():
3030
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
3131
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
3232

33-
from iib.web.config import Config
3433

3534
log = logging.getLogger(__name__)
3635
propagator = TraceContextTextMapPropagator()
@@ -43,7 +42,7 @@ class TracingWrapper:
4342

4443
def __new__(cls):
4544
"""Create a new instance if one does not exist."""
46-
if not Config.IIB_OTEL_TRACING:
45+
if not os.getenv('IIB_OTEL_TRACING', '').lower() == 'true':
4746
return None
4847

4948
if TracingWrapper.__instance is None:
@@ -87,7 +86,7 @@ def instrument_tracing(
8786
def decorator_instrument_tracing(func):
8887
@functools.wraps(func)
8988
def wrapper(*args, **kwargs):
90-
if not Config.IIB_OTEL_TRACING:
89+
if not os.getenv('IIB_OTEL_TRACING', '').lower() == 'true':
9190
return func(*args, **kwargs)
9291
log.debug('Context inside %s: %s', span_name, context)
9392
if kwargs.get('traceparent'):

iib/web/app.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ def validate_api_config(config: Config) -> None:
159159
'These are used for read/write access to the s3 bucket by IIB'
160160
)
161161

162-
if config['IIB_OTEL_TRACING']:
163-
if not isinstance(config['IIB_OTEL_TRACING'], bool):
164-
raise ConfigError('"IIB_OTEL_TRACING" must be a valid boolean value')
162+
if os.getenv('IIB_OTEL_TRACING', '').lower() == 'true':
165163
if not os.getenv('OTEL_EXPORTER_OTLP_ENDPOINT') or not os.getenv('OTEL_SERVICE_NAME'):
166164
raise ConfigError(
167165
'"OTEL_EXPORTER_OTLP_ENDPOINT" and "OTEL_SERVICE_NAME" environment '
@@ -241,7 +239,7 @@ def create_app(config_obj: Optional[str] = None) -> Flask: # pragma: no cover
241239
app.register_error_handler(kombu.exceptions.KombuError, json_error)
242240

243241
# Add Auto-instrumentation
244-
if app.config['IIB_OTEL_TRACING']:
242+
if os.getenv('IIB_OTEL_TRACING', '').lower() == 'true':
245243
FlaskInstrumentor().instrument_app(
246244
app, enable_commenter=True, commenter_options={}, tracer_provider=tracerWrapper.provider
247245
)

iib/web/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class Config(object):
3030
IIB_MESSAGING_DURABLE: bool = True
3131
IIB_MESSAGING_KEY: str = '/etc/iib/messaging.key'
3232
IIB_MESSAGING_TIMEOUT: int = 30
33-
IIB_OTEL_TRACING: bool = False
3433
IIB_REQUEST_DATA_DAYS_TO_LIVE: int = 3
3534
IIB_REQUEST_LOGS_DIR: Optional[str] = None
3635
IIB_REQUEST_RELATED_BUNDLES_DIR: Optional[str] = None

iib/workers/config.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class Config(object):
4141
iib_log_level: str = 'INFO'
4242
iib_max_recursive_related_bundles = 15
4343
iib_organization_customizations: iib_organization_customizations_type = {}
44-
iib_otel_tracing: bool = False
4544
iib_sac_queues: List[str] = []
4645
iib_request_logs_dir: Optional[str] = None
4746
iib_request_logs_format: str = (
@@ -320,13 +319,11 @@ def validate_celery_config(conf: app.utils.Settings, **kwargs) -> None:
320319
if not os.access(iib_request_temp_data_dir, os.W_OK):
321320
raise ConfigError(f'{directory}, is not writable!')
322321

323-
if conf.get('iib_otel_tracing'):
324-
if not isinstance(conf.get('iib_otel_tracing'), bool):
325-
raise ConfigError('"iib_otel_tracing" must be a valid boolean value')
322+
if os.getenv('IIB_OTEL_TRACING', '').lower() == 'true':
326323
if not os.getenv('OTEL_EXPORTER_OTLP_ENDPOINT') or not os.getenv('OTEL_SERVICE_NAME'):
327324
raise ConfigError(
328325
'"OTEL_EXPORTER_OTLP_ENDPOINT" and "OTEL_SERVICE_NAME" environment '
329-
'variables must be set to valid strings when iib_otel_tracing is set to True.'
326+
'variables must be set to valid strings when "IIB_OTEL_TRACING" is set to True.'
330327
)
331328

332329

iib/workers/tasks/celery.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# SPDX-License-Identifier: GPL-3.0-or-later
2+
import os
3+
24
import celery
35
from celery.signals import celeryd_init
46
from opentelemetry.instrumentation.celery import CeleryInstrumentor
@@ -15,13 +17,13 @@
1517
configure_celery(app)
1618
celeryd_init.connect(validate_celery_config)
1719

18-
if app.conf['iib_otel_tracing']:
20+
if os.getenv('IIB_OTEL_TRACING', '').lower() == 'true':
1921
RequestsInstrumentor().instrument(trace_provider=tracerWrapper.provider)
2022

2123

2224
# Add the init_celery_tracing method with its annotation
2325
@worker_process_init.connect(weak=False)
2426
def init_celery_tracing(*args, **kwargs):
2527
"""Initialize the tracing for celery."""
26-
if app.conf['iib_otel_tracing']:
28+
if os.getenv('IIB_OTEL_TRACING', '').lower() == 'true':
2729
CeleryInstrumentor().instrument(trace_provider=tracerWrapper.provider)

tests/test_web/test_app.py

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -170,37 +170,19 @@ def test_validate_api_config_failure_aws_s3_params(config, error_msg):
170170
validate_api_config(config)
171171

172172

173-
@pytest.mark.parametrize(
174-
'config, error_msg',
175-
(
176-
(
177-
{
178-
'IIB_AWS_S3_BUCKET_NAME': None,
179-
'IIB_REQUEST_LOGS_DIR': 'some-dir',
180-
'IIB_REQUEST_RECURSIVE_RELATED_BUNDLES_DIR': 'some-dir',
181-
'IIB_GREENWAVE_CONFIG': {},
182-
'IIB_BINARY_IMAGE_CONFIG': {},
183-
'IIB_OTEL_TRACING': True,
184-
},
185-
(
186-
'"OTEL_EXPORTER_OTLP_ENDPOINT" and "OTEL_SERVICE_NAME" environment '
187-
'variables must be set to valid strings when IIB_OTEL_TRACING is set to True.'
188-
),
189-
),
190-
(
191-
{
192-
'IIB_AWS_S3_BUCKET_NAME': None,
193-
'IIB_REQUEST_LOGS_DIR': 'some-dir',
194-
'IIB_REQUEST_RECURSIVE_RELATED_BUNDLES_DIR': 'some-dir',
195-
'IIB_GREENWAVE_CONFIG': {},
196-
'IIB_BINARY_IMAGE_CONFIG': {},
197-
'IIB_OTEL_TRACING': 'some-str',
198-
},
199-
'"IIB_OTEL_TRACING" must be a valid boolean value',
200-
),
201-
),
202-
)
203-
def test_validate_api_config_failure_otel_params(config, error_msg):
173+
@mock.patch.dict(os.environ, {'IIB_OTEL_TRACING': 'True'})
174+
def test_validate_api_config_failure_otel_params():
175+
config = {
176+
'IIB_AWS_S3_BUCKET_NAME': None,
177+
'IIB_REQUEST_LOGS_DIR': 'some-dir',
178+
'IIB_REQUEST_RECURSIVE_RELATED_BUNDLES_DIR': 'some-dir',
179+
'IIB_GREENWAVE_CONFIG': {},
180+
'IIB_BINARY_IMAGE_CONFIG': {},
181+
}
182+
error_msg = (
183+
'"OTEL_EXPORTER_OTLP_ENDPOINT" and "OTEL_SERVICE_NAME" environment '
184+
'variables must be set to valid strings when IIB_OTEL_TRACING is set to True.'
185+
)
204186
with pytest.raises(ConfigError, match=error_msg):
205187
validate_api_config(config)
206188

tests/test_workers/test_config.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: GPL-3.0-or-later
22
from unittest.mock import patch
3+
from unittest import mock
34
from io import BytesIO
45
import os
56
import re
@@ -37,6 +38,7 @@ def test_configure_celery_with_classes_and_files(mock_open, mock_isfile, mock_ge
3738
assert celery_app.conf.broker_connection_max_retries == 10
3839

3940

41+
@mock.patch.dict(os.environ, {'IIB_OTEL_TRACING': 'some-str'})
4042
@patch('os.path.isdir', return_value=True)
4143
@patch('os.access', return_value=True)
4244
def test_validate_celery_config(mock_isdir, mock_isaccess):
@@ -291,35 +293,23 @@ def test_validate_celery_config_invalid_s3_env_vars():
291293
validate_celery_config(conf)
292294

293295

294-
@pytest.mark.parametrize(
295-
'config, error',
296-
(
297-
(
298-
{'iib_otel_tracing': True},
299-
(
300-
'"OTEL_EXPORTER_OTLP_ENDPOINT" and "OTEL_SERVICE_NAME" environment '
301-
'variables must be set to valid strings when iib_otel_tracing is set to True.'
302-
),
303-
),
304-
(
305-
{'iib_otel_tracing': 'random-str'},
306-
'"iib_otel_tracing" must be a valid boolean value',
307-
),
308-
),
309-
)
310-
def test_validate_celery_config_invalid_otel_config(tmpdir, config, error):
296+
@mock.patch.dict(os.environ, {'IIB_OTEL_TRACING': 'True'})
297+
def test_validate_celery_config_invalid_otel_config(tmpdir):
311298
conf = {
312299
'iib_api_url': 'http://localhost:8080/api/v1/',
313300
'iib_registry': 'registry',
314301
'iib_required_labels': {},
315302
'iib_organization_customizations': {},
316303
'iib_request_recursive_related_bundles_dir': tmpdir.join('some-dir'),
317304
}
305+
error = (
306+
'"OTEL_EXPORTER_OTLP_ENDPOINT" and "OTEL_SERVICE_NAME" environment '
307+
'variables must be set to valid strings when "IIB_OTEL_TRACING" is set to True.'
308+
)
318309
iib_request_recursive_related_bundles_dir = conf['iib_request_recursive_related_bundles_dir']
319310
iib_request_recursive_related_bundles_dir.mkdir()
320-
worker_config = {**conf, **config}
321311
with pytest.raises(ConfigError, match=error):
322-
validate_celery_config(worker_config)
312+
validate_celery_config(conf)
323313

324314

325315
def test_validate_celery_config_invalid_recursive_related_bundles_config():

0 commit comments

Comments
 (0)