fix: tighten ingest metrics protocol labels

This commit is contained in:
lingniu
2026-07-01 13:38:10 +08:00
parent 27f9cbf0d2
commit 981879136f
4 changed files with 111 additions and 16 deletions

View File

@@ -28,11 +28,12 @@ import java.util.concurrent.ConcurrentMap;
public class IngestMetrics implements IngestInterceptor, Ordered {
private static final String ATTR_TIMER_SAMPLE = "metrics.timer.sample";
private static final String ATTR_PROTOCOL = "metrics.protocol";
private final MeterRegistry registry;
private final ConcurrentMap<ProtocolId, Counter> frameCounters = new ConcurrentHashMap<>();
private final ConcurrentMap<ProtocolId, Counter> errorCounters = new ConcurrentHashMap<>();
private final ConcurrentMap<ProtocolId, Timer> frameTimers = new ConcurrentHashMap<>();
private final ConcurrentMap<String, Counter> errorCounters = new ConcurrentHashMap<>();
private final ConcurrentMap<String, Timer> frameTimers = new ConcurrentHashMap<>();
private final ConcurrentMap<String, Counter> eventCounters = new ConcurrentHashMap<>();
public IngestMetrics(MeterRegistry registry) {
@@ -41,7 +42,8 @@ public class IngestMetrics implements IngestInterceptor, Ordered {
@Override
public boolean before(RawFrame frame, IngestContext ctx) {
ProtocolId p = frame.protocolId();
ProtocolId p = frame.protocolId() == null ? ProtocolId.UNKNOWN : frame.protocolId();
ctx.attr(ATTR_PROTOCOL, p);
frameCounters.computeIfAbsent(p, k ->
Counter.builder("ingest_frames_total").tag("protocol", k.name()).register(registry)).increment();
Timer.Sample sample = Timer.start(registry);
@@ -61,24 +63,42 @@ public class IngestMetrics implements IngestInterceptor, Ordered {
Timer.Sample s = ctx.attr(ATTR_TIMER_SAMPLE);
if (s != null) {
Timer t = frameTimers.computeIfAbsent(event.source(), p ->
Timer.builder("ingest_frame_duration_seconds").tag("protocol", p.name()).register(registry));
s.stop(t);
ctx.attr(ATTR_TIMER_SAMPLE, null);
stopTimer(ctx, event.source() == null ? "unknown" : event.source().name());
}
}
@Override
public void onError(Throwable error, IngestContext ctx) {
// 无协议上下文时归到 UNKNOWN
Counter c = errorCounters.computeIfAbsent(
ProtocolId.GB32960, // 默认桶:由上层拦截器设置具体协议更精确
p -> Counter.builder("ingest_errors_total").tag("protocol", "unknown").register(registry));
c.increment();
String protocol = protocolTag(ctx);
errorCounters.computeIfAbsent(protocol, p ->
Counter.builder("ingest_errors_total").tag("protocol", p).register(registry)).increment();
stopTimer(ctx, protocol);
}
@Override
public int getOrder() {
return 10; // 尽早介入,晚于 Tracingorder=0
}
private void stopTimer(IngestContext ctx, String protocol) {
if (ctx == null) {
return;
}
Timer.Sample sample = ctx.attr(ATTR_TIMER_SAMPLE);
if (sample == null) {
return;
}
Timer timer = frameTimers.computeIfAbsent(protocol, p ->
Timer.builder("ingest_frame_duration_seconds").tag("protocol", p).register(registry));
sample.stop(timer);
ctx.attr(ATTR_TIMER_SAMPLE, null);
}
private static String protocolTag(IngestContext ctx) {
if (ctx == null) {
return "unknown";
}
ProtocolId protocol = ctx.attr(ATTR_PROTOCOL);
return protocol == null || protocol == ProtocolId.UNKNOWN ? "unknown" : protocol.name();
}
}