Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: fix metrics tags coverage in the seata-server side #4860

Merged
merged 11 commits into from Aug 19, 2022
15 changes: 15 additions & 0 deletions metrics/seata-metrics-api/src/main/java/io/seata/metrics/Id.java
Expand Up @@ -68,6 +68,21 @@ public Id withTag(Iterable<Entry<String, String>> tags) {
return this;
}

public String getMeterKey() {
StringBuilder builder = new StringBuilder(name);
builder.append("(");
if (tags.size() == 0) {
builder.append(")");
return builder.toString();
}
for (Entry<String, String> tag : tags.entrySet()) {
builder.append(String.format("%s,", tag.getValue()));
}
builder.delete(builder.length() - 1, builder.length());
builder.append(")");
return builder.toString();
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder(name);
Expand Down
Expand Up @@ -18,7 +18,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

Expand All @@ -40,26 +39,30 @@
*/
@LoadLevel(name = "compact", order = 1)
public class CompactRegistry implements Registry {
private static final Map<UUID, Meter> METERS = new ConcurrentHashMap<>();
private static final Map<String, Meter> METERS = new ConcurrentHashMap<>();

@Override
public <T extends Number> Gauge<T> getGauge(Id id, Supplier<T> supplier) {
return (Gauge<T>)CollectionUtils.computeIfAbsent(METERS, id.getId(), key -> new CompactGauge<>(id, supplier));
return (Gauge<T>)CollectionUtils.computeIfAbsent(METERS, id.getMeterKey(), key -> new CompactGauge<>(
new Id(id.getName()).withTag(id.getTags()), supplier));
}

@Override
public Counter getCounter(Id id) {
return (Counter)CollectionUtils.computeIfAbsent(METERS, id.getId(), key -> new CompactCounter(id));
return (Counter)CollectionUtils.computeIfAbsent(METERS, id.getMeterKey(), key -> new CompactCounter(
new Id(id.getName()).withTag(id.getTags())));
}

@Override
public Summary getSummary(Id id) {
return (Summary)CollectionUtils.computeIfAbsent(METERS, id.getId(), key -> new CompactSummary(id));
return (Summary)CollectionUtils.computeIfAbsent(METERS, id.getMeterKey(), key -> new CompactSummary(
new Id(id.getName()).withTag(id.getTags())));
}

@Override
public Timer getTimer(Id id) {
return (Timer)CollectionUtils.computeIfAbsent(METERS, id.getId(), key -> new CompactTimer(id));
return (Timer)CollectionUtils.computeIfAbsent(METERS, id.getMeterKey(), key -> new CompactTimer(
new Id(id.getName()).withTag(id.getTags())));
}

@Override
Expand Down
Expand Up @@ -47,7 +47,7 @@ public Registry getRegistry() {

public void init() {
boolean enabled = ConfigurationFactory.getInstance().getBoolean(
ConfigurationKeys.METRICS_PREFIX + ConfigurationKeys.METRICS_ENABLED, false);
ConfigurationKeys.METRICS_PREFIX + ConfigurationKeys.METRICS_ENABLED, true);
if (enabled) {
registry = RegistryFactory.getInstance();
if (registry != null) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/resources/application.yml
Expand Up @@ -37,4 +37,4 @@ seata:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
slievrly marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,18 @@
package io.seata.server.metrics;

import io.seata.metrics.Counter;
import io.seata.metrics.Id;
import io.seata.metrics.IdConstants;
import org.junit.jupiter.api.Test;

import static io.seata.server.metrics.MeterIdConstants.COUNTER_ACTIVE;

public class RegistryMeterKeyTest {
@Test
public void testGetIdMeterKey() {
System.out.println(COUNTER_ACTIVE.getMeterKey());
Id id = COUNTER_ACTIVE.withTag(IdConstants.ROLE_KEY, IdConstants.ROLE_VALUE_TM);
slievrly marked this conversation as resolved.
Show resolved Hide resolved
System.out.println(id.getMeterKey());
}

}