|
| 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.bookkeeper.mledger.impl; |
| 20 | + |
| 21 | +import com.google.common.collect.Streams; |
| 22 | +import io.opentelemetry.api.OpenTelemetry; |
| 23 | +import io.opentelemetry.api.metrics.BatchCallback; |
| 24 | +import io.opentelemetry.api.metrics.ObservableLongMeasurement; |
| 25 | +import org.apache.bookkeeper.mledger.ManagedCursor; |
| 26 | +import org.apache.pulsar.opentelemetry.Constants; |
| 27 | + |
| 28 | +public class OpenTelemetryManagedCursorStats implements AutoCloseable { |
| 29 | + |
| 30 | + // Replaces ['pulsar_ml_cursor_persistLedgerSucceed', 'pulsar_ml_cursor_persistLedgerErrors'] |
| 31 | + public static final String PERSIST_OPERATION_COUNTER = "pulsar.broker.managed_ledger.persist.operation.count"; |
| 32 | + private final ObservableLongMeasurement persistOperationCounter; |
| 33 | + |
| 34 | + // Replaces ['pulsar_ml_cursor_persistZookeeperSucceed', 'pulsar_ml_cursor_persistZookeeperErrors'] |
| 35 | + public static final String PERSIST_OPERATION_METADATA_STORE_COUNTER = |
| 36 | + "pulsar.broker.managed_ledger.persist.mds.operation.count"; |
| 37 | + private final ObservableLongMeasurement persistOperationMetadataStoreCounter; |
| 38 | + |
| 39 | + // Replaces pulsar_ml_cursor_nonContiguousDeletedMessagesRange |
| 40 | + public static final String NON_CONTIGUOUS_MESSAGE_RANGE_COUNTER = |
| 41 | + "pulsar.broker.managed_ledger.message_range.count"; |
| 42 | + private final ObservableLongMeasurement nonContiguousMessageRangeCounter; |
| 43 | + |
| 44 | + // Replaces pulsar_ml_cursor_writeLedgerSize |
| 45 | + public static final String OUTGOING_BYTE_COUNTER = "pulsar.broker.managed_ledger.cursor.outgoing.size"; |
| 46 | + private final ObservableLongMeasurement outgoingByteCounter; |
| 47 | + |
| 48 | + // Replaces pulsar_ml_cursor_writeLedgerLogicalSize |
| 49 | + public static final String OUTGOING_BYTE_LOGICAL_COUNTER = |
| 50 | + "pulsar.broker.managed_ledger.cursor.outgoing.logical.size"; |
| 51 | + private final ObservableLongMeasurement outgoingByteLogicalCounter; |
| 52 | + |
| 53 | + // Replaces pulsar_ml_cursor_readLedgerSize |
| 54 | + public static final String INCOMING_BYTE_COUNTER = "pulsar.broker.managed_ledger.cursor.incoming.size"; |
| 55 | + private final ObservableLongMeasurement incomingByteCounter; |
| 56 | + |
| 57 | + private final BatchCallback batchCallback; |
| 58 | + |
| 59 | + public OpenTelemetryManagedCursorStats(OpenTelemetry openTelemetry, ManagedLedgerFactoryImpl factory) { |
| 60 | + var meter = openTelemetry.getMeter(Constants.BROKER_INSTRUMENTATION_SCOPE_NAME); |
| 61 | + |
| 62 | + persistOperationCounter = meter |
| 63 | + .counterBuilder(PERSIST_OPERATION_COUNTER) |
| 64 | + .setUnit("{operation}") |
| 65 | + .setDescription("The number of acknowledgment operations on the ledger.") |
| 66 | + .buildObserver(); |
| 67 | + |
| 68 | + persistOperationMetadataStoreCounter = meter |
| 69 | + .counterBuilder(PERSIST_OPERATION_METADATA_STORE_COUNTER) |
| 70 | + .setUnit("{operation}") |
| 71 | + .setDescription("The number of acknowledgment operations in the metadata store.") |
| 72 | + .buildObserver(); |
| 73 | + |
| 74 | + nonContiguousMessageRangeCounter = meter |
| 75 | + .upDownCounterBuilder(NON_CONTIGUOUS_MESSAGE_RANGE_COUNTER) |
| 76 | + .setUnit("{range}") |
| 77 | + .setDescription("The number of non-contiguous deleted messages ranges.") |
| 78 | + .buildObserver(); |
| 79 | + |
| 80 | + outgoingByteCounter = meter |
| 81 | + .counterBuilder(OUTGOING_BYTE_COUNTER) |
| 82 | + .setUnit("{By}") |
| 83 | + .setDescription("The total amount of data written to the ledger.") |
| 84 | + .buildObserver(); |
| 85 | + |
| 86 | + outgoingByteLogicalCounter = meter |
| 87 | + .counterBuilder(OUTGOING_BYTE_LOGICAL_COUNTER) |
| 88 | + .setUnit("{By}") |
| 89 | + .setDescription("The total amount of data written to the ledger, not including replicas.") |
| 90 | + .buildObserver(); |
| 91 | + |
| 92 | + incomingByteCounter = meter |
| 93 | + .counterBuilder(INCOMING_BYTE_COUNTER) |
| 94 | + .setUnit("{By}") |
| 95 | + .setDescription("The total amount of data read from the ledger.") |
| 96 | + .buildObserver(); |
| 97 | + |
| 98 | + batchCallback = meter.batchCallback(() -> factory.getManagedLedgers() |
| 99 | + .values() |
| 100 | + .stream() |
| 101 | + .map(ManagedLedgerImpl::getCursors) |
| 102 | + .flatMap(Streams::stream) |
| 103 | + .forEach(this::recordMetrics), |
| 104 | + persistOperationCounter, |
| 105 | + persistOperationMetadataStoreCounter, |
| 106 | + nonContiguousMessageRangeCounter, |
| 107 | + outgoingByteCounter, |
| 108 | + outgoingByteLogicalCounter, |
| 109 | + incomingByteCounter); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void close() { |
| 114 | + batchCallback.close(); |
| 115 | + } |
| 116 | + |
| 117 | + private void recordMetrics(ManagedCursor cursor) { |
| 118 | + var stats = cursor.getStats(); |
| 119 | + var cursorAttributesSet = cursor.getManagedCursorAttributes(); |
| 120 | + var attributes = cursorAttributesSet.getAttributes(); |
| 121 | + var attributesSucceed = cursorAttributesSet.getAttributesOperationSucceed(); |
| 122 | + var attributesFailed = cursorAttributesSet.getAttributesOperationFailure(); |
| 123 | + |
| 124 | + persistOperationCounter.record(stats.getPersistLedgerSucceed(), attributesSucceed); |
| 125 | + persistOperationCounter.record(stats.getPersistLedgerErrors(), attributesFailed); |
| 126 | + |
| 127 | + persistOperationMetadataStoreCounter.record(stats.getPersistZookeeperSucceed(), attributesSucceed); |
| 128 | + persistOperationMetadataStoreCounter.record(stats.getPersistZookeeperErrors(), attributesFailed); |
| 129 | + |
| 130 | + nonContiguousMessageRangeCounter.record(cursor.getTotalNonContiguousDeletedMessagesRange(), attributes); |
| 131 | + |
| 132 | + outgoingByteCounter.record(stats.getWriteCursorLedgerSize(), attributes); |
| 133 | + outgoingByteLogicalCounter.record(stats.getWriteCursorLedgerLogicalSize(), attributes); |
| 134 | + incomingByteCounter.record(stats.getReadCursorLedgerSize(), attributes); |
| 135 | + } |
| 136 | +} |
0 commit comments