generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 98
improv(metrics): Add thread-safety to Metrics utility through thread-local Metrics instance management #2294
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ed6a69b
Add proxy to manage thread local metrics provider instances to achiev…
phipag 8857936
Refactor from inheritable thread local to tracking by trace id. Simil…
phipag 784af1e
Do not hard code JVM system property for trace id.
phipag 308faa1
Fix PMD findings.
phipag 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
156 changes: 156 additions & 0 deletions
156
...in/java/software/amazon/lambda/powertools/metrics/internal/RequestScopedMetricsProxy.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,156 @@ | ||
| /* | ||
| * Copyright 2023 Amazon.com, Inc. or its affiliates. | ||
| * 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 | ||
| * http://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 software.amazon.lambda.powertools.metrics.internal; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.HashMap; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import com.amazonaws.services.lambda.runtime.Context; | ||
|
|
||
| import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor; | ||
| import software.amazon.lambda.powertools.metrics.Metrics; | ||
| import software.amazon.lambda.powertools.metrics.model.DimensionSet; | ||
| import software.amazon.lambda.powertools.metrics.model.MetricResolution; | ||
| import software.amazon.lambda.powertools.metrics.model.MetricUnit; | ||
| import software.amazon.lambda.powertools.metrics.provider.MetricsProvider; | ||
|
|
||
| public class RequestScopedMetricsProxy implements Metrics { | ||
| private static final String DEFAULT_TRACE_ID = "DEFAULT"; | ||
| private final ConcurrentMap<String, Metrics> metricsMap = new ConcurrentHashMap<>(); | ||
| private final MetricsProvider provider; | ||
| private final AtomicReference<String> initialNamespace = new AtomicReference<>(); | ||
| private final AtomicReference<DimensionSet> initialDefaultDimensions = new AtomicReference<>(); | ||
| private final AtomicBoolean initialRaiseOnEmptyMetrics = new AtomicBoolean(false); | ||
|
|
||
| public RequestScopedMetricsProxy(MetricsProvider provider) { | ||
| this.provider = provider; | ||
| } | ||
|
|
||
| private String getTraceId() { | ||
| return LambdaHandlerProcessor.getXrayTraceId().orElse(DEFAULT_TRACE_ID); | ||
| } | ||
|
|
||
| private Metrics getOrCreateMetrics() { | ||
| String traceId = getTraceId(); | ||
| return metricsMap.computeIfAbsent(traceId, key -> { | ||
| Metrics metrics = provider.getMetricsInstance(); | ||
| String namespace = initialNamespace.get(); | ||
| if (namespace != null) { | ||
| metrics.setNamespace(namespace); | ||
| } | ||
| DimensionSet dimensions = initialDefaultDimensions.get(); | ||
| if (dimensions != null) { | ||
| metrics.setDefaultDimensions(dimensions); | ||
| } | ||
| metrics.setRaiseOnEmptyMetrics(initialRaiseOnEmptyMetrics.get()); | ||
| return metrics; | ||
| }); | ||
| } | ||
|
|
||
| // Configuration methods - called by MetricsFactory and MetricsBuilder | ||
| // These methods DO NOT eagerly create instances because they are typically called | ||
| // outside the Lambda handler (e.g., during class initialization) potentially on a different thread. | ||
| // We delay instance creation until the first operation that needs the metrics backend (e.g., addMetric). | ||
| // See {@link software.amazon.lambda.powertools.metrics.MetricsFactory#getMetricsInstance()} | ||
| // and {@link software.amazon.lambda.powertools.metrics.MetricsBuilder#build()} | ||
|
|
||
| @Override | ||
| public void setNamespace(String namespace) { | ||
| this.initialNamespace.set(namespace); | ||
| Optional.ofNullable(metricsMap.get(getTraceId())).ifPresent(m -> m.setNamespace(namespace)); | ||
| } | ||
|
|
||
| @Override | ||
| public void setDefaultDimensions(DimensionSet dimensionSet) { | ||
| if (dimensionSet == null) { | ||
| throw new IllegalArgumentException("DimensionSet cannot be null"); | ||
| } | ||
| this.initialDefaultDimensions.set(dimensionSet); | ||
| Optional.ofNullable(metricsMap.get(getTraceId())).ifPresent(m -> m.setDefaultDimensions(dimensionSet)); | ||
| } | ||
|
|
||
| @Override | ||
| public void setRaiseOnEmptyMetrics(boolean raiseOnEmptyMetrics) { | ||
| this.initialRaiseOnEmptyMetrics.set(raiseOnEmptyMetrics); | ||
| Optional.ofNullable(metricsMap.get(getTraceId())).ifPresent(m -> m.setRaiseOnEmptyMetrics(raiseOnEmptyMetrics)); | ||
| } | ||
|
|
||
| @Override | ||
| public DimensionSet getDefaultDimensions() { | ||
| Metrics metrics = metricsMap.get(getTraceId()); | ||
| if (metrics != null) { | ||
| return metrics.getDefaultDimensions(); | ||
| } | ||
| DimensionSet dimensions = initialDefaultDimensions.get(); | ||
| return dimensions != null ? dimensions : DimensionSet.of(new HashMap<>()); | ||
| } | ||
|
|
||
| // Metrics operations - these eagerly create instances | ||
|
|
||
| @Override | ||
| public void addMetric(String key, double value, MetricUnit unit, MetricResolution resolution) { | ||
| getOrCreateMetrics().addMetric(key, value, unit, resolution); | ||
| } | ||
|
|
||
| @Override | ||
| public void addDimension(DimensionSet dimensionSet) { | ||
| getOrCreateMetrics().addDimension(dimensionSet); | ||
| } | ||
|
|
||
| @Override | ||
| public void setTimestamp(Instant timestamp) { | ||
| getOrCreateMetrics().setTimestamp(timestamp); | ||
| } | ||
|
|
||
| @Override | ||
| public void addMetadata(String key, Object value) { | ||
| getOrCreateMetrics().addMetadata(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void clearDefaultDimensions() { | ||
| getOrCreateMetrics().clearDefaultDimensions(); | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() { | ||
| // Always create instance to ensure validation and warnings are triggered. E.g. when raiseOnEmptyMetrics | ||
| // is enabled. | ||
| Metrics metrics = getOrCreateMetrics(); | ||
| metrics.flush(); | ||
| metricsMap.remove(getTraceId()); | ||
| } | ||
|
|
||
| @Override | ||
| public void captureColdStartMetric(Context context, DimensionSet dimensions) { | ||
| getOrCreateMetrics().captureColdStartMetric(context, dimensions); | ||
| } | ||
|
|
||
| @Override | ||
| public void captureColdStartMetric(DimensionSet dimensions) { | ||
| getOrCreateMetrics().captureColdStartMetric(dimensions); | ||
| } | ||
|
|
||
| @Override | ||
| public void flushMetrics(Consumer<Metrics> metricsConsumer) { | ||
| getOrCreateMetrics().flushMetrics(metricsConsumer); | ||
| } | ||
| } |
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
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.