-
Notifications
You must be signed in to change notification settings - Fork 168
span stacktrace refactor + autoconfig #1499
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2539208
refactor to use ExtendedSpanProcessor.onEnding
SylvainJuge 01edb03
add autoconfig
SylvainJuge e1e0423
update readme doc
SylvainJuge 2b0f80c
remove duplicated code
SylvainJuge 9e11955
lint before push you should
SylvainJuge 4ac2c28
fix markdown
SylvainJuge 7a9f2f4
the final markdown ?
SylvainJuge c75298a
markdown again
SylvainJuge d1f984a
skip extra processor when disabled
SylvainJuge 3072467
test autoconfig end-to-end for better coverage
SylvainJuge 60451c0
increase coverage + reformat
SylvainJuge 8745a7f
elaborate a bit on autoconfiguration
SylvainJuge 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
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
109 changes: 109 additions & 0 deletions
109
span-stacktrace/src/main/java/io/opentelemetry/contrib/stacktrace/StackTraceAutoConfig.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,109 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.contrib.stacktrace; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
| import io.opentelemetry.sdk.trace.ReadableSpan; | ||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.time.Duration; | ||
| import java.util.function.Predicate; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| @AutoService(AutoConfigurationCustomizerProvider.class) | ||
| public class StackTraceAutoConfig implements AutoConfigurationCustomizerProvider { | ||
|
|
||
| private static final Logger log = Logger.getLogger(StackTraceAutoConfig.class.getName()); | ||
|
|
||
| private static final String CONFIG_MIN_DURATION = | ||
| "otel.java.experimental.span-stacktrace.min.duration"; | ||
| private static final Duration CONFIG_MIN_DURATION_DEFAULT = Duration.ofMillis(5); | ||
|
|
||
| private static final String CONFIG_FILTER = "otel.java.experimental.span-stacktrace.filter"; | ||
|
|
||
| @Override | ||
| public void customize(AutoConfigurationCustomizer config) { | ||
| config.addTracerProviderCustomizer( | ||
| (providerBuilder, properties) -> { | ||
| long minDuration = getMinDuration(properties); | ||
| Predicate<ReadableSpan> filter = getFilterPredicate(properties); | ||
| providerBuilder.addSpanProcessor(new StackTraceSpanProcessor(minDuration, filter)); | ||
SylvainJuge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return providerBuilder; | ||
| }); | ||
| } | ||
|
|
||
| // package-private for testing | ||
| static long getMinDuration(ConfigProperties properties) { | ||
| long minDuration = | ||
| properties.getDuration(CONFIG_MIN_DURATION, CONFIG_MIN_DURATION_DEFAULT).toNanos(); | ||
| if (minDuration < 0) { | ||
| log.fine("Stack traces capture is disabled"); | ||
| } else { | ||
| log.log( | ||
| Level.FINE, | ||
| "Stack traces will be added to spans with a minimum duration of {0} nanos", | ||
| minDuration); | ||
| } | ||
| return minDuration; | ||
| } | ||
|
|
||
| // package private for testing | ||
| static Predicate<ReadableSpan> getFilterPredicate(ConfigProperties properties) { | ||
| String filterClass = properties.getString(CONFIG_FILTER); | ||
| Predicate<ReadableSpan> filter = null; | ||
| if (filterClass != null) { | ||
| Class<?> filterType = getFilterType(filterClass); | ||
| if (filterType != null) { | ||
| filter = getFilterInstance(filterType); | ||
| } | ||
| } | ||
|
|
||
| if (filter == null) { | ||
| // if value is set, lack of filtering is likely an error and must be reported | ||
| Level disabledLogLevel = filterClass != null ? Level.SEVERE : Level.FINE; | ||
| log.log(disabledLogLevel, "Span stacktrace filtering disabled"); | ||
| return span -> true; | ||
| } else { | ||
| log.fine("Span stacktrace filtering enabled with: " + filterClass); | ||
| return filter; | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private static Class<?> getFilterType(String filterClass) { | ||
| try { | ||
| Class<?> filterType = Class.forName(filterClass); | ||
| if (!Predicate.class.isAssignableFrom(filterType)) { | ||
| log.severe("Filter must be a subclass of java.util.function.Predicate"); | ||
| return null; | ||
| } | ||
| return filterType; | ||
| } catch (ClassNotFoundException e) { | ||
| log.severe("Unable to load filter class: " + filterClass); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| @SuppressWarnings("unchecked") | ||
| private static Predicate<ReadableSpan> getFilterInstance(Class<?> filterType) { | ||
| try { | ||
| Constructor<?> constructor = filterType.getConstructor(); | ||
| return (Predicate<ReadableSpan>) constructor.newInstance(); | ||
| } catch (NoSuchMethodException | ||
| | InstantiationException | ||
| | IllegalAccessException | ||
| | InvocationTargetException e) { | ||
| log.severe("Unable to create filter instance with no-arg constructor: " + filterType); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
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
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.