-
Notifications
You must be signed in to change notification settings - Fork 996
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement LogbackMetrics binder, ensure Meter deduplication in regist…
…ries
- Loading branch information
1 parent
09daddc
commit e2d873a
Showing
20 changed files
with
358 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/org/springframework/metrics/instrument/Tags.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.springframework.metrics.instrument; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class Tags { | ||
public static Iterable<Tag> tagList(String... keyValues) { | ||
if (keyValues.length % 2 == 1) { | ||
throw new IllegalArgumentException("size must be even, it is a set of key=value pairs"); | ||
} | ||
ArrayList<Tag> ts = new ArrayList<>(keyValues.length); | ||
for (int i = 0; i < keyValues.length; i += 2) { | ||
ts.add(new ImmutableTag(keyValues[i], keyValues[i + 1])); | ||
} | ||
return ts; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/springframework/metrics/instrument/binder/LogbackMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.springframework.metrics.instrument.binder; | ||
|
||
import ch.qos.logback.classic.LoggerContext; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.metrics.instrument.MeterRegistry; | ||
|
||
public class LogbackMetrics implements MeterBinder { | ||
@Override | ||
public void bindTo(MeterRegistry registry) { | ||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
context.addTurboFilter(new MetricsTurboFilter(registry)); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/org/springframework/metrics/instrument/binder/MetricsTurboFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.springframework.metrics.instrument.binder; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.turbo.TurboFilter; | ||
import ch.qos.logback.core.spi.FilterReply; | ||
import org.slf4j.Marker; | ||
import org.springframework.metrics.instrument.Counter; | ||
import org.springframework.metrics.instrument.MeterRegistry; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class MetricsTurboFilter extends TurboFilter { | ||
private final Map<Level, Counter> levelCounters; | ||
|
||
public MetricsTurboFilter(MeterRegistry registry) { | ||
levelCounters = new HashMap<Level, Counter>() {{ | ||
put(Level.ERROR, registry.counter("logback_events", "level", "error")); | ||
put(Level.WARN, registry.counter("logback_events", "level", "warn")); | ||
put(Level.INFO, registry.counter("logback_events", "level", "info")); | ||
put(Level.DEBUG, registry.counter("logback_events", "level", "debug")); | ||
put(Level.TRACE, registry.counter("logback_events", "level", "trace")); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jkschneider
Author
Contributor
|
||
}}; | ||
} | ||
|
||
@Override | ||
public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) { | ||
Counter counter = levelCounters.get(level); | ||
if(counter != null) | ||
counter.increment(); | ||
return FilterReply.ACCEPT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/org/springframework/metrics/instrument/internal/MeterId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.springframework.metrics.instrument.internal; | ||
|
||
import org.springframework.metrics.instrument.Tag; | ||
|
||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class MeterId { | ||
private final String name; | ||
private final Tag[] tags; | ||
|
||
public MeterId(String name, Iterable<Tag> tags) { | ||
this.name = name; | ||
this.tags = StreamSupport.stream(tags.spliterator(), false).sorted(Comparator.comparing(Tag::getKey)) | ||
.toArray(Tag[]::new); | ||
} | ||
|
||
public static MeterId id(String name, Iterable<Tag> tags) { | ||
return new MeterId(name, tags); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
MeterId meterId = (MeterId) o; | ||
return (name != null ? name.equals(meterId.name) : meterId.name == null) && Arrays.equals(tags, meterId.tags); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = name != null ? name.hashCode() : 0; | ||
result = 31 * result + Arrays.hashCode(tags); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "MeterId{" + | ||
"name='" + name + '\'' + | ||
", tags=" + Arrays.toString(tags) + | ||
'}'; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Tag[] getTags() { | ||
return tags; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Is it worth mimicking https://github.com/prometheus/client_java/blob/master/simpleclient_logback/src/main/java/io/prometheus/client/logback/InstrumentedAppender.java more closely? I realize you didn't use the prometheus one directly since we ant the instrumentation to apply regardless of underlying metric implementation.
Also would a
switch
be slightly more efficient than a map lookup and null check?I don't typically focus on optimizations, but in the case of logging we do want it to as minimally intrusive as possible.
Lastly, it would be good to align with the existing prometheus metric name
logback_appender_total
(though if we aren't using an appender I guess it makes sense to change names)