def test_global_proxy_meter_provider(self): # Global get_meter_provider() should initially be a _ProxyMeterProvider # singleton proxy_meter_provider: _ProxyMeterProvider = get_meter_provider() self.assertIsInstance(proxy_meter_provider, _ProxyMeterProvider) self.assertIs(get_meter_provider(), proxy_meter_provider)
def test_get_meter_provider(reset_meter_provider): """ Test that the API provides a way to get a global default MeterProvider """ assert metrics._METER_PROVIDER is None assert isinstance(get_meter_provider(), _ProxyMeterProvider) metrics._METER_PROVIDER = None with patch.dict("os.environ", {OTEL_PYTHON_METER_PROVIDER: "test_meter_provider"}): with patch("opentelemetry._metrics._load_provider", Mock()): with patch( "opentelemetry._metrics.cast", Mock(**{"return_value": "test_meter_provider"}), ): assert get_meter_provider() == "test_meter_provider"
def test_set_meter_provider(reset_meter_provider): """ Test that the API provides a way to set a global default MeterProvider """ mock = Mock() assert metrics._METER_PROVIDER is None set_meter_provider(mock) assert metrics._METER_PROVIDER is mock assert get_meter_provider() is mock
from opentelemetry._metrics import get_meter_provider, set_meter_provider from opentelemetry.exporter.otlp.proto.grpc._metric_exporter import ( OTLPMetricExporter, ) from opentelemetry.sdk._metrics import MeterProvider from opentelemetry.sdk._metrics.export import PeriodicExportingMetricReader exporter = OTLPMetricExporter(insecure=True) reader = PeriodicExportingMetricReader(exporter) provider = MeterProvider(metric_readers=[reader]) set_meter_provider(provider) meter = get_meter_provider().get_meter("getting-started") counter = meter.create_counter("first_counter") counter.add(1) # TODO: fill in details for additional metrics