function MetricConverter::convert
Parameters
iterable<SDK\Metrics\Data\Metric> $batch:
File
-
vendor/
open-telemetry/ exporter-otlp/ MetricConverter.php, line 40
Class
Namespace
OpenTelemetry\Contrib\OtlpCode
public function convert(iterable $batch) : ExportMetricsServiceRequest {
$pExportMetricsServiceRequest = new ExportMetricsServiceRequest();
$resourceMetrics = [];
$resourceCache = [];
$scopeMetrics = [];
$scopeCache = [];
foreach ($batch as $metric) {
$resource = $metric->resource;
$instrumentationScope = $metric->instrumentationScope;
$resourceId = $resourceCache[spl_object_id($resource)] ??= serialize([
$resource->getSchemaUrl(),
$resource->getAttributes()
->toArray(),
$resource->getAttributes()
->getDroppedAttributesCount(),
]);
$instrumentationScopeId = $scopeCache[spl_object_id($instrumentationScope)] ??= serialize([
$instrumentationScope->getName(),
$instrumentationScope->getVersion(),
$instrumentationScope->getSchemaUrl(),
$instrumentationScope->getAttributes()
->toArray(),
$instrumentationScope->getAttributes()
->getDroppedAttributesCount(),
]);
if (($pResourceMetrics = $resourceMetrics[$resourceId] ?? null) === null) {
/** @psalm-suppress InvalidArgument */
$pExportMetricsServiceRequest->getResourceMetrics()[] = $resourceMetrics[$resourceId] = $pResourceMetrics = $this->convertResourceMetrics($resource);
}
if (($pScopeMetrics = $scopeMetrics[$resourceId][$instrumentationScopeId] ?? null) === null) {
/** @psalm-suppress InvalidArgument */
$pResourceMetrics->getScopeMetrics()[] = $scopeMetrics[$resourceId][$instrumentationScopeId] = $pScopeMetrics = $this->convertScopeMetrics($instrumentationScope);
}
/** @psalm-suppress InvalidArgument */
$pScopeMetrics->getMetrics()[] = $this->convertMetric($metric);
}
return $pExportMetricsServiceRequest;
}