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 transaction metrics (apache#22970)
- Loading branch information
1 parent
4ac9bc4
commit 4e535cb
Showing
25 changed files
with
678 additions
and
43 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
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
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
87 changes: 87 additions & 0 deletions
87
...rc/main/java/org/apache/pulsar/broker/stats/OpenTelemetryTransactionCoordinatorStats.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,87 @@ | ||
/* | ||
* 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 org.apache.pulsar.broker.PulsarService; | ||
import org.apache.pulsar.transaction.coordinator.TransactionMetadataStore; | ||
|
||
public class OpenTelemetryTransactionCoordinatorStats implements AutoCloseable { | ||
|
||
// Replaces ['pulsar_txn_aborted_total', | ||
// 'pulsar_txn_committed_total', | ||
// 'pulsar_txn_created_total', | ||
// 'pulsar_txn_timeout_total', | ||
// 'pulsar_txn_active_count'] | ||
public static final String TRANSACTION_COUNTER = "pulsar.broker.transaction.coordinator.transaction.count"; | ||
private final ObservableLongMeasurement transactionCounter; | ||
|
||
// Replaces pulsar_txn_append_log_total | ||
public static final String APPEND_LOG_COUNTER = "pulsar.broker.transaction.coordinator.append.log.count"; | ||
private final ObservableLongMeasurement appendLogCounter; | ||
|
||
private final BatchCallback batchCallback; | ||
|
||
public OpenTelemetryTransactionCoordinatorStats(PulsarService pulsar) { | ||
var meter = pulsar.getOpenTelemetry().getMeter(); | ||
|
||
transactionCounter = meter | ||
.upDownCounterBuilder(TRANSACTION_COUNTER) | ||
.setUnit("{transaction}") | ||
.setDescription("The number of transactions handled by the coordinator.") | ||
.buildObserver(); | ||
|
||
appendLogCounter = meter | ||
.counterBuilder(APPEND_LOG_COUNTER) | ||
.setUnit("{entry}") | ||
.setDescription("The number of transaction metadata entries appended by the coordinator.") | ||
.buildObserver(); | ||
|
||
batchCallback = meter.batchCallback(() -> { | ||
var transactionMetadataStoreService = pulsar.getTransactionMetadataStoreService(); | ||
// Avoid NPE during Pulsar shutdown. | ||
if (transactionMetadataStoreService != null) { | ||
transactionMetadataStoreService.getStores() | ||
.values() | ||
.forEach(this::recordMetricsForTransactionMetadataStore); | ||
} | ||
}, | ||
transactionCounter, | ||
appendLogCounter); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
batchCallback.close(); | ||
} | ||
|
||
private void recordMetricsForTransactionMetadataStore(TransactionMetadataStore transactionMetadataStore) { | ||
var attributes = transactionMetadataStore.getAttributes(); | ||
var stats = transactionMetadataStore.getMetadataStoreStats(); | ||
|
||
transactionCounter.record(stats.getAbortedCount(), attributes.getTxnAbortedAttributes()); | ||
transactionCounter.record(stats.getActives(), attributes.getTxnActiveAttributes()); | ||
transactionCounter.record(stats.getCommittedCount(), attributes.getTxnCommittedAttributes()); | ||
transactionCounter.record(stats.getCreatedCount(), attributes.getTxnCreatedAttributes()); | ||
transactionCounter.record(stats.getTimeoutCount(), attributes.getTxnTimeoutAttributes()); | ||
|
||
appendLogCounter.record(stats.getAppendLogCount(), attributes.getCommonAttributes()); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...ain/java/org/apache/pulsar/broker/stats/OpenTelemetryTransactionPendingAckStoreStats.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,72 @@ | ||
/* | ||
* 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.ObservableLongCounter; | ||
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.Subscription; | ||
import org.apache.pulsar.broker.service.Topic; | ||
import org.apache.pulsar.broker.service.persistent.PersistentSubscription; | ||
|
||
public class OpenTelemetryTransactionPendingAckStoreStats implements AutoCloseable { | ||
|
||
// Replaces ['pulsar_txn_tp_committed_count_total', 'pulsar_txn_tp_aborted_count_total'] | ||
public static final String ACK_COUNTER = "pulsar.broker.transaction.pending.ack.store.transaction.count"; | ||
private final ObservableLongCounter ackCounter; | ||
|
||
public OpenTelemetryTransactionPendingAckStoreStats(PulsarService pulsar) { | ||
var meter = pulsar.getOpenTelemetry().getMeter(); | ||
|
||
ackCounter = meter | ||
.counterBuilder(ACK_COUNTER) | ||
.setUnit("{transaction}") | ||
.setDescription("The number of transactions handled by the persistent ack store.") | ||
.buildWithCallback(measurement -> pulsar.getBrokerService() | ||
.getTopics() | ||
.values() | ||
.stream() | ||
.filter(topicFuture -> topicFuture.isDone() && !topicFuture.isCompletedExceptionally()) | ||
.map(CompletableFuture::join) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.filter(Topic::isPersistent) | ||
.map(Topic::getSubscriptions) | ||
.forEach(subs -> subs.forEach((__, sub) -> recordMetricsForSubscription(measurement, sub)))); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
ackCounter.close(); | ||
} | ||
|
||
private void recordMetricsForSubscription(ObservableLongMeasurement measurement, Subscription subscription) { | ||
assert subscription instanceof PersistentSubscription; // The topics have already been filtered for persistence. | ||
var stats = ((PersistentSubscription) subscription).getPendingAckHandle().getPendingAckHandleStats(); | ||
if (stats != null) { | ||
var attributes = stats.getAttributes(); | ||
measurement.record(stats.getCommitSuccessCount(), attributes.getCommitSuccessAttributes()); | ||
measurement.record(stats.getCommitFailedCount(), attributes.getCommitFailureAttributes()); | ||
measurement.record(stats.getAbortSuccessCount(), attributes.getAbortSuccessAttributes()); | ||
measurement.record(stats.getAbortFailedCount(), attributes.getAbortFailureAttributes()); | ||
} | ||
} | ||
} |
Oops, something went wrong.