Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding suppress_instrumentation in Metrics #529

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import atexit
import threading

from opentelemetry.context import attach, detach, set_value


class PushController(threading.Thread):
"""A push based controller, used for exporting.
Expand Down Expand Up @@ -50,7 +52,9 @@ def shutdown(self):
def tick(self):
# Collect all of the meter's metrics to be exported
self.meter.collect()
token = attach(set_value("suppress_instrumentation", True))
# Export the given metrics in the batcher
self.exporter.export(self.meter.batcher.checkpoint_set())
detach(token)
# Perform post-exporting logic based on batcher configuration
self.meter.batcher.finished_collection()
16 changes: 16 additions & 0 deletions opentelemetry-sdk/tests/metrics/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import unittest
from unittest import mock

from opentelemetry.context import get_value
from opentelemetry.sdk import metrics
from opentelemetry.sdk.metrics.export import (
ConsoleMetricsExporter,
Expand Down Expand Up @@ -511,3 +512,18 @@ def test_push_controller(self):
controller.shutdown()
self.assertTrue(controller.finished.isSet())
exporter.shutdown.assert_any_call()

def test_push_controller_suppress_instrumentation(self):
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
meter = mock.Mock()
exporter = mock.Mock()
exporter.export = lambda x: self.assertIsNotNone(
get_value("suppress_instrumentation")
)
with mock.patch(
"opentelemetry.context._RUNTIME_CONTEXT"
) as context_patch:
controller = PushController(meter, exporter, 30.0)
controller.tick()
self.assertEqual(context_patch.attach.called, True)
self.assertEqual(context_patch.detach.called, True)
self.assertEqual(get_value("suppress_instrumentation"), None)