-
Notifications
You must be signed in to change notification settings - Fork 68
Fix native-image StatsD publishing and logback metrics #1148
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
Open
n0tl3ss
wants to merge
4
commits into
6.1.x
Choose a base branch
from
fix-native-image-statsd-logback-1062
base: 6.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
201 changes: 201 additions & 0 deletions
201
.../main/java/io/micronaut/configuration/metrics/binder/logging/MicronautLogbackMetrics.java
This file contains hidden or 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,201 @@ | ||
| /* | ||
| * Copyright 2017-2019 original authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micronaut.configuration.metrics.binder.logging; | ||
|
|
||
| import ch.qos.logback.classic.Level; | ||
| import ch.qos.logback.classic.Logger; | ||
| import ch.qos.logback.classic.LoggerContext; | ||
| import ch.qos.logback.classic.spi.LoggerContextListener; | ||
| import ch.qos.logback.classic.turbo.TurboFilter; | ||
| import ch.qos.logback.core.spi.FilterReply; | ||
| import io.micrometer.core.instrument.FunctionCounter; | ||
| import io.micrometer.core.instrument.MeterRegistry; | ||
| import io.micrometer.core.instrument.Tag; | ||
| import io.micrometer.core.instrument.binder.BaseUnits; | ||
| import io.micrometer.core.instrument.binder.logging.LogbackMetrics; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.slf4j.Marker; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.LongAdder; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
|
|
||
| /** | ||
| * Async-safe variant of {@link LogbackMetrics}. | ||
| */ | ||
| final class MicronautLogbackMetrics extends LogbackMetrics { | ||
|
|
||
| private final Iterable<Tag> tags; | ||
| private final LoggerContext loggerContext; | ||
| private final Map<MeterRegistry, MetricsTurboFilter> metricsTurboFilters = new HashMap<>(); | ||
| private final LoggerContextListener resetListener; | ||
|
|
||
| MicronautLogbackMetrics() { | ||
| this(emptyList()); | ||
| } | ||
|
|
||
| MicronautLogbackMetrics(Iterable<Tag> tags) { | ||
| this(tags, (LoggerContext) LoggerFactory.getILoggerFactory()); | ||
| } | ||
|
|
||
| MicronautLogbackMetrics(Iterable<Tag> tags, LoggerContext loggerContext) { | ||
| super(tags, loggerContext); | ||
| this.tags = tags; | ||
| this.loggerContext = loggerContext; | ||
|
|
||
| this.resetListener = new LoggerContextListener() { | ||
| @Override | ||
| public boolean isResetResistant() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void onReset(LoggerContext context) { | ||
| synchronized (metricsTurboFilters) { | ||
| for (MetricsTurboFilter metricsTurboFilter : metricsTurboFilters.values()) { | ||
| loggerContext.addTurboFilter(metricsTurboFilter); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onStart(LoggerContext context) { | ||
| // no-op | ||
| } | ||
|
|
||
| @Override | ||
| public void onStop(LoggerContext context) { | ||
| // no-op | ||
| } | ||
|
|
||
| @Override | ||
| public void onLevelChange(Logger logger, Level level) { | ||
| // no-op | ||
| } | ||
| }; | ||
| loggerContext.addListener(resetListener); | ||
| } | ||
|
|
||
| @Override | ||
| public void bindTo(MeterRegistry registry) { | ||
| synchronized (metricsTurboFilters) { | ||
| MetricsTurboFilter existingFilter = metricsTurboFilters.get(registry); | ||
| if (existingFilter != null) { | ||
| loggerContext.getTurboFilterList().remove(existingFilter); | ||
| loggerContext.addTurboFilter(existingFilter); | ||
| return; | ||
| } | ||
| MetricsTurboFilter filter = new MetricsTurboFilter(registry, tags); | ||
| metricsTurboFilters.put(registry, filter); | ||
| loggerContext.addTurboFilter(filter); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| synchronized (metricsTurboFilters) { | ||
| for (MetricsTurboFilter metricsTurboFilter : metricsTurboFilters.values()) { | ||
| loggerContext.getTurboFilterList().remove(metricsTurboFilter); | ||
| } | ||
| metricsTurboFilters.clear(); | ||
| } | ||
| loggerContext.removeListener(resetListener); | ||
| } | ||
|
|
||
| static final class MetricsTurboFilter extends TurboFilter { | ||
|
|
||
| private static final String METER_NAME = "logback.events"; | ||
| private static final String METER_DESCRIPTION = "Number of log events that were enabled by the effective log level"; | ||
| private static final String LEVEL_TAG = "level"; | ||
|
|
||
| private final LongAdder errorCount = new LongAdder(); | ||
| private final LongAdder warnCount = new LongAdder(); | ||
| private final LongAdder infoCount = new LongAdder(); | ||
| private final LongAdder debugCount = new LongAdder(); | ||
| private final LongAdder traceCount = new LongAdder(); | ||
|
|
||
| MetricsTurboFilter(MeterRegistry registry, Iterable<Tag> tags) { | ||
| FunctionCounter.builder(METER_NAME, errorCount, LongAdder::doubleValue) | ||
| .tags(tags) | ||
| .tags(LEVEL_TAG, "error") | ||
| .description(METER_DESCRIPTION) | ||
| .baseUnit(BaseUnits.EVENTS) | ||
| .register(registry); | ||
|
|
||
| FunctionCounter.builder(METER_NAME, warnCount, LongAdder::doubleValue) | ||
| .tags(tags) | ||
| .tags(LEVEL_TAG, "warn") | ||
| .description(METER_DESCRIPTION) | ||
| .baseUnit(BaseUnits.EVENTS) | ||
| .register(registry); | ||
|
|
||
| FunctionCounter.builder(METER_NAME, infoCount, LongAdder::doubleValue) | ||
| .tags(tags) | ||
| .tags(LEVEL_TAG, "info") | ||
| .description(METER_DESCRIPTION) | ||
| .baseUnit(BaseUnits.EVENTS) | ||
| .register(registry); | ||
|
|
||
| FunctionCounter.builder(METER_NAME, debugCount, LongAdder::doubleValue) | ||
| .tags(tags) | ||
| .tags(LEVEL_TAG, "debug") | ||
| .description(METER_DESCRIPTION) | ||
| .baseUnit(BaseUnits.EVENTS) | ||
| .register(registry); | ||
|
|
||
| FunctionCounter.builder(METER_NAME, traceCount, LongAdder::doubleValue) | ||
| .tags(tags) | ||
| .tags(LEVEL_TAG, "trace") | ||
| .description(METER_DESCRIPTION) | ||
| .baseUnit(BaseUnits.EVENTS) | ||
| .register(registry); | ||
| } | ||
|
|
||
| @Override | ||
| public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) { | ||
| if (format == null || !level.isGreaterOrEqual(logger.getEffectiveLevel())) { | ||
| return FilterReply.NEUTRAL; | ||
| } | ||
|
|
||
| recordMetrics(level); | ||
| return FilterReply.NEUTRAL; | ||
| } | ||
|
|
||
| private void recordMetrics(Level level) { | ||
| switch (level.toInt()) { | ||
| case Level.ERROR_INT: | ||
| errorCount.increment(); | ||
| break; | ||
| case Level.WARN_INT: | ||
| warnCount.increment(); | ||
| break; | ||
| case Level.INFO_INT: | ||
| infoCount.increment(); | ||
| break; | ||
| case Level.DEBUG_INT: | ||
| debugCount.increment(); | ||
| break; | ||
| case Level.TRACE_INT: | ||
| traceCount.increment(); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
40 changes: 40 additions & 0 deletions
40
...java/io/micronaut/configuration/metrics/micrometer/statsd/NativeImageStatsdCondition.java
This file contains hidden or 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,40 @@ | ||
| /* | ||
| * Copyright 2017-2026 original authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micronaut.configuration.metrics.micrometer.statsd; | ||
|
|
||
| import io.micronaut.context.condition.Condition; | ||
| import io.micronaut.context.condition.ConditionContext; | ||
|
|
||
| /** | ||
| * Enables the native-image StatsD workaround only for UDP-based native executables. | ||
| */ | ||
| final class NativeImageStatsdCondition implements Condition { | ||
|
|
||
| static final String NATIVE_IMAGE_CODE_PROPERTY = "org.graalvm.nativeimage.imagecode"; | ||
| private static final String RUNTIME = "runtime"; | ||
| private static final String UDP = "UDP"; | ||
|
|
||
| @Override | ||
| public boolean matches(ConditionContext context) { | ||
| String imageCode = System.getProperty(NATIVE_IMAGE_CODE_PROPERTY); | ||
| if (!RUNTIME.equalsIgnoreCase(imageCode)) { | ||
| return false; | ||
| } | ||
| String protocol = context.getProperty(StatsdMeterRegistryFactory.STATSD_CONFIG + ".protocol", String.class) | ||
| .orElse(UDP); | ||
| return UDP.equalsIgnoreCase(protocol); | ||
| } | ||
|
n0tl3ss marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.