File tree 2 files changed +20
-0
lines changed
src/opentelemetry/sdk/metrics/export
2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 15
15
import atexit
16
16
import threading
17
17
18
+ from opentelemetry .context import attach , detach , set_value
19
+
18
20
19
21
class PushController (threading .Thread ):
20
22
"""A push based controller, used for exporting.
@@ -50,7 +52,9 @@ def shutdown(self):
50
52
def tick (self ):
51
53
# Collect all of the meter's metrics to be exported
52
54
self .meter .collect ()
55
+ token = attach (set_value ("suppress_instrumentation" , True ))
53
56
# Export the given metrics in the batcher
54
57
self .exporter .export (self .meter .batcher .checkpoint_set ())
58
+ detach (token )
55
59
# Perform post-exporting logic based on batcher configuration
56
60
self .meter .batcher .finished_collection ()
Original file line number Diff line number Diff line change 17
17
import unittest
18
18
from unittest import mock
19
19
20
+ from opentelemetry .context import get_value
20
21
from opentelemetry .sdk import metrics
21
22
from opentelemetry .sdk .metrics .export import (
22
23
ConsoleMetricsExporter ,
@@ -511,3 +512,18 @@ def test_push_controller(self):
511
512
controller .shutdown ()
512
513
self .assertTrue (controller .finished .isSet ())
513
514
exporter .shutdown .assert_any_call ()
515
+
516
+ def test_push_controller_suppress_instrumentation (self ):
517
+ meter = mock .Mock ()
518
+ exporter = mock .Mock ()
519
+ exporter .export = lambda x : self .assertIsNotNone (
520
+ get_value ("suppress_instrumentation" )
521
+ )
522
+ with mock .patch (
523
+ "opentelemetry.context._RUNTIME_CONTEXT"
524
+ ) as context_patch :
525
+ controller = PushController (meter , exporter , 30.0 )
526
+ controller .tick ()
527
+ self .assertEqual (context_patch .attach .called , True )
528
+ self .assertEqual (context_patch .detach .called , True )
529
+ self .assertEqual (get_value ("suppress_instrumentation" ), None )
You can’t perform that action at this time.
0 commit comments