adding sdk console metrics exporter (#1055)
to make getting started easier (in opentelemetry.io docs), create an sdk-based metrics exporter. this means we can export all signals without needing a protobuf implementation. since console export is only useful for playing around, the change in format to a simpler human-readable output seems reasonable.
This commit is contained in:
parent
dc9501e8a9
commit
a0601c41ec
|
@ -13,6 +13,13 @@ use OpenTelemetry\SDK\Trace\TracerProvider;
|
|||
|
||||
require __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* Example of logging used in conjunction with tracing. The trace id and span id
|
||||
* will be injected into the logged record.
|
||||
* Note that logging output is human-readable JSON, and is not compatible with the
|
||||
* OTEL format.
|
||||
*/
|
||||
|
||||
$loggerProvider = new LoggerProvider(
|
||||
new SimpleLogsProcessor(
|
||||
(new ConsoleExporterFactory())->create()
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use OpenTelemetry\API\Metrics\ObserverInterface;
|
||||
use OpenTelemetry\SDK\Metrics\Data\Temporality;
|
||||
use OpenTelemetry\SDK\Metrics\MeterProvider;
|
||||
use OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricsExporter;
|
||||
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
|
||||
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* Basic async/observable metrics generation example. This uses the console
|
||||
* metrics exporter to print metrics out in a human-readable format (but does
|
||||
* not require protobuf or the OTLP exporter)
|
||||
*/
|
||||
|
||||
$reader = new ExportingReader(
|
||||
new ConsoleMetricsExporter(Temporality::DELTA)
|
||||
);
|
||||
|
||||
$meterProvider = MeterProvider::builder()
|
||||
->setResource(ResourceInfoFactory::emptyResource())
|
||||
->addReader($reader)
|
||||
->build();
|
||||
|
||||
$meterProvider
|
||||
->getMeter('demo_meter')
|
||||
->createObservableGauge('number', 'items', 'Random number')
|
||||
->observe(static function (ObserverInterface $observer): void {
|
||||
$observer->observe(random_int(0, 256));
|
||||
});
|
||||
|
||||
//metrics are collected every time `collect()` is called
|
||||
$reader->collect();
|
|
@ -5,8 +5,8 @@ declare(strict_types=1);
|
|||
namespace OpenTelemetry\Example;
|
||||
|
||||
use OpenTelemetry\API\Metrics\ObserverInterface;
|
||||
use OpenTelemetry\Contrib\Otlp\ConsoleMetricExporterFactory;
|
||||
use OpenTelemetry\SDK\Metrics\MeterProvider;
|
||||
use OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricExporterFactory;
|
||||
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
|
||||
|
||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
declare(strict_types=1);
|
||||
\OpenTelemetry\SDK\Registry::registerSpanExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\SpanExporterFactory::class);
|
||||
\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\MetricExporterFactory::class);
|
||||
\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('console', \OpenTelemetry\Contrib\Otlp\ConsoleMetricExporterFactory::class);
|
||||
|
||||
\OpenTelemetry\SDK\Registry::registerTransportFactory('http', \OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory::class);
|
||||
|
||||
|
|
|
@ -13,6 +13,10 @@ use OpenTelemetry\SDK\Logs\LogRecordExporterInterface;
|
|||
use OpenTelemetry\SDK\Logs\ReadableLogRecord;
|
||||
use OpenTelemetry\SDK\Resource\ResourceInfo;
|
||||
|
||||
/**
|
||||
* A JSON console exporter for LogRecords. This is only useful for testing; the
|
||||
* output is human-readable, and is not compatible with the OTLP format.
|
||||
*/
|
||||
class ConsoleExporter implements LogRecordExporterInterface
|
||||
{
|
||||
private TransportInterface $transport;
|
||||
|
|
|
@ -2,18 +2,15 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OpenTelemetry\Contrib\Otlp;
|
||||
namespace OpenTelemetry\SDK\Metrics\MetricExporter;
|
||||
|
||||
use OpenTelemetry\SDK\Metrics\MetricExporterFactoryInterface;
|
||||
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
|
||||
use OpenTelemetry\SDK\Registry;
|
||||
|
||||
class ConsoleMetricExporterFactory implements MetricExporterFactoryInterface
|
||||
{
|
||||
public function create(): MetricExporterInterface
|
||||
{
|
||||
$transport = Registry::transportFactory('stream')->create('php://stdout', 'application/x-ndjson');
|
||||
|
||||
return new MetricExporter($transport);
|
||||
return new ConsoleMetricsExporter();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OpenTelemetry\SDK\Metrics\MetricExporter;
|
||||
|
||||
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
|
||||
use OpenTelemetry\SDK\Metrics\Data\Metric;
|
||||
use OpenTelemetry\SDK\Metrics\Data\Temporality;
|
||||
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
|
||||
use OpenTelemetry\SDK\Metrics\MetricMetadataInterface;
|
||||
use OpenTelemetry\SDK\Resource\ResourceInfo;
|
||||
|
||||
/**
|
||||
* Console metrics exporter.
|
||||
* Note that the output is human-readable JSON, not compatible with OTLP.
|
||||
*/
|
||||
class ConsoleMetricsExporter implements MetricExporterInterface
|
||||
{
|
||||
/**
|
||||
* @var string|Temporality|null
|
||||
*/
|
||||
private $temporality;
|
||||
|
||||
/**
|
||||
* @param string|Temporality|null $temporality
|
||||
*/
|
||||
public function __construct($temporality = null)
|
||||
{
|
||||
$this->temporality = $temporality;
|
||||
}
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function temporality(MetricMetadataInterface $metric)
|
||||
{
|
||||
return $this->temporality ?? $metric->temporality();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function export(iterable $batch): bool
|
||||
{
|
||||
$resource = null;
|
||||
$scope = null;
|
||||
foreach ($batch as $metric) {
|
||||
/** @var Metric $metric */
|
||||
if (!$resource) {
|
||||
$resource = $this->convertResource($metric->resource);
|
||||
}
|
||||
if (!$scope) {
|
||||
$scope = $this->convertInstrumentationScope($metric->instrumentationScope);
|
||||
$scope['metrics'] = [];
|
||||
}
|
||||
$scope['metrics'][] = $this->convertMetric($metric);
|
||||
}
|
||||
$output = [
|
||||
'resource' => $resource,
|
||||
'scope' => $scope,
|
||||
];
|
||||
echo json_encode($output, JSON_PRETTY_PRINT) . PHP_EOL;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function shutdown(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function forceFlush(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private function convertMetric(Metric $metric): array
|
||||
{
|
||||
return [
|
||||
'name' => $metric->name,
|
||||
'description' => $metric->description,
|
||||
'unit' => $metric->unit,
|
||||
'data' => $metric->data,
|
||||
];
|
||||
}
|
||||
|
||||
private function convertResource(ResourceInfo $resource): array
|
||||
{
|
||||
return [
|
||||
'attributes' => $resource->getAttributes()->toArray(),
|
||||
'dropped_attributes_count' => $resource->getAttributes()->getDroppedAttributesCount(),
|
||||
];
|
||||
}
|
||||
private function convertInstrumentationScope(InstrumentationScopeInterface $scope): array
|
||||
{
|
||||
return [
|
||||
'name' => $scope->getName(),
|
||||
'version' => $scope->getVersion(),
|
||||
'attributes' => $scope->getAttributes()->toArray(),
|
||||
'dropped_attributes_count' => $scope->getAttributes()->getDroppedAttributesCount(),
|
||||
'schema_url' => $scope->getSchemaUrl(),
|
||||
];
|
||||
}
|
||||
}
|
|
@ -3,4 +3,5 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('memory', \OpenTelemetry\SDK\Metrics\MetricExporter\InMemoryExporterFactory::class);
|
||||
\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('console', \OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricExporterFactory::class);
|
||||
\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('none', \OpenTelemetry\SDK\Metrics\MetricExporter\NoopMetricExporterFactory::class);
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OpenTelemetry\Tests\Unit\Contrib\Otlp;
|
||||
|
||||
use OpenTelemetry\Contrib\Otlp\ConsoleMetricExporterFactory;
|
||||
use OpenTelemetry\Contrib\Otlp\MetricExporter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \OpenTelemetry\Contrib\Otlp\ConsoleMetricExporterFactory
|
||||
*/
|
||||
class ConsoleMetricExporterFactoryTest extends TestCase
|
||||
{
|
||||
public function test_create(): void
|
||||
{
|
||||
$exporter = (new ConsoleMetricExporterFactory())->create();
|
||||
$this->assertInstanceOf(MetricExporter::class, $exporter);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OpenTelemetry\Tests\Unit\Contrib\Otlp;
|
||||
|
||||
use OpenTelemetry\Contrib\Otlp\ProtobufSerializer;
|
||||
use Opentelemetry\Proto\Collector\Trace\V1\ExportTraceServiceResponse;
|
||||
use OpenTelemetry\SDK\Common\Export\TransportInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \OpenTelemetry\Contrib\Otlp\ProtobufSerializer
|
||||
*/
|
||||
class ProtobufSerializerTest extends TestCase
|
||||
{
|
||||
public function test_empty_json_response(): void
|
||||
{
|
||||
$transport = $this->createMock(TransportInterface::class);
|
||||
$transport->method('contentType')->willReturn('application/json');
|
||||
$serializer = ProtobufSerializer::forTransport($transport);
|
||||
$response = new ExportTraceServiceResponse();
|
||||
$serializer->hydrate($response, '{}');
|
||||
|
||||
$this->assertNull($response->getPartialSuccess());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OpenTelemetry\Tests\Unit\SDK\Metrics\MetricExporter;
|
||||
|
||||
use OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricExporterFactory;
|
||||
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricExporterFactory
|
||||
*/
|
||||
class ConsoleMetricExporterFactoryTest extends TestCase
|
||||
{
|
||||
public function test_create(): void
|
||||
{
|
||||
$exporter = (new ConsoleMetricExporterFactory())->create();
|
||||
$this->assertInstanceOf(MetricExporterInterface::class, $exporter);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue