1111use OpenTelemetry \Contrib \Otlp \MetricExporter ;
1212use OpenTelemetry \Contrib \Otlp \OtlpHttpTransportFactory ;
1313use OpenTelemetry \SDK \Common \Attribute \Attributes ;
14+ use OpenTelemetry \SDK \Common \Attribute \AttributesInterface ;
15+ use OpenTelemetry \SDK \Metrics \Data \Temporality ;
1416use OpenTelemetry \SDK \Metrics \MeterProvider ;
1517use OpenTelemetry \SDK \Metrics \MetricExporterInterface ;
1618use OpenTelemetry \SDK \Metrics \MetricReader \ExportingReader ;
@@ -28,6 +30,12 @@ class OpenTelemetry implements Adapter
2830{
2931 private MetricReaderInterface $ reader ;
3032 private MeterInterface $ meter ;
33+ private array $ meterStorage = [
34+ Counter::class => [],
35+ UpDownCounter::class => [],
36+ Histogram::class => [],
37+ Gauge::class => [],
38+ ];
3139
3240 public function __construct (string $ endpoint , string $ serviceNamespace , string $ serviceName , string $ serviceInstanceId )
3341 {
@@ -40,7 +48,7 @@ public function __construct(string $endpoint, string $serviceNamespace, string $
4048 $ this ->meter = $ this ->initMeter ($ exporter , $ attributes );
4149 }
4250
43- protected function initMeter (MetricExporterInterface $ exporter , Attributes $ attributes ): MeterInterface
51+ protected function initMeter (MetricExporterInterface $ exporter , AttributesInterface $ attributes ): MeterInterface
4452 {
4553 $ this ->reader = new ExportingReader ($ exporter );
4654 $ meterProvider = MeterProvider::builder ()
@@ -56,12 +64,21 @@ protected function initMeter(MetricExporterInterface $exporter, Attributes $attr
5664 protected function createExporter (string $ endpoint ): MetricExporterInterface
5765 {
5866 $ transport = (new OtlpHttpTransportFactory ())->create ($ endpoint , ContentTypes::PROTOBUF );
59- return new MetricExporter ($ transport );
67+ return new MetricExporter ($ transport , Temporality::CUMULATIVE );
68+ }
69+
70+ private function createMeter (string $ type , string $ name , callable $ creator ): mixed
71+ {
72+ if (!isset ($ this ->meterStorage [$ type ][$ name ])) {
73+ $ this ->meterStorage [$ type ][$ name ] = $ creator ();
74+ }
75+
76+ return $ this ->meterStorage [$ type ][$ name ];
6077 }
6178
6279 public function createCounter (string $ name , ?string $ unit = null , ?string $ description = null , array $ advisory = []): Counter
6380 {
64- $ counter = $ this ->meter ->createCounter ($ name , $ unit , $ description , $ advisory );
81+ $ counter = $ this ->createMeter (Counter::class, $ name , fn () => $ this -> meter ->createCounter ($ name , $ unit , $ description , $ advisory) );
6582 return new class ($ counter ) extends Counter {
6683 public function __construct (private CounterInterface $ counter )
6784 {
@@ -75,12 +92,12 @@ public function add(float|int $amount, iterable $attributes = []): void
7592
7693 public function createHistogram (string $ name , ?string $ unit = null , ?string $ description = null , array $ advisory = []): Histogram
7794 {
78- $ histogram = $ this ->meter ->createHistogram ($ name , $ unit , $ description , $ advisory );
95+ $ histogram = $ this ->createMeter (Histogram::class, $ name , fn () => $ this -> meter ->createHistogram ($ name , $ unit , $ description , $ advisory) );
7996 return new class ($ histogram ) extends Histogram {
8097 public function __construct (private HistogramInterface $ histogram )
8198 {
8299 }
83- public function add (float |int $ amount , iterable $ attributes = []): void
100+ public function record (float |int $ amount , iterable $ attributes = []): void
84101 {
85102 $ this ->histogram ->record ($ amount , $ attributes );
86103 }
@@ -89,12 +106,12 @@ public function add(float|int $amount, iterable $attributes = []): void
89106
90107 public function createGauge (string $ name , ?string $ unit = null , ?string $ description = null , array $ advisory = []): Gauge
91108 {
92- $ gauge = $ this ->meter ->createGauge ($ name , $ unit , $ description , $ advisory );
109+ $ gauge = $ this ->createMeter (Gauge::class, $ name , fn () => $ this -> meter ->createGauge ($ name , $ unit , $ description , $ advisory) );
93110 return new class ($ gauge ) extends Gauge {
94111 public function __construct (private GaugeInterface $ gauge )
95112 {
96113 }
97- public function add (float |int $ amount , iterable $ attributes = []): void
114+ public function record (float |int $ amount , iterable $ attributes = []): void
98115 {
99116 $ this ->gauge ->record ($ amount , $ attributes );
100117 }
@@ -103,7 +120,7 @@ public function add(float|int $amount, iterable $attributes = []): void
103120
104121 public function createUpDownCounter (string $ name , ?string $ unit = null , ?string $ description = null , array $ advisory = []): UpDownCounter
105122 {
106- $ upDownCounter = $ this ->meter ->createUpDownCounter ($ name , $ unit , $ description , $ advisory );
123+ $ upDownCounter = $ this ->createMeter (UpDownCounter::class, $ name , fn () => $ this -> meter ->createUpDownCounter ($ name , $ unit , $ description , $ advisory) );
107124 return new class ($ upDownCounter ) extends UpDownCounter {
108125 public function __construct (private UpDownCounterInterface $ upDownCounter )
109126 {
0 commit comments