def tearDownClass(cls): # This is done because set_tracer_provider cannot override the # current tracer provider. trace_api._TRACER_PROVIDER = None # pylint: disable=protected-access trace_api.set_tracer_provider(cls.original_tracer_provider) # This is done because set_meter_provider cannot override the # current meter provider. metrics_api._METER_PROVIDER = None # pylint: disable=protected-access metrics_api.set_meter_provider(cls.original_meter_provider)
def setUpClass(cls): cls.original_tracer_provider = trace_api.get_tracer_provider() result = cls.create_tracer_provider() cls.tracer_provider, cls.memory_exporter = result trace_api.set_tracer_provider(cls.tracer_provider) cls.original_meter_provider = metrics_api.get_meter_provider() result = cls.create_meter_provider() cls.meter_provider, cls.memory_metrics_exporter = result metrics_api.set_meter_provider(cls.meter_provider)
def test_meter_provider_override_warning(self): """metrics.set_meter_provider should throw a warning when overridden""" metrics.set_meter_provider(MeterProvider()) with self.assertLogs(level=WARNING) as test: metrics.set_meter_provider(MeterProvider()) self.assertEqual( test.output, [("WARNING:opentelemetry.metrics:Overriding of current " "MeterProvider is not allowed")], )
def setUpClass(cls): # pylint: disable=protected-access cls._resource_labels = { "key_with_str_value": "some string", "key_with_int_val": 321, "key_with_true": True, } metrics.set_meter_provider( MeterProvider(resource=Resource(cls._resource_labels))) cls._meter = metrics.get_meter(__name__) cls._labels = {"environment": "staging", "number": 321} cls._key_labels = get_dict_as_key(cls._labels)
def register(self): """ NOTE: Applications should instead call `initialize()`, which will call this method on the application's behalf. Registers this Telemetry instance as the default, global instance. If an existing instance was registered, it will be replaced. :return: None """ trace_api._TRACER_PROVIDER = None metrics_api._METER_PROVIDER = None trace_api.set_tracer_provider(self.tracer_provider) metrics_api.set_meter_provider(self.metrics.meter_provider)
def setUpClass(cls): metrics.set_meter_provider(MeterProvider()) cls._meter = metrics.get_meter(__name__) cls._test_metric = cls._meter.create_metric("testname", "testdesc", "unit", int, Counter, ["environment"]) testing_labels = {"environment": "testing"} cls._test_metric.add(5, testing_labels) cls._instrumentation_key = "99c42f65-1656-4c41-afde-bd86b709a4a7" cls._manager = None cls._ping = None cls._post = None cls._span_processor = AzureMetricsSpanProcessor()
def setUpClass(cls): metrics.set_meter_provider(MeterProvider()) cls._meter = metrics.get_meter(__name__) cls._test_metric = cls._meter.create_metric("testname", "testdesc", "unit", int, Counter, ["environment"]) testing_labels = {"environment": "testing"} cls._test_metric.add(5, testing_labels) cls._instrumentation_key = "12345678-1234-5678-abcd-12345678abcd" cls._manager = None cls._ping = None cls._post = None cls._span_processor = AzureMetricsSpanProcessor()
def setUp(self): set_meter_provider(metrics.MeterProvider()) self._meter = get_meter_provider().get_meter(__name__) self._test_metric = self._meter.create_counter( "testname", "testdesc", "unit", int, ) labels = {"environment": "staging"} self._labels_key = get_dict_as_key(labels) self._mock_registry_register = mock.Mock() self._registry_register_patch = mock.patch( "prometheus_client.core.REGISTRY.register", side_effect=self._mock_registry_register, )
def setUpClass(cls): cls.original_tracer_provider = trace_api.get_tracer_provider() result = cls.create_tracer_provider() cls.tracer_provider, cls.memory_exporter = result # This is done because set_tracer_provider cannot override the # current tracer provider. trace_api._TRACER_PROVIDER = None # pylint: disable=protected-access trace_api.set_tracer_provider(cls.tracer_provider) cls.original_meter_provider = metrics_api.get_meter_provider() result = cls.create_meter_provider() cls.meter_provider, cls.memory_metrics_exporter = result # This is done because set_meter_provider cannot override the # current meter provider. metrics_api._METER_PROVIDER = None # pylint: disable=protected-access metrics_api.set_meter_provider(cls.meter_provider)
def setUp(self): set_meter_provider(metrics.MeterProvider()) self._meter = get_meter_provider().get_meter(__name__) self._test_metric = self._meter.create_metric( "testname", "testdesc", "unit", int, metrics.Counter, ["environment"], ) kvp = {"environment": "staging"} self._test_label_set = self._meter.get_label_set(kvp) self._mock_registry_register = mock.Mock() self._registry_register_patch = mock.patch( "prometheus_client.core.REGISTRY.register", side_effect=self._mock_registry_register, )
def setUpClass(cls): cls._instrumentation_key = "12345678-1234-5678-abcd-12345678abcd" metrics.set_meter_provider(MeterProvider()) cls._meter = metrics.get_meter(__name__) cls._test_metric = cls._meter.create_metric("testname", "testdesc", "unit", int, Counter, ["environment"]) cls._test_metric2 = cls._meter.create_metric("testname", "testdesc", "unit", int, ValueRecorder, ["environment"]) cls._test_obs = cls._meter.register_observer( lambda x: x, "testname", "testdesc", "unit", int, ValueObserver, ["environment"], ) cls._test_labels = tuple({"environment": "staging"}.items()) cls._span_processor = AzureMetricsSpanProcessor()
def setUpClass(cls): os.environ[ "APPINSIGHTS_INSTRUMENTATIONKEY"] = "1234abcd-5678-4efa-8abc-1234567890ab" cls._exporter = AzureMonitorMetricsExporter(storage_path=STORAGE_PATH) metrics.set_meter_provider(MeterProvider()) cls._meter = metrics.get_meter(__name__) cls._test_metric = cls._meter.create_metric("testname", "testdesc", "unit", int, Counter, ["environment"]) cls._test_value_recorder = cls._meter.create_metric( "testname", "testdesc", "unit", int, ValueRecorder, ["environment"]) cls._test_obs = cls._meter.register_observer( lambda x: x, "testname", "testdesc", "unit", int, ValueObserver, ["environment"], ) cls._test_labels = tuple({"environment": "staging"}.items())
trace.set_tracer_provider(TracerProvider(resource=resource)) tracer = trace.get_tracer(__name__) OTEL_AGENT = os.getenv('OTEL_AGENT', "otel-agent") otlp_exporter = OTLPSpanExporter(endpoint=OTEL_AGENT + ":4317", insecure=True) span_processor = BatchExportSpanProcessor(otlp_exporter) trace.get_tracer_provider().add_span_processor(span_processor) metric_exporter = OpenCensusMetricsExporter( endpoint=OTEL_AGENT + ":55678", service_name="service1", ) # Meter is responsible for creating and recording metrics metrics.set_meter_provider(MeterProvider(resource=resource)) meter = metrics.get_meter(__name__) # controller collects metrics created from meter and exports it via the # exporter every interval controller = PushController(meter, metric_exporter, 5) requests_counter = meter.create_counter( name="requests_count", description="number of requests", unit="1", value_type=int, ) # Labels are used to identify key-values that are associated with a specific # metric that you want to record. These are useful for pre-aggregation and can # be used to store custom dimensions pertaining to a metric labels = {"service_id": "service1"}
# Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. # pylint: disable=import-error # pylint: disable=no-member # pylint: disable=no-name-in-module import time import requests from opentelemetry import metrics from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.sdk.metrics import MeterProvider from azure_monitor import AzureMonitorMetricsExporter # Use the default sdk implementation metrics.set_meter_provider(MeterProvider(stateful=False)) # Track telemetry from the requests library RequestsInstrumentor().instrument() meter = RequestsInstrumentor().meter exporter = AzureMonitorMetricsExporter( connection_string="InstrumentationKey=<INSTRUMENTATION KEY HERE>") # Export standard metrics from requests library to Azure Monitor metrics.get_meter_provider().start_pipeline(meter, exporter, 5) for x in range(10): for y in range(10): requests.get("http://example.com") time.sleep(2) time.sleep(5)
def tearDownClass(cls): trace_api.set_tracer_provider(cls.original_tracer_provider) metrics_api.set_meter_provider(cls.original_meter_provider)
def enable_metrics() -> None: from opentelemetry.metrics import set_meter_provider from opentelemetry.sdk.metrics import MeterProvider set_meter_provider(MeterProvider())
def setUpClass(cls): # pylint: disable=protected-access metrics.set_meter_provider(MeterProvider()) cls._meter = metrics.get_meter(__name__) cls._labels = {"environment": "staging"} cls._key_labels = get_labels_as_key(cls._labels)
def setUpClass(cls): metrics.set_meter_provider(MeterProvider()) cls._meter = metrics.get_meter(__name__) cls._test_labels = tuple({"environment": "staging"}.items())
def setUpClass(cls): metrics_api._METER_PROVIDER = None set_meter_provider(metrics.MeterProvider())
def setUpClass(cls): metrics.set_meter_provider(MeterProvider()) cls._meter = metrics.get_meter(__name__) cls._test_labels = {"environment": "staging"} cls._span_processor = AzureMetricsSpanProcessor()
# limitations under the License. # """ This example shows how to export metrics to the OT collector. """ from opentelemetry import metrics from opentelemetry.ext.otcollector.metrics_exporter import ( CollectorMetricsExporter, ) from opentelemetry.sdk.metrics import Counter, MeterProvider from opentelemetry.sdk.metrics.export.controller import PushController exporter = CollectorMetricsExporter(service_name="basic-service", endpoint="localhost:55678") metrics.set_meter_provider(MeterProvider()) meter = metrics.get_meter(__name__) controller = PushController(meter, exporter, 5) requests_counter = meter.create_metric( name="requests", description="number of requests", unit="1", value_type=int, metric_type=Counter, label_keys=("environment", ), ) staging_label_set = meter.get_label_set({"environment": "staging"}) requests_counter.add(25, staging_label_set)
def setUpClass(cls): # pylint: disable=protected-access metrics.set_meter_provider(MeterProvider()) cls._meter = metrics.get_meter(__name__) kvp = {"environment": "staging"} cls._test_label_set = cls._meter.get_label_set(kvp)