Skip to content

Commit 868f491

Browse files
committed
Added annotations package for annotation metrics
1 parent 2713006 commit 868f491

File tree

11 files changed

+780
-1
lines changed

11 files changed

+780
-1
lines changed

annotations/build.gradle

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
plugins {
2+
id 'java'
3+
id 'java-library'
4+
id "io.freefair.aspectj.post-compile-weaving" version "6.4.3"
5+
id 'com.diffplug.spotless' version '5.8.2'
6+
}
7+
8+
repositories {
9+
jcenter()
10+
mavenCentral()
11+
}
12+
13+
dependencies {
14+
implementation rootProject
15+
16+
annotationProcessor 'org.projectlombok:lombok:1.18.24'
17+
compileOnly 'org.projectlombok:lombok:1.18.12'
18+
19+
implementation "org.aspectj:aspectjrt:1.9.22"
20+
implementation "org.aspectj:aspectjweaver:1.9.8.RC3"
21+
22+
implementation 'com.fasterxml.jackson.core:jackson-core:2.14.2'
23+
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.14.2'
24+
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.2'
25+
implementation 'org.slf4j:slf4j-api:2.0.6'
26+
implementation 'org.javatuples:javatuples:1.2'
27+
implementation 'org.apache.commons:commons-lang3:3.12.0'
28+
29+
// Use JUnit test framework
30+
testImplementation 'software.amazon.awssdk:cloudwatch:2.20.13'
31+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2'
32+
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.2'
33+
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
34+
testImplementation 'org.junit.vintage:junit-vintage-engine:5.9.2'
35+
testImplementation 'org.mockito:mockito-core:2.+'
36+
testImplementation 'org.powermock:powermock-module-junit4:2.0.9'
37+
testImplementation 'org.powermock:powermock-api-mockito2:2.0.9'
38+
testImplementation 'com.github.javafaker:javafaker:1.0.2'
39+
testImplementation 'com.github.tomakehurst:wiremock-jre8:2.35.0'
40+
testCompileOnly 'org.projectlombok:lombok:1.18.26'
41+
testAnnotationProcessor 'org.projectlombok:lombok:1.18.26'
42+
}
43+
44+
compileTestJava.ajc.options.aspectpath.from sourceSets.main.output
45+
46+
compileJava {
47+
sourceCompatibility = JavaVersion.VERSION_1_8
48+
targetCompatibility = JavaVersion.VERSION_1_8
49+
}
50+
51+
spotless {
52+
format 'misc', {
53+
target '*.gradle', '*.md', '.gitignore'
54+
55+
trimTrailingWhitespace()
56+
indentWithTabs()
57+
endWithNewline()
58+
}
59+
60+
java {
61+
importOrder()
62+
googleJavaFormat('1.7').aosp()
63+
removeUnusedImports()
64+
}
65+
}
66+
67+
test {
68+
outputs.upToDateWhen {false}
69+
useJUnitPlatform()
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package software.amazon.cloudwatchlogs.emf.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Repeatable;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* This annotation is used to put a count metric to CloudWatch Metrics. By default, when the
11+
* annotated method is called, the value 1.0 will be published with the metric name
12+
* "[ClassName].[methodName].Count". The value and metric name can be overridden, and the "applies"
13+
* field can be used to only publish for invocations when failures are/aren't thrown by the
14+
* annotated method.
15+
*/
16+
@Retention(RetentionPolicy.RUNTIME)
17+
@Target(ElementType.METHOD)
18+
@Repeatable(CountMetrics.class)
19+
public @interface CountMetric {
20+
String name() default "";
21+
22+
boolean logSuccess() default true;
23+
24+
Class<? extends Throwable>[] logExceptions() default {Throwable.class};
25+
26+
double value() default 1.0d;
27+
28+
String logger() default "_defaultLogger";
29+
30+
boolean flush() default false;
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package software.amazon.cloudwatchlogs.emf.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/** Allows multiple {@link CountMetric} annotations on a single method. */
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Target(ElementType.METHOD)
11+
public @interface CountMetrics {
12+
CountMetric[] value();
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package software.amazon.cloudwatchlogs.emf.annotations;
18+
19+
import java.util.HashMap;
20+
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
21+
22+
/** */
23+
public class MetricAnnotationMediator {
24+
public static MetricAnnotationMediator getInstance() {
25+
return SINGLETON;
26+
}
27+
28+
private static final MetricAnnotationMediator SINGLETON = new MetricAnnotationMediator();
29+
30+
private static final String defaultLoggerKey = "_defaultLogger";
31+
32+
// protected instead of private for testing purposes
33+
protected static HashMap<String, MetricsLogger> loggers;
34+
35+
private MetricAnnotationMediator() {
36+
loggers = new HashMap<>();
37+
loggers.put(defaultLoggerKey, new MetricsLogger());
38+
}
39+
40+
/** @return the default logger this singleton uses */
41+
public static MetricsLogger getDefaultLogger() {
42+
return loggers.get(defaultLoggerKey);
43+
}
44+
45+
/**
46+
* @param name the name of the logger to get
47+
* @return the logger with the specified name if it exists, otherwise will return the default
48+
* logger
49+
* @see MetricAnnotationMediator#getDefaultLogger() getDefaultLogger()
50+
*/
51+
public static MetricsLogger getLogger(String name) {
52+
if (name.isEmpty()) {
53+
return getDefaultLogger();
54+
}
55+
return loggers.getOrDefault(name, getDefaultLogger());
56+
}
57+
58+
/**
59+
* Adds a logger to this singleton if no other logger has the same name.
60+
*
61+
* @param name the desired name of the logger
62+
* @param logger the logger to be added
63+
* @return true if the logger was successfully added and false if annother logger already has
64+
* the same name
65+
*/
66+
public static boolean addLogger(String name, MetricsLogger logger) {
67+
if (loggers.containsKey(name)) {
68+
return false;
69+
}
70+
71+
loggers.put(name, logger);
72+
return true;
73+
}
74+
75+
/** Flushes all loggers added to this singleton */
76+
public static void flushAll() {
77+
for (MetricsLogger logger : loggers.values()) {
78+
logger.flush();
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)