class ConsoleMetricExporter
Console metrics exporter. Note that the output is human-readable JSON, not compatible with OTLP.
Hierarchy
- class \OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricExporter implements \OpenTelemetry\SDK\Metrics\PushMetricExporterInterface, \OpenTelemetry\SDK\Metrics\AggregationTemporalitySelectorInterface
Expanded class hierarchy of ConsoleMetricExporter
File
-
vendor/
open-telemetry/ sdk/ Metrics/ MetricExporter/ ConsoleMetricExporter.php, line 19
Namespace
OpenTelemetry\SDK\Metrics\MetricExporterView source
class ConsoleMetricExporter implements PushMetricExporterInterface, AggregationTemporalitySelectorInterface {
public function __construct(Temporality|string|null $temporality = null) {
}
/**
* @inheritDoc
*/
public function temporality(MetricMetadataInterface $metric) : Temporality|string|null {
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(),
];
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ConsoleMetricExporter::convertInstrumentationScope | private | function | ||
ConsoleMetricExporter::convertMetric | private | function | ||
ConsoleMetricExporter::convertResource | private | function | ||
ConsoleMetricExporter::export | public | function | @inheritDoc | |
ConsoleMetricExporter::forceFlush | public | function | Overrides PushMetricExporterInterface::forceFlush | |
ConsoleMetricExporter::shutdown | public | function | ||
ConsoleMetricExporter::temporality | public | function | @inheritDoc | Overrides AggregationTemporalitySelectorInterface::temporality |
ConsoleMetricExporter::__construct | public | function |