Skip to content

Commit 597d123

Browse files
committed
Automatic priming of powertools-metrics
1 parent 697f176 commit 597d123

File tree

5 files changed

+9859
-1
lines changed

5 files changed

+9859
-1
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<aws-embedded-metrics.version>4.1.2</aws-embedded-metrics.version>
9292
<jmespath.version>0.6.0</jmespath.version>
9393
<elastic.version>1.6.0</elastic.version>
94+
<crac.version>1.4.0</crac.version>
9495

9596
<!-- As we have a .mvn directory at the root of the project, this will evaluate to the root directory
9697
regardless of where maven is run - sub-module, or root. -->
@@ -238,6 +239,11 @@
238239
<artifactId>logback-ecs-encoder</artifactId>
239240
<version>${elastic.version}</version>
240241
</dependency>
242+
<dependency>
243+
<groupId>org.crac</groupId>
244+
<artifactId>crac</artifactId>
245+
<version>${crac.version}</version>
246+
</dependency>
241247
<dependency>
242248
<groupId>org.slf4j</groupId>
243249
<artifactId>slf4j-api</artifactId>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
package software.amazon.lambda.powertools.common.internal;
15+
16+
import java.io.BufferedReader;
17+
import java.io.IOException;
18+
import java.io.InputStream;
19+
import java.io.InputStreamReader;
20+
import java.nio.charset.StandardCharsets;
21+
import java.net.URL;
22+
import java.net.URLConnection;
23+
import java.util.Enumeration;
24+
25+
public class ClassPreLoader {
26+
public static final String CLASSES_FILE = "classesloaded.txt";
27+
public static void preloadClass(String classname, boolean initialize) {
28+
try {
29+
Class.forName(classname, initialize, ClassPreLoader.class.getClassLoader());
30+
} catch (Throwable ignored) {
31+
}
32+
}
33+
public static void preloadClasses(boolean initialize) {
34+
try {
35+
Enumeration<URL> files = ClassPreLoader.class.getClassLoader().getResources(CLASSES_FILE);
36+
while (files.hasMoreElements()) {
37+
URL url = files.nextElement();
38+
URLConnection conn = url.openConnection();
39+
conn.setUseCaches(false);
40+
InputStream is = conn.getInputStream();
41+
preloadClassesFromStream(is, initialize);
42+
}
43+
} catch (IOException ignored) {
44+
}
45+
InputStream is = ClassPreLoader.class
46+
.getResourceAsStream( CLASSES_FILE);
47+
if (is != null)
48+
preloadClassesFromStream(is, initialize);
49+
}
50+
public static void preloadClassesFromStream(InputStream is, boolean initialize) {
51+
try (is;
52+
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
53+
BufferedReader reader = new BufferedReader(isr)) {
54+
String line;
55+
while ((line = reader.readLine()) != null) {
56+
int idx = line.indexOf('#');
57+
if (idx != -1) {
58+
line = line.substring(0, idx);
59+
}
60+
final String className = line.stripTrailing();
61+
if (!className.isBlank()) {
62+
preloadClass(className, initialize);
63+
}
64+
}
65+
} catch (Exception ignored) {
66+
}
67+
}
68+
public void invokePreloadClasses(boolean initialize) {
69+
preloadClasses(initialize);
70+
}
71+
}

powertools-metrics/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
<artifactId>aspectjrt</artifactId>
4040
<scope>provided</scope>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.crac</groupId>
44+
<artifactId>crac</artifactId>
45+
</dependency>
4246
<dependency>
4347
<groupId>software.amazon.lambda</groupId>
4448
<artifactId>powertools-common</artifactId>

powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspect.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,28 @@
3030
import org.aspectj.lang.annotation.Around;
3131
import org.aspectj.lang.annotation.Aspect;
3232
import org.aspectj.lang.annotation.Pointcut;
33+
import org.crac.Core;
34+
import org.crac.Resource;
3335
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
3436
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
3537
import software.amazon.cloudwatchlogs.emf.model.MetricsContext;
3638
import software.amazon.cloudwatchlogs.emf.model.Unit;
39+
import software.amazon.lambda.powertools.common.internal.ClassPreLoader;
3740
import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor;
3841
import software.amazon.lambda.powertools.metrics.Metrics;
3942
import software.amazon.lambda.powertools.metrics.MetricsUtils;
4043
import software.amazon.lambda.powertools.metrics.ValidationException;
4144

4245
@Aspect
43-
public class LambdaMetricsAspect {
46+
public class LambdaMetricsAspect implements Resource {
4447
public static final String TRACE_ID_PROPERTY = "xray_trace_id";
4548
public static final String REQUEST_ID_PROPERTY = "function_request_id";
4649
private static final String NAMESPACE = System.getenv("POWERTOOLS_METRICS_NAMESPACE");
4750

51+
public LambdaMetricsAspect() {
52+
Core.getGlobalContext().register(this);
53+
}
54+
4855
private static String service(Metrics metrics) {
4956
return !"".equals(metrics.service()) ? metrics.service() : serviceName();
5057
}
@@ -148,4 +155,14 @@ private void validateMetricsAndRefreshOnFailure(Metrics metrics) {
148155
throw e;
149156
}
150157
}
158+
159+
@Override
160+
public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throws Exception {
161+
System.out.println("before preloading");
162+
ClassPreLoader.preloadClasses(true);
163+
System.out.println("after preloading");
164+
}
165+
@Override
166+
public void afterRestore(org.crac.Context<? extends Resource> context) throws Exception {
167+
}
151168
}

0 commit comments

Comments
 (0)