forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat][broker] PIP-264: Add OpenTelemetry producer metrics (apache#22882
- Loading branch information
1 parent
f122817
commit f83dbe9
Showing
10 changed files
with
348 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/OpenTelemetryProducerStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.broker.stats; | ||
|
||
import io.opentelemetry.api.metrics.BatchCallback; | ||
import io.opentelemetry.api.metrics.ObservableLongMeasurement; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.apache.pulsar.broker.PulsarService; | ||
import org.apache.pulsar.broker.service.Producer; | ||
import org.apache.pulsar.common.policies.data.stats.NonPersistentPublisherStatsImpl; | ||
|
||
public class OpenTelemetryProducerStats implements AutoCloseable { | ||
|
||
// Replaces pulsar_producer_msg_rate_in | ||
public static final String MESSAGE_IN_COUNTER = "pulsar.broker.producer.message.incoming.count"; | ||
private final ObservableLongMeasurement messageInCounter; | ||
|
||
// Replaces pulsar_producer_msg_throughput_in | ||
public static final String BYTES_IN_COUNTER = "pulsar.broker.producer.message.incoming.size"; | ||
private final ObservableLongMeasurement bytesInCounter; | ||
|
||
public static final String MESSAGE_DROP_COUNTER = "pulsar.broker.producer.message.drop.count"; | ||
private final ObservableLongMeasurement messageDropCounter; | ||
|
||
private final BatchCallback batchCallback; | ||
|
||
public OpenTelemetryProducerStats(PulsarService pulsar) { | ||
var meter = pulsar.getOpenTelemetry().getMeter(); | ||
|
||
messageInCounter = meter | ||
.counterBuilder(MESSAGE_IN_COUNTER) | ||
.setUnit("{message}") | ||
.setDescription("The total number of messages received from this producer.") | ||
.buildObserver(); | ||
|
||
bytesInCounter = meter | ||
.counterBuilder(BYTES_IN_COUNTER) | ||
.setUnit("By") | ||
.setDescription("The total number of messages bytes received from this producer.") | ||
.buildObserver(); | ||
|
||
messageDropCounter = meter | ||
.counterBuilder(MESSAGE_DROP_COUNTER) | ||
.setUnit("{message}") | ||
.setDescription("The total number of messages dropped from this producer.") | ||
.buildObserver(); | ||
|
||
batchCallback = meter.batchCallback(() -> pulsar.getBrokerService() | ||
.getTopics() | ||
.values() | ||
.stream() | ||
.filter(future -> future.isDone() && !future.isCompletedExceptionally()) | ||
.map(CompletableFuture::join) | ||
.filter(Optional::isPresent) | ||
.flatMap(topic -> topic.get().getProducers().values().stream()) | ||
.forEach(this::recordMetricsForProducer), | ||
messageInCounter, | ||
bytesInCounter, | ||
messageDropCounter); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
batchCallback.close(); | ||
} | ||
|
||
private void recordMetricsForProducer(Producer producer) { | ||
var attributes = producer.getOpenTelemetryAttributes(); | ||
var stats = producer.getStats(); | ||
|
||
messageInCounter.record(stats.getMsgInCounter(), attributes); | ||
bytesInCounter.record(stats.getBytesInCounter(), attributes); | ||
|
||
if (stats instanceof NonPersistentPublisherStatsImpl nonPersistentStats) { | ||
messageDropCounter.record(nonPersistentStats.getMsgDropCount(), attributes); | ||
} | ||
} | ||
} |
Oops, something went wrong.