Skip to content

Commit a8b506e

Browse files
committed
JVMCBC-1560: Add cluster UUID and name to metrics and spans (6/6)
Adding cluster UUID and name attributes for transactional metrics. Change-Id: Ic195c68a884d1b6b52eabb04c16b50c0da206fc9 Reviewed-on: https://review.couchbase.org/c/couchbase-jvm-clients/+/217018 Tested-by: Build Bot <[email protected]> Reviewed-by: Graham Pople <[email protected]>
1 parent 448eda1 commit a8b506e

File tree

4 files changed

+99
-22
lines changed

4 files changed

+99
-22
lines changed

core-io/src/main/java/com/couchbase/client/core/Core.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
import com.couchbase.client.core.service.ServiceState;
9595
import com.couchbase.client.core.service.ServiceType;
9696
import com.couchbase.client.core.topology.ClusterIdentifier;
97+
import com.couchbase.client.core.topology.ClusterIdentifierUtil;
9798
import com.couchbase.client.core.topology.ClusterTopology;
9899
import com.couchbase.client.core.topology.ClusterTopologyWithBucket;
99100
import com.couchbase.client.core.topology.NodeIdentifier;
@@ -348,7 +349,7 @@ public RequestTracer requestTracer() {
348349
);
349350

350351
this.transactionsCleanup = new CoreTransactionsCleanup(this, environment.transactionsConfig());
351-
this.transactionsContext = new CoreTransactionsContext(environment.meter());
352+
this.transactionsContext = new CoreTransactionsContext(this, environment.meter());
352353
context().environment().eventBus().publish(new TransactionsStartedEvent(environment.transactionsConfig().cleanupConfig().runLostAttemptsCleanupThread(),
353354
environment.transactionsConfig().cleanupConfig().runRegularAttemptsCleanupThread()));
354355
}
@@ -629,7 +630,7 @@ public ValueRecorder responseMetric(final Request<?> request, @Nullable Throwabl
629630
}
630631
}
631632
final String finalExceptionSimpleName = exceptionSimpleName;
632-
final ClusterIdentifier clusterIdent = currentConfig == null ? null : currentConfig.globalConfig() == null ? null : currentConfig.globalConfig().clusterIdent();
633+
final ClusterIdentifier clusterIdent = ClusterIdentifierUtil.fromConfig(currentConfig);
633634

634635
return responseMetrics.computeIfAbsent(new ResponseMetricIdentifier(request, exceptionSimpleName, clusterIdent), key -> {
635636
Map<String, String> tags = new HashMap<>(9);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2024 Couchbase, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.couchbase.client.core.topology;
17+
18+
import com.couchbase.client.core.config.ClusterConfig;
19+
import reactor.util.annotation.Nullable;
20+
21+
public class ClusterIdentifierUtil {
22+
private ClusterIdentifierUtil() {}
23+
24+
public static @Nullable ClusterIdentifier fromConfig(@Nullable ClusterConfig config) {
25+
return config == null ? null : config.globalConfig() == null ? null : config.globalConfig().clusterIdent();
26+
}
27+
}

core-io/src/main/java/com/couchbase/client/core/transaction/context/CoreTransactionsContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.couchbase.client.core.transaction.context;
1717

18+
import com.couchbase.client.core.Core;
1819
import com.couchbase.client.core.annotation.Stability;
1920
import com.couchbase.client.core.cnc.Meter;
2021

@@ -27,8 +28,8 @@
2728
public class CoreTransactionsContext {
2829
private final CoreTransactionsCounters counters;
2930

30-
public CoreTransactionsContext(Meter meter) {
31-
this.counters = new CoreTransactionsCounters(meter);
31+
public CoreTransactionsContext(Core core, Meter meter) {
32+
this.counters = new CoreTransactionsCounters(core, meter);
3233
}
3334

3435
public CoreTransactionsCounters counters() {

core-io/src/main/java/com/couchbase/client/core/transaction/context/CoreTransactionsCounters.java

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,85 @@
1515
*/
1616
package com.couchbase.client.core.transaction.context;
1717

18+
import com.couchbase.client.core.Core;
1819
import com.couchbase.client.core.annotation.Stability;
1920
import com.couchbase.client.core.cnc.Counter;
2021
import com.couchbase.client.core.cnc.Meter;
2122
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;
2326
import com.couchbase.client.core.util.CbCollections;
27+
import reactor.util.annotation.Nullable;
2428

2529
import java.util.HashMap;
2630
import java.util.Map;
31+
import java.util.Objects;
32+
import java.util.concurrent.ConcurrentHashMap;
2733

2834
import static com.couchbase.client.core.cnc.TracingIdentifiers.METER_TRANSACTION_ATTEMPTS;
2935
import static com.couchbase.client.core.cnc.TracingIdentifiers.METER_TRANSACTION_TOTAL;
3036
import static com.couchbase.client.core.cnc.TracingIdentifiers.SERVICE_TRANSACTIONS;
3137

3238
@Stability.Internal
3339
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+
}
5199
}

0 commit comments

Comments
 (0)