Skip to content

Commit f5d7666

Browse files
committed
Prototyping of OpenTelemetry metric plugin
1 parent 2188ca2 commit f5d7666

File tree

21 files changed

+1127
-12
lines changed

21 files changed

+1127
-12
lines changed

pinot-common/src/main/java/org/apache/pinot/common/metrics/AbstractMetrics.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ public void addCallbackGaugeIfNeeded(final String metricName, final Callable<Lon
603603
public void addCallbackGauge(final String metricName, final Callable<Long> valueCallback) {
604604
PinotMetricUtils
605605
.makeGauge(_metricsRegistry, PinotMetricUtils.makePinotMetricName(_clazz, _metricPrefix + metricName),
606-
PinotMetricUtils.makePinotGauge(avoid -> {
606+
PinotMetricUtils.makePinotGauge(_metricPrefix + metricName, avoid -> {
607607
try {
608608
return valueCallback.call();
609609
} catch (Exception e) {
@@ -680,7 +680,7 @@ public void setOrUpdateTableGauge(final String tableName, final G gauge,
680680
public void setOrUpdateGauge(final String metricName, long value) {
681681
PinotGauge<Long> pinotGauge = PinotMetricUtils.makeGauge(_metricsRegistry,
682682
PinotMetricUtils.makePinotMetricName(_clazz, _metricPrefix + metricName),
683-
PinotMetricUtils.makePinotGauge(avoid -> value));
683+
PinotMetricUtils.makePinotGauge(_metricPrefix + metricName, avoid -> value));
684684
pinotGauge.setValue(value);
685685
}
686686

@@ -694,7 +694,7 @@ public void setOrUpdateGauge(final String metricName, long value) {
694694
public void setOrUpdateGauge(final String metricName, final Supplier<Long> valueSupplier) {
695695
PinotGauge<Long> pinotGauge = PinotMetricUtils.makeGauge(_metricsRegistry,
696696
PinotMetricUtils.makePinotMetricName(_clazz, _metricPrefix + metricName),
697-
PinotMetricUtils.makePinotGauge(avoid -> valueSupplier.get()));
697+
PinotMetricUtils.makePinotGauge(_metricPrefix + metricName, avoid -> valueSupplier.get()));
698698
pinotGauge.setValueSupplier(valueSupplier);
699699
}
700700

@@ -704,7 +704,7 @@ public void setOrUpdateGauge(final String metricName, final Supplier<Long> value
704704
public void setOrUpdateGauge(final String metricName, final LongSupplier valueSupplier) {
705705
PinotGauge<Long> pinotGauge = PinotMetricUtils.makeGauge(_metricsRegistry,
706706
PinotMetricUtils.makePinotMetricName(_clazz, _metricPrefix + metricName),
707-
PinotMetricUtils.makePinotGauge(avoid -> valueSupplier.getAsLong()));
707+
PinotMetricUtils.makePinotGauge(_metricPrefix + metricName, avoid -> valueSupplier.getAsLong()));
708708
pinotGauge.setValueSupplier((Supplier<Long>) () -> (Long) valueSupplier.getAsLong());
709709
}
710710

pinot-common/src/main/java/org/apache/pinot/common/metrics/ValidationMetrics.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Long value() {
5757

5858
@Override
5959
public Object getGauge() {
60-
return PinotMetricUtils.makePinotGauge(avoid -> value()).getGauge();
60+
return PinotMetricUtils.makePinotGauge(_key, avoid -> value()).getGauge();
6161
}
6262

6363
@Override
@@ -92,10 +92,9 @@ public Double value() {
9292
public Object getMetric() {
9393
return getGauge();
9494
}
95-
9695
@Override
9796
public Object getGauge() {
98-
return PinotMetricUtils.makePinotGauge(avoid -> value()).getGauge();
97+
return PinotMetricUtils.makePinotGauge(_key, avoid -> value()).getGauge();
9998
}
10099
}
101100

pinot-plugins/pinot-metrics/pinot-compound-metrics/src/main/java/org/apache/pinot/plugin/metrics/compound/CompoundPinotMetricRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public <T> PinotGauge<T> newGauge(PinotMetricName name, PinotGauge<T> gauge) {
7272
return (PinotGauge<T>) _allMetrics.computeIfAbsent(name,
7373
key -> new CompoundPinotGauge<>(map(key, (reg, subName) -> reg.newGauge(subName, null))));
7474
} else {
75-
CompoundPinotGauge<T> compoundGauge =
76-
(CompoundPinotGauge<T>) PinotMetricUtils.makePinotGauge(avoid -> gauge.value());
75+
CompoundPinotGauge<T> compoundGauge = (CompoundPinotGauge<T>) PinotMetricUtils
76+
.makePinotGauge(name.getMetricName().toString(), avoid -> gauge.value());
7777

7878
Function<PinotMetricName, CompoundPinotGauge<?>> creator = key -> {
7979
CompoundPinotMetricName compoundName = (CompoundPinotMetricName) key;

pinot-plugins/pinot-metrics/pinot-compound-metrics/src/main/java/org/apache/pinot/plugin/metrics/compound/CompoundPinotMetricsFactory.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,22 @@ public PinotMetricName makePinotMetricName(Class<?> klass, String name) {
144144
}
145145

146146
@Override
147+
@Deprecated
147148
public <T> PinotGauge<T> makePinotGauge(Function<Void, T> condition) {
148149
List<PinotGauge<T>> gauges = _factories.stream()
149150
.map(factory -> factory.makePinotGauge(condition))
150151
.collect(Collectors.toList());
151152
return new CompoundPinotGauge<T>(gauges);
152153
}
153154

155+
@Override
156+
public <T> PinotGauge<T> makePinotGauge(String metricName, Function<Void, T> condition) {
157+
List<PinotGauge<T>> gauges = _factories.stream()
158+
.map(factory -> factory.makePinotGauge(metricName, condition))
159+
.collect(Collectors.toList());
160+
return new CompoundPinotGauge<T>(gauges);
161+
}
162+
154163
@Override
155164
public PinotJmxReporter makePinotJmxReporter(PinotMetricsRegistry metricsRegistry) {
156165
CompoundPinotMetricRegistry registry = (CompoundPinotMetricRegistry) metricsRegistry;

pinot-plugins/pinot-metrics/pinot-dropwizard/src/main/java/org/apache/pinot/plugin/metrics/dropwizard/DropwizardMetricsFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,16 @@ public PinotMetricName makePinotMetricName(Class<?> klass, String name) {
5757
}
5858

5959
@Override
60+
@Deprecated
6061
public <T> PinotGauge<T> makePinotGauge(Function<Void, T> condition) {
6162
return new DropwizardGauge<T>(condition);
6263
}
6364

65+
@Override
66+
public <T> PinotGauge<T> makePinotGauge(String metricName, Function<Void, T> condition) {
67+
return new DropwizardGauge<T>(condition);
68+
}
69+
6470
@Override
6571
public PinotJmxReporter makePinotJmxReporter(PinotMetricsRegistry metricsRegistry) {
6672
return new DropwizardJmxReporter(metricsRegistry, _domainName);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
21+
-->
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
<parent>
25+
<artifactId>pinot-metrics</artifactId>
26+
<groupId>org.apache.pinot</groupId>
27+
<version>1.4.0-SNAPSHOT</version>
28+
</parent>
29+
30+
<artifactId>pinot-open-telemetry</artifactId>
31+
<name>Pinot OpenTelemetry Metrics</name>
32+
<url>https://pinot.apache.org/</url>
33+
<properties>
34+
<pinot.root>${basedir}/../../..</pinot.root>
35+
<shade.phase.prop>package</shade.phase.prop>
36+
</properties>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>com.google.auto.service</groupId>
41+
<artifactId>auto-service-annotations</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>io.opentelemetry</groupId>
45+
<artifactId>opentelemetry-api</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>io.opentelemetry</groupId>
49+
<artifactId>opentelemetry-sdk</artifactId>
50+
</dependency>
51+
<dependency>
52+
<groupId>io.opentelemetry</groupId>
53+
<artifactId>opentelemetry-exporter-common</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>io.opentelemetry</groupId>
57+
<artifactId>opentelemetry-exporter-logging</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>io.opentelemetry</groupId>
61+
<artifactId>opentelemetry-exporter-otlp</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.apache.pinot</groupId>
65+
<artifactId>pinot-common</artifactId>
66+
</dependency>
67+
</dependencies>
68+
69+
<profiles>
70+
<profile>
71+
<id>pinot-fastdev</id>
72+
<properties>
73+
<shade.phase.prop>none</shade.phase.prop>
74+
</properties>
75+
</profile>
76+
</profiles>
77+
78+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.pinot.plugin.metrics.opentelemetry;
20+
21+
import io.opentelemetry.api.metrics.LongCounter;
22+
import org.apache.pinot.spi.metrics.PinotCounter;
23+
24+
25+
public class OpenTelemetryCounter implements PinotCounter {
26+
private final OpenTelemetryMetricName _metricName;
27+
private final LongCounter _counter;
28+
29+
public OpenTelemetryCounter(OpenTelemetryMetricName metricName) {
30+
_metricName = metricName;
31+
_counter = OpenTelemetryMetricsRegistry.OTEL_METER_PROVIDER.counterBuilder(metricName.getOtelMetricName()).build();
32+
}
33+
34+
@Override
35+
public Object getCounter() {
36+
return _counter;
37+
}
38+
39+
@Override
40+
public Object getMetric() {
41+
return _counter;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.pinot.plugin.metrics.opentelemetry;
20+
21+
import io.opentelemetry.api.metrics.DoubleGauge;
22+
import io.opentelemetry.api.metrics.LongGauge;
23+
import java.util.function.DoubleSupplier;
24+
import java.util.function.Function;
25+
import java.util.function.LongSupplier;
26+
import java.util.function.Supplier;
27+
import org.apache.pinot.spi.metrics.PinotGauge;
28+
29+
30+
public class OpenTelemetryGauge<T> implements PinotGauge<T> {
31+
private OpenTelemetryMetricName _metricName;
32+
private LongGauge _longGauge;
33+
private DoubleGauge _doubleGauge;
34+
private Supplier<T> _valueSupplier;
35+
36+
public OpenTelemetryGauge(OpenTelemetryMetricName metricName, Function<Void, T> valueSupplier) {
37+
if (valueSupplier instanceof LongSupplier) {
38+
_longGauge = OpenTelemetryMetricsRegistry.OTEL_METER_PROVIDER
39+
.gaugeBuilder(metricName.getOtelMetricName())
40+
.ofLongs()
41+
.build();
42+
} else if (valueSupplier instanceof DoubleSupplier) {
43+
_doubleGauge = OpenTelemetryMetricsRegistry.OTEL_METER_PROVIDER
44+
.gaugeBuilder(metricName.getOtelMetricName())
45+
.build();
46+
} else {
47+
throw new IllegalArgumentException("Unsupported value supplier type: " + _valueSupplier.getClass());
48+
}
49+
_metricName = metricName;
50+
setValueSupplier(() -> valueSupplier.apply(null));
51+
}
52+
53+
@Override
54+
public Object getGauge() {
55+
return _longGauge != null ? _longGauge : _doubleGauge;
56+
}
57+
58+
@Override
59+
public Object getMetric() {
60+
return _longGauge != null ? _longGauge : _doubleGauge;
61+
}
62+
63+
@Override
64+
public T value() {
65+
return _valueSupplier.get();
66+
}
67+
68+
@Override
69+
public void setValue(T value) {
70+
if (_longGauge != null && value instanceof Long) {
71+
_longGauge.set((Long) value, _metricName.getOtelAttributes());
72+
} else if (_doubleGauge != null && value instanceof Double) {
73+
_doubleGauge.set((Double) value, _metricName.getOtelAttributes());
74+
} else {
75+
throw new IllegalArgumentException(
76+
String.format("Value type %s does not match gauge type %s", value.getClass().getSimpleName(),
77+
_longGauge != null ? "LongGauge" : "DoubleGauge")
78+
);
79+
}
80+
_valueSupplier = () -> value;
81+
}
82+
83+
@Override
84+
public void setValueSupplier(Supplier<T> valueSupplier) {
85+
_valueSupplier = valueSupplier;
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.pinot.plugin.metrics.opentelemetry;
20+
21+
import io.opentelemetry.api.metrics.DoubleHistogram;
22+
import org.apache.pinot.spi.metrics.PinotHistogram;
23+
24+
25+
public class OpenTelemetryHistogram implements PinotHistogram {
26+
private final OpenTelemetryMetricName _metricName;
27+
private final DoubleHistogram _histogram;
28+
29+
public OpenTelemetryHistogram(OpenTelemetryMetricName metricName) {
30+
_metricName = metricName;
31+
_histogram = OpenTelemetryMetricsRegistry.OTEL_METER_PROVIDER
32+
.histogramBuilder(metricName.getOtelMetricName())
33+
.build();
34+
}
35+
36+
@Override
37+
public Object getHistogram() {
38+
return _histogram;
39+
}
40+
41+
@Override
42+
public Object getMetric() {
43+
return _histogram;
44+
}
45+
}

0 commit comments

Comments
 (0)