From cdd7ad020e165fe680703b6d3319b908b682fb7a Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 20 Oct 2023 17:12:29 +0300 Subject: jaeger-client -> opentelemetry --- .../exporter-otlp/MetricExporterFactory.php | 110 +++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 vendor/open-telemetry/exporter-otlp/MetricExporterFactory.php (limited to 'vendor/open-telemetry/exporter-otlp/MetricExporterFactory.php') diff --git a/vendor/open-telemetry/exporter-otlp/MetricExporterFactory.php b/vendor/open-telemetry/exporter-otlp/MetricExporterFactory.php new file mode 100644 index 000000000..284428b73 --- /dev/null +++ b/vendor/open-telemetry/exporter-otlp/MetricExporterFactory.php @@ -0,0 +1,110 @@ +transportFactory = $transportFactory; + } + + /** + * @psalm-suppress ArgumentTypeCoercion + */ + public function create(): MetricExporterInterface + { + $protocol = Configuration::has(Variables::OTEL_EXPORTER_OTLP_METRICS_PROTOCOL) + ? Configuration::getEnum(Variables::OTEL_EXPORTER_OTLP_METRICS_PROTOCOL) + : Configuration::getEnum(Variables::OTEL_EXPORTER_OTLP_PROTOCOL); + $temporality = $this->getTemporality(); + + return new MetricExporter($this->buildTransport($protocol), $temporality); + } + + /** + * @psalm-suppress UndefinedClass + */ + private function buildTransport(string $protocol): TransportInterface + { + /** + * @todo (https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#periodic-exporting-metricreader) + * - OTEL_METRIC_EXPORT_INTERVAL + * - OTEL_METRIC_EXPORT_TIMEOUT + */ + $endpoint = $this->getEndpoint($protocol); + + $headers = Configuration::has(Variables::OTEL_EXPORTER_OTLP_METRICS_HEADERS) + ? Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_METRICS_HEADERS) + : Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_HEADERS); + $headers += OtlpUtil::getUserAgentHeader(); + $compression = $this->getCompression(); + + $factoryClass = Registry::transportFactory($protocol); + $factory = $this->transportFactory ?: new $factoryClass(); + + return $factory->create( + $endpoint, + Protocols::contentType($protocol), + $headers, + $compression, + ); + } + + /** + * @todo return string|Temporality|null (php >= 8.0) + */ + private function getTemporality() + { + $value = Configuration::getEnum(Variables::OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE); + switch (strtolower($value)) { + case 'cumulative': + return Temporality::CUMULATIVE; + case 'delta': + return Temporality::DELTA; + case 'lowmemory': + return null; + default: + throw new \UnexpectedValueException('Unknown temporality: ' . $value); + } + } + + private function getCompression(): string + { + return Configuration::has(Variables::OTEL_EXPORTER_OTLP_METRICS_COMPRESSION) ? + Configuration::getEnum(Variables::OTEL_EXPORTER_OTLP_METRICS_COMPRESSION) : + Configuration::getEnum(Variables::OTEL_EXPORTER_OTLP_COMPRESSION, self::DEFAULT_COMPRESSION); + } + + private function getEndpoint(string $protocol): string + { + if (Configuration::has(Variables::OTEL_EXPORTER_OTLP_METRICS_ENDPOINT)) { + return Configuration::getString(Variables::OTEL_EXPORTER_OTLP_METRICS_ENDPOINT); + } + $endpoint = Configuration::has(Variables::OTEL_EXPORTER_OTLP_ENDPOINT) + ? Configuration::getString(Variables::OTEL_EXPORTER_OTLP_ENDPOINT) + : Defaults::OTEL_EXPORTER_OTLP_ENDPOINT; + if ($protocol === Protocols::GRPC) { + return $endpoint . OtlpUtil::method(Signals::METRICS); + } + + return HttpEndpointResolver::create()->resolveToString($endpoint, Signals::METRICS); + } +} -- cgit v1.2.3