def test_export(self):
     meter_provider = metrics.MeterProvider()
     meter = meter_provider.get_meter(__name__)
     exporter = ConsoleMetricsExporter()
     metric = metrics.Counter(
         "available memory",
         "available memory",
         "bytes",
         int,
         meter,
         ("environment", ),
     )
     labels = {"environment": "staging"}
     aggregator = SumAggregator()
     record = MetricRecord(metric, labels, aggregator,
                           meter_provider.resource)
     result = '{}(data="{}", labels="{}", value={}, resource={})'.format(
         ConsoleMetricsExporter.__name__,
         metric,
         labels,
         aggregator.checkpoint,
         meter_provider.resource.attributes,
     )
     with mock.patch("sys.stdout") as mock_stdout:
         exporter.export([record])
         mock_stdout.write.assert_any_call(result)
Exemple #2
0
 def test_export(self):
     exporter = ConsoleMetricsExporter()
     metric = metrics.Counter(
         "available memory",
         "available memory",
         "bytes",
         int,
         ("environment", ),
     )
     label_values = ("staging", )
     handle = metric.get_handle(label_values)
     result = '{}(data="{}", label_values="{}", metric_data={})'.format(
         ConsoleMetricsExporter.__name__, metric, label_values, handle)
     with mock.patch("sys.stdout") as mock_stdout:
         exporter.export([(metric, label_values)])
         mock_stdout.write.assert_any_call(result)
Exemple #3
0
 def test_export(self):
     meter = metrics.MeterProvider().get_meter(__name__)
     exporter = ConsoleMetricsExporter()
     metric = metrics.Counter(
         "available memory",
         "available memory",
         "bytes",
         int,
         meter,
         ("environment", ),
     )
     kvp = {"environment": "staging"}
     label_set = metrics.LabelSet(kvp)
     aggregator = CounterAggregator()
     record = MetricRecord(aggregator, label_set, metric)
     result = '{}(data="{}", label_set="{}", value={})'.format(
         ConsoleMetricsExporter.__name__,
         metric,
         label_set.labels,
         aggregator.checkpoint,
     )
     with mock.patch("sys.stdout") as mock_stdout:
         exporter.export([record])
         mock_stdout.write.assert_any_call(result)
Exemple #4
0
from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, Meter
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter

metrics.set_preferred_meter_implementation(lambda T: Meter())
meter = metrics.meter()
exporter = ConsoleMetricsExporter()

counter = meter.create_metric(
    "available memory",
    "available memory",
    "bytes",
    int,
    Counter,
    ("environment",),
)

label_values = ("staging",)
counter_handle = counter.get_handle(label_values)
counter_handle.add(100)

exporter.export([(counter, label_values)])
exporter.shutdown()