|
15 | 15 | */
|
16 | 16 | package com.couchbase.client.core.transaction.context;
|
17 | 17 |
|
| 18 | +import com.couchbase.client.core.Core; |
18 | 19 | import com.couchbase.client.core.annotation.Stability;
|
19 | 20 | import com.couchbase.client.core.cnc.Counter;
|
20 | 21 | import com.couchbase.client.core.cnc.Meter;
|
21 | 22 | import com.couchbase.client.core.cnc.TracingIdentifiers;
|
22 |
| -import com.couchbase.client.core.cnc.ValueRecorder; |
| 23 | +import com.couchbase.client.core.config.ClusterConfig; |
| 24 | +import com.couchbase.client.core.topology.ClusterIdentifier; |
| 25 | +import com.couchbase.client.core.topology.ClusterIdentifierUtil; |
23 | 26 | import com.couchbase.client.core.util.CbCollections;
|
| 27 | +import reactor.util.annotation.Nullable; |
24 | 28 |
|
25 | 29 | import java.util.HashMap;
|
26 | 30 | import java.util.Map;
|
| 31 | +import java.util.Objects; |
| 32 | +import java.util.concurrent.ConcurrentHashMap; |
27 | 33 |
|
28 | 34 | import static com.couchbase.client.core.cnc.TracingIdentifiers.METER_TRANSACTION_ATTEMPTS;
|
29 | 35 | import static com.couchbase.client.core.cnc.TracingIdentifiers.METER_TRANSACTION_TOTAL;
|
30 | 36 | import static com.couchbase.client.core.cnc.TracingIdentifiers.SERVICE_TRANSACTIONS;
|
31 | 37 |
|
32 | 38 | @Stability.Internal
|
33 | 39 | public class CoreTransactionsCounters {
|
34 |
| - private final Counter transactions; |
35 |
| - private final Counter attempts; |
36 |
| - // A histogram of transaction durations |
37 |
| - |
38 |
| - public CoreTransactionsCounters(Meter meter) { |
39 |
| - Map<String, String> tags = CbCollections.mapOf(TracingIdentifiers.ATTR_SERVICE, SERVICE_TRANSACTIONS); |
40 |
| - transactions = meter.counter(METER_TRANSACTION_TOTAL, tags); |
41 |
| - attempts = meter.counter(METER_TRANSACTION_ATTEMPTS, tags); |
42 |
| - } |
43 |
| - |
44 |
| - public Counter attempts() { |
45 |
| - return attempts; |
46 |
| - } |
47 |
| - |
48 |
| - public Counter transactions() { |
49 |
| - return transactions; |
50 |
| - } |
| 40 | + @Stability.Internal |
| 41 | + public static class TransactionMetricIdentifier { |
| 42 | + |
| 43 | + private final @Nullable String clusterName; |
| 44 | + private final @Nullable String clusterUuid; |
| 45 | + |
| 46 | + TransactionMetricIdentifier(@Nullable ClusterIdentifier clusterIdent) { |
| 47 | + clusterName = clusterIdent == null ? null : clusterIdent.clusterName(); |
| 48 | + clusterUuid = clusterIdent == null ? null : clusterIdent.clusterUuid(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean equals(Object o) { |
| 53 | + if (this == o) return true; |
| 54 | + if (o == null || getClass() != o.getClass()) return false; |
| 55 | + TransactionMetricIdentifier that = (TransactionMetricIdentifier) o; |
| 56 | + return Objects.equals(clusterName, that.clusterName) |
| 57 | + && Objects.equals(clusterUuid, that.clusterUuid); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public int hashCode() { |
| 62 | + return Objects.hash(clusterName, clusterUuid); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private final Map<TransactionMetricIdentifier, Counter> transactionsMetrics = new ConcurrentHashMap<>(); |
| 67 | + private final Map<TransactionMetricIdentifier, Counter> attemptMetrics = new ConcurrentHashMap<>(); |
| 68 | + private final Core core; |
| 69 | + private final Meter meter; |
| 70 | + |
| 71 | + public CoreTransactionsCounters(Core core, Meter meter) { |
| 72 | + this.core = core; |
| 73 | + this.meter = meter; |
| 74 | + } |
| 75 | + |
| 76 | + public Counter attempts() { |
| 77 | + return genericCounter(METER_TRANSACTION_ATTEMPTS, attemptMetrics); |
| 78 | + } |
| 79 | + |
| 80 | + public Counter transactions() { |
| 81 | + return genericCounter(METER_TRANSACTION_TOTAL, transactionsMetrics); |
| 82 | + } |
| 83 | + |
| 84 | + private Counter genericCounter(String name, Map<TransactionMetricIdentifier, Counter> metricsMap) { |
| 85 | + ClusterConfig config = core.configurationProvider().config(); |
| 86 | + ClusterIdentifier clusterIdent = ClusterIdentifierUtil.fromConfig(config); |
| 87 | + return metricsMap.computeIfAbsent(new TransactionMetricIdentifier(clusterIdent), id -> { |
| 88 | + HashMap<String, String> tags = new HashMap<>(); |
| 89 | + tags.put(TracingIdentifiers.ATTR_SYSTEM, TracingIdentifiers.ATTR_SYSTEM_COUCHBASE); |
| 90 | + if (id.clusterName != null) { |
| 91 | + tags.put(TracingIdentifiers.ATTR_CLUSTER_NAME, id.clusterName); |
| 92 | + } |
| 93 | + if (id.clusterUuid != null) { |
| 94 | + tags.put(TracingIdentifiers.ATTR_CLUSTER_UUID, id.clusterUuid); |
| 95 | + } |
| 96 | + return meter.counter(name, tags); |
| 97 | + }); |
| 98 | + } |
51 | 99 | }
|
0 commit comments