def test_shutdown(self): with mock.patch( "prometheus_client.core.REGISTRY.unregister" ) as registry_unregister_patch: exporter = PrometheusMetricsExporter() exporter.shutdown() self.assertTrue(registry_unregister_patch.called)
def test_export(self): with self._registry_register_patch: record = MetricRecord( self._test_metric, self._labels_key, SumAggregator(), ) exporter = PrometheusMetricsExporter() result = exporter.export([record]) # pylint: disable=protected-access self.assertEqual(len(exporter._collector._metrics_to_export), 1) self.assertIs(result, MetricsExportResult.SUCCESS)
import time from prometheus_client import start_http_server from opentelemetry import metrics from opentelemetry.exporter.prometheus import PrometheusMetricsExporter from opentelemetry.sdk.metrics import Counter, MeterProvider, ValueRecorder from opentelemetry.sdk.metrics.export.controller import PushController # Start Prometheus client start_http_server(port=8000, addr="localhost") metrics.set_meter_provider(MeterProvider()) meter = metrics.get_meter(__name__) # meter = metrics.get_meter(__name__, True) exporter = PrometheusMetricsExporter("MyAppPrefix") controller = PushController(meter, exporter, 5) staging_labels = {"environment": "staging"} requests_counter = meter.create_metric( name="requests", description="number of requests", unit="1", value_type=int, metric_type=Counter, ) size_recorder = meter.create_metric( name="size", description="current size",
import uvicorn from fastapi import FastAPI from opentelemetry import metrics from opentelemetry.exporter.prometheus import PrometheusMetricsExporter from opentelemetry.sdk.metrics import Counter, MeterProvider from opentelemetry.sdk.metrics.export.controller import PushController from starlette_exporter import PrometheusMiddleware, handle_metrics app = FastAPI() app_name = 'api' app.add_middleware(PrometheusMiddleware, app_name=app_name) app.add_route("/metrics", handle_metrics) metrics.set_meter_provider(MeterProvider()) meter = metrics.get_meter(__name__) exporter = PrometheusMetricsExporter(app_name) controller = PushController(meter, exporter, 5) staging_labels = {"environment": "staging"} requests_counter = meter.create_metric( name="requests", description="number of requests", unit="1", value_type=int, metric_type=Counter, label_keys=("environment", ), ) @app.get("/")
def test_constructor(self): """Test the constructor.""" with self._registry_register_patch: exporter = PrometheusMetricsExporter("testprefix") self.assertEqual(exporter._collector._prefix, "testprefix") self.assertTrue(self._mock_registry_register.called)
from opentelemetry.sdk.metrics import MeterProvider from opentelemetry.exporter.prometheus import PrometheusMetricsExporter from opentelemetry.sdk.metrics import Counter, Meter from prometheus_client import start_http_server # Start Prometheus client start_http_server(port=8080, addr="localhost") # Meter is responsible for creating and recording metrics # mp = metrics.DefaultMeterProvider() # metrics.set_meter_provider(mp) metrics.set_meter_provider(MeterProvider()) # metrics.set_meter_provider(global.GetMeter(name, version)) meter = metrics.get_meter(__name__) # exporter to export metrics to Prometheus prefix = "HXNYC" exporter = PrometheusMetricsExporter(prefix) # Starts the collect/export pipeline for metrics metrics.get_meter_provider().start_pipeline(meter, exporter, 5) counter = meter.create_counter( # counter = meter.create_metric( "requests", "number of requests", "requests", int, # Counter, # ("environment",), ) # 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