-
Notifications
You must be signed in to change notification settings - Fork 90
feat(v2): upgrade embedded-metrics to v4 #1405
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
Changes from all commits
400551c
a0e1fcf
67152ec
e4317db
75c94f0
0ec27cf
75b3fb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ | |
* Handler for requests to Lambda function. | ||
*/ | ||
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { | ||
private final static Logger log = LogManager.getLogger(App.class); | ||
private static final Logger log = LogManager.getLogger(App.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's up with all the whitespace changes? |
||
|
||
@Logging(logEvent = true, samplingRate = 0.7) | ||
@Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR) | ||
|
@@ -57,11 +57,11 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv | |
|
||
metricsLogger().putMetric("CustomMetric1", 1, Unit.COUNT); | ||
|
||
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) -> | ||
{ | ||
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService")); | ||
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1")); | ||
}); | ||
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", metric -> | ||
{ | ||
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService")); | ||
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1")); | ||
}); | ||
|
||
LoggingUtils.appendKey("test", "willBeLogged"); | ||
|
||
|
@@ -74,11 +74,11 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv | |
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents); | ||
|
||
TracingUtils.withSubsegment("loggingResponse", subsegment -> | ||
{ | ||
String sampled = "log something out"; | ||
log.info(sampled); | ||
log.info(output); | ||
}); | ||
{ | ||
String sampled = "log something out"; | ||
log.info(sampled); | ||
log.info(output); | ||
}); | ||
|
||
log.info("After output"); | ||
return response | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ | |
|
||
package software.amazon.lambda.powertools.metrics; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static java.util.Optional.ofNullable; | ||
import static software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor.getXrayTraceId; | ||
import static software.amazon.lambda.powertools.metrics.internal.LambdaMetricsAspect.REQUEST_ID_PROPERTY; | ||
|
@@ -25,11 +24,14 @@ | |
import java.util.function.Consumer; | ||
import software.amazon.cloudwatchlogs.emf.config.SystemWrapper; | ||
import software.amazon.cloudwatchlogs.emf.environment.EnvironmentProvider; | ||
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException; | ||
import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException; | ||
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger; | ||
import software.amazon.cloudwatchlogs.emf.model.DimensionSet; | ||
import software.amazon.cloudwatchlogs.emf.model.MetricsContext; | ||
import software.amazon.cloudwatchlogs.emf.model.MetricsLoggerHelper; | ||
import software.amazon.cloudwatchlogs.emf.model.Unit; | ||
import software.amazon.lambda.powertools.metrics.exception.InvalidMetricNamespaceException; | ||
|
||
/** | ||
* A class used to retrieve the instance of the {@code MetricsLogger} used by | ||
|
@@ -63,23 +65,6 @@ public static void defaultDimensions(final DimensionSet... dimensionSets) { | |
MetricsUtils.defaultDimensions = dimensionSets; | ||
} | ||
|
||
/** | ||
* Configure default dimension to be used by logger. | ||
* By default, @{@link Metrics} annotation captures configured service as a dimension <i>Service</i> | ||
* | ||
* @param dimensionSet Default value of dimension set for logger | ||
* @deprecated use {@link #defaultDimensions(DimensionSet...)} instead | ||
*/ | ||
@Deprecated | ||
public static void defaultDimensionSet(final DimensionSet dimensionSet) { | ||
requireNonNull(dimensionSet, "Null dimension set not allowed"); | ||
|
||
if (dimensionSet.getDimensionKeys().size() > 0) { | ||
defaultDimensions(dimensionSet); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Add and immediately flush a single metric. It will use the default namespace | ||
* specified either on {@link Metrics} annotation or via POWERTOOLS_METRICS_NAMESPACE env var. | ||
|
@@ -95,11 +80,20 @@ public static void withSingleMetric(final String name, | |
final double value, | ||
final Unit unit, | ||
final Consumer<MetricsLogger> logger) { | ||
withMetricsLogger(metricsLogger -> | ||
{ | ||
MetricsLogger metricsLogger = logger(); | ||
|
||
try { | ||
metricsLogger.setNamespace(defaultNameSpace()); | ||
metricsLogger.putMetric(name, value, unit); | ||
captureRequestAndTraceId(metricsLogger); | ||
logger.accept(metricsLogger); | ||
}); | ||
} catch (InvalidNamespaceException e) { | ||
throw new InvalidMetricNamespaceException(e); | ||
} catch (InvalidMetricException e) { | ||
throw new software.amazon.lambda.powertools.metrics.exception.InvalidMetricException(e); | ||
} finally { | ||
metricsLogger.flush(); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -118,12 +112,20 @@ public static void withSingleMetric(final String name, | |
final Unit unit, | ||
final String namespace, | ||
final Consumer<MetricsLogger> logger) { | ||
withMetricsLogger(metricsLogger -> | ||
{ | ||
MetricsLogger metricsLogger = logger(); | ||
|
||
try { | ||
metricsLogger.setNamespace(namespace); | ||
metricsLogger.putMetric(name, value, unit); | ||
captureRequestAndTraceId(metricsLogger); | ||
logger.accept(metricsLogger); | ||
}); | ||
} catch (InvalidNamespaceException e) { | ||
throw new InvalidMetricNamespaceException(e); | ||
} catch (InvalidMetricException e) { | ||
throw new software.amazon.lambda.powertools.metrics.exception.InvalidMetricException(e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit confusing that sometimes the wrapping RuntimeException has the same name and only a different NS and sometimes not |
||
} finally { | ||
metricsLogger.flush(); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -141,25 +143,13 @@ public static void withMetricsLogger(final Consumer<MetricsLogger> logger) { | |
metricsLogger.setNamespace(defaultNameSpace()); | ||
captureRequestAndTraceId(metricsLogger); | ||
logger.accept(metricsLogger); | ||
} catch (InvalidNamespaceException e) { | ||
throw new InvalidMetricNamespaceException(e); | ||
} finally { | ||
metricsLogger.flush(); | ||
} | ||
} | ||
|
||
/** | ||
* Provide and immediately flush a {@link MetricsLogger}. It uses the default namespace | ||
* specified either on {@link Metrics} annotation or via POWERTOOLS_METRICS_NAMESPACE env var. | ||
* It by default captures function_request_id as property if used together with {@link Metrics} annotation. It will also | ||
* capture xray_trace_id as property if tracing is enabled. | ||
* | ||
* @param logger the MetricsLogger | ||
* @deprecated use {@link MetricsUtils#withMetricsLogger} instead | ||
*/ | ||
@Deprecated | ||
public static void withMetricLogger(final Consumer<MetricsLogger> logger) { | ||
withMetricsLogger(logger); | ||
} | ||
|
||
public static DimensionSet[] getDefaultDimensions() { | ||
return Arrays.copyOf(defaultDimensions, defaultDimensions.length); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* 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.exception; | ||
|
||
public class InvalidMetricDimensionException extends RuntimeException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I gather we are using these to wrap the checked exceptions with RuntimeExceptions while retaining something catchable. Worth explaining that and why we're doing it on the |
||
|
||
private static final long serialVersionUID = -2011349422261364971L; | ||
|
||
public InvalidMetricDimensionException(Throwable e) { | ||
super(e); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth documenting the exception transformation here, probably? I'm also wondering if we should reach out to the SDK team to understand their thinking for the checked exceptions here; maybe we are missing something.