|
| 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.pulsar.broker.stats; |
| 20 | + |
| 21 | +import io.opentelemetry.api.common.Attributes; |
| 22 | +import io.opentelemetry.api.metrics.BatchCallback; |
| 23 | +import io.opentelemetry.api.metrics.ObservableLongMeasurement; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.Optional; |
| 26 | +import org.apache.pulsar.broker.PulsarService; |
| 27 | +import org.apache.pulsar.broker.service.Consumer; |
| 28 | +import org.apache.pulsar.broker.service.Subscription; |
| 29 | +import org.apache.pulsar.broker.service.Topic; |
| 30 | +import org.apache.pulsar.common.naming.TopicName; |
| 31 | +import org.apache.pulsar.opentelemetry.OpenTelemetryAttributes; |
| 32 | + |
| 33 | +public class OpenTelemetryConsumerStats implements AutoCloseable { |
| 34 | + |
| 35 | + // Replaces pulsar_consumer_msg_rate_out |
| 36 | + public static final String MESSAGE_OUT_COUNTER = "pulsar.broker.consumer.message.outgoing.count"; |
| 37 | + private final ObservableLongMeasurement messageOutCounter; |
| 38 | + |
| 39 | + // Replaces pulsar_consumer_msg_throughput_out |
| 40 | + public static final String BYTES_OUT_COUNTER = "pulsar.broker.consumer.message.outgoing.size"; |
| 41 | + private final ObservableLongMeasurement bytesOutCounter; |
| 42 | + |
| 43 | + // Replaces pulsar_consumer_msg_ack_rate |
| 44 | + public static final String MESSAGE_ACK_COUNTER = "pulsar.broker.consumer.message.ack.count"; |
| 45 | + private final ObservableLongMeasurement messageAckCounter; |
| 46 | + |
| 47 | + // Replaces pulsar_consumer_msg_rate_redeliver |
| 48 | + public static final String MESSAGE_REDELIVER_COUNTER = "pulsar.broker.consumer.message.redeliver.count"; |
| 49 | + private final ObservableLongMeasurement messageRedeliverCounter; |
| 50 | + |
| 51 | + // Replaces pulsar_consumer_unacked_messages |
| 52 | + public static final String MESSAGE_UNACKNOWLEDGED_COUNTER = "pulsar.broker.consumer.message.unack.count"; |
| 53 | + private final ObservableLongMeasurement messageUnacknowledgedCounter; |
| 54 | + |
| 55 | + // Replaces pulsar_consumer_available_permits |
| 56 | + public static final String MESSAGE_PERMITS_COUNTER = "pulsar.broker.consumer.permit.count"; |
| 57 | + private final ObservableLongMeasurement messagePermitsCounter; |
| 58 | + |
| 59 | + private final BatchCallback batchCallback; |
| 60 | + |
| 61 | + public OpenTelemetryConsumerStats(PulsarService pulsar) { |
| 62 | + var meter = pulsar.getOpenTelemetry().getMeter(); |
| 63 | + |
| 64 | + messageOutCounter = meter |
| 65 | + .counterBuilder(MESSAGE_OUT_COUNTER) |
| 66 | + .setUnit("{message}") |
| 67 | + .setDescription("The total number of messages dispatched to this consumer.") |
| 68 | + .buildObserver(); |
| 69 | + |
| 70 | + bytesOutCounter = meter |
| 71 | + .counterBuilder(BYTES_OUT_COUNTER) |
| 72 | + .setUnit("By") |
| 73 | + .setDescription("The total number of messages bytes dispatched to this consumer.") |
| 74 | + .buildObserver(); |
| 75 | + |
| 76 | + messageAckCounter = meter |
| 77 | + .counterBuilder(MESSAGE_ACK_COUNTER) |
| 78 | + .setUnit("{ack}") |
| 79 | + .setDescription("The total number of message acknowledgments received from this consumer.") |
| 80 | + .buildObserver(); |
| 81 | + |
| 82 | + messageRedeliverCounter = meter |
| 83 | + .counterBuilder(MESSAGE_REDELIVER_COUNTER) |
| 84 | + .setUnit("{message}") |
| 85 | + .setDescription("The total number of messages that have been redelivered to this consumer.") |
| 86 | + .buildObserver(); |
| 87 | + |
| 88 | + messageUnacknowledgedCounter = meter |
| 89 | + .upDownCounterBuilder(MESSAGE_UNACKNOWLEDGED_COUNTER) |
| 90 | + .setUnit("{message}") |
| 91 | + .setDescription("The total number of messages unacknowledged by this consumer.") |
| 92 | + .buildObserver(); |
| 93 | + |
| 94 | + messagePermitsCounter = meter |
| 95 | + .upDownCounterBuilder(MESSAGE_PERMITS_COUNTER) |
| 96 | + .setUnit("{permit}") |
| 97 | + .setDescription("The number of permits currently available for this consumer.") |
| 98 | + .buildObserver(); |
| 99 | + |
| 100 | + batchCallback = meter.batchCallback(() -> pulsar.getBrokerService() |
| 101 | + .getTopics() |
| 102 | + .values() |
| 103 | + .stream() |
| 104 | + .map(topicFuture -> topicFuture.getNow(Optional.empty())) |
| 105 | + .filter(Optional::isPresent) |
| 106 | + .map(Optional::get) |
| 107 | + .map(Topic::getSubscriptions) |
| 108 | + .flatMap(s -> s.values().stream()) |
| 109 | + .map(Subscription::getConsumers) |
| 110 | + .flatMap(Collection::stream) |
| 111 | + .forEach(this::recordMetricsForConsumer), |
| 112 | + messageOutCounter, |
| 113 | + bytesOutCounter, |
| 114 | + messageAckCounter, |
| 115 | + messageRedeliverCounter, |
| 116 | + messageUnacknowledgedCounter, |
| 117 | + messagePermitsCounter); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void close() { |
| 122 | + batchCallback.close(); |
| 123 | + } |
| 124 | + |
| 125 | + private void recordMetricsForConsumer(Consumer consumer) { |
| 126 | + var subscription = consumer.getSubscription(); |
| 127 | + var topicName = TopicName.get(subscription.getTopic().getName()); |
| 128 | + |
| 129 | + var builder = Attributes.builder() |
| 130 | + .put(OpenTelemetryAttributes.PULSAR_CONSUMER_NAME, consumer.consumerName()) |
| 131 | + .put(OpenTelemetryAttributes.PULSAR_CONSUMER_ID, consumer.consumerId()) |
| 132 | + .put(OpenTelemetryAttributes.PULSAR_CONSUMER_CONNECTED_SINCE, |
| 133 | + consumer.getConnectedSince().getEpochSecond()) |
| 134 | + .put(OpenTelemetryAttributes.PULSAR_SUBSCRIPTION_NAME, subscription.getName()) |
| 135 | + .put(OpenTelemetryAttributes.PULSAR_SUBSCRIPTION_TYPE, consumer.subType().toString()) |
| 136 | + .put(OpenTelemetryAttributes.PULSAR_DOMAIN, topicName.getDomain().toString()) |
| 137 | + .put(OpenTelemetryAttributes.PULSAR_TENANT, topicName.getTenant()) |
| 138 | + .put(OpenTelemetryAttributes.PULSAR_NAMESPACE, topicName.getNamespace()) |
| 139 | + .put(OpenTelemetryAttributes.PULSAR_TOPIC, topicName.getPartitionedTopicName()); |
| 140 | + if (topicName.isPartitioned()) { |
| 141 | + builder.put(OpenTelemetryAttributes.PULSAR_PARTITION_INDEX, topicName.getPartitionIndex()); |
| 142 | + } |
| 143 | + var clientAddress = consumer.getClientAddressAndPort(); |
| 144 | + if (clientAddress != null) { |
| 145 | + builder.put(OpenTelemetryAttributes.PULSAR_CLIENT_ADDRESS, clientAddress); |
| 146 | + } |
| 147 | + var clientVersion = consumer.getClientVersion(); |
| 148 | + if (clientVersion != null) { |
| 149 | + builder.put(OpenTelemetryAttributes.PULSAR_CLIENT_VERSION, clientVersion); |
| 150 | + } |
| 151 | + var metadataList = consumer.getMetadata() |
| 152 | + .entrySet() |
| 153 | + .stream() |
| 154 | + .map(e -> String.format("%s:%s", e.getKey(), e.getValue())) |
| 155 | + .toList(); |
| 156 | + builder.put(OpenTelemetryAttributes.PULSAR_CONSUMER_METADATA, metadataList); |
| 157 | + var attributes = builder.build(); |
| 158 | + |
| 159 | + messageOutCounter.record(consumer.getMsgOutCounter(), attributes); |
| 160 | + bytesOutCounter.record(consumer.getBytesOutCounter(), attributes); |
| 161 | + messageAckCounter.record(consumer.getMessageAckCounter(), attributes); |
| 162 | + messageRedeliverCounter.record(consumer.getMessageRedeliverCounter(), attributes); |
| 163 | + messageUnacknowledgedCounter.record(consumer.getUnackedMessages(), |
| 164 | + Attributes.builder() |
| 165 | + .putAll(attributes) |
| 166 | + .put(OpenTelemetryAttributes.PULSAR_CONSUMER_BLOCKED, consumer.isBlocked()) |
| 167 | + .build()); |
| 168 | + messagePermitsCounter.record(consumer.getAvailablePermits(), attributes); |
| 169 | + } |
| 170 | +} |
0 commit comments