-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added annotations package for annotation metrics
- Loading branch information
Showing
11 changed files
with
780 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,70 @@ | ||
plugins { | ||
id 'java' | ||
id 'java-library' | ||
id "io.freefair.aspectj.post-compile-weaving" version "6.4.3" | ||
id 'com.diffplug.spotless' version '5.8.2' | ||
} | ||
|
||
repositories { | ||
jcenter() | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation rootProject | ||
|
||
annotationProcessor 'org.projectlombok:lombok:1.18.24' | ||
compileOnly 'org.projectlombok:lombok:1.18.12' | ||
|
||
implementation "org.aspectj:aspectjrt:1.9.22" | ||
implementation "org.aspectj:aspectjweaver:1.9.8.RC3" | ||
|
||
implementation 'com.fasterxml.jackson.core:jackson-core:2.14.2' | ||
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.14.2' | ||
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.2' | ||
implementation 'org.slf4j:slf4j-api:2.0.6' | ||
implementation 'org.javatuples:javatuples:1.2' | ||
implementation 'org.apache.commons:commons-lang3:3.12.0' | ||
|
||
// Use JUnit test framework | ||
testImplementation 'software.amazon.awssdk:cloudwatch:2.20.13' | ||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2' | ||
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.2' | ||
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.2' | ||
testImplementation 'org.junit.vintage:junit-vintage-engine:5.9.2' | ||
testImplementation 'org.mockito:mockito-core:2.+' | ||
testImplementation 'org.powermock:powermock-module-junit4:2.0.9' | ||
testImplementation 'org.powermock:powermock-api-mockito2:2.0.9' | ||
testImplementation 'com.github.javafaker:javafaker:1.0.2' | ||
testImplementation 'com.github.tomakehurst:wiremock-jre8:2.35.0' | ||
testCompileOnly 'org.projectlombok:lombok:1.18.26' | ||
testAnnotationProcessor 'org.projectlombok:lombok:1.18.26' | ||
} | ||
|
||
compileTestJava.ajc.options.aspectpath.from sourceSets.main.output | ||
|
||
compileJava { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
spotless { | ||
format 'misc', { | ||
target '*.gradle', '*.md', '.gitignore' | ||
|
||
trimTrailingWhitespace() | ||
indentWithTabs() | ||
endWithNewline() | ||
} | ||
|
||
java { | ||
importOrder() | ||
googleJavaFormat('1.7').aosp() | ||
removeUnusedImports() | ||
} | ||
} | ||
|
||
test { | ||
outputs.upToDateWhen {false} | ||
useJUnitPlatform() | ||
} |
31 changes: 31 additions & 0 deletions
31
annotations/src/main/java/software/amazon/cloudwatchlogs/emf/annotations/CountMetric.java
This file contains 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,31 @@ | ||
package software.amazon.cloudwatchlogs.emf.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* This annotation is used to put a count metric to CloudWatch Metrics. By default, when the | ||
* annotated method is called, the value 1.0 will be published with the metric name | ||
* "[ClassName].[methodName].Count". The value and metric name can be overridden, and the "applies" | ||
* field can be used to only publish for invocations when failures are/aren't thrown by the | ||
* annotated method. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
@Repeatable(CountMetrics.class) | ||
public @interface CountMetric { | ||
String name() default ""; | ||
|
||
boolean logSuccess() default true; | ||
|
||
Class<? extends Throwable>[] logExceptions() default {Throwable.class}; | ||
|
||
double value() default 1.0d; | ||
|
||
String logger() default "_defaultLogger"; | ||
|
||
boolean flush() default false; | ||
} |
13 changes: 13 additions & 0 deletions
13
annotations/src/main/java/software/amazon/cloudwatchlogs/emf/annotations/CountMetrics.java
This file contains 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,13 @@ | ||
package software.amazon.cloudwatchlogs.emf.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** Allows multiple {@link CountMetric} annotations on a single method. */ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface CountMetrics { | ||
CountMetric[] value(); | ||
} |
81 changes: 81 additions & 0 deletions
81
...rc/main/java/software/amazon/cloudwatchlogs/emf/annotations/MetricAnnotationMediator.java
This file contains 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,81 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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.cloudwatchlogs.emf.annotations; | ||
|
||
import java.util.HashMap; | ||
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger; | ||
|
||
/** */ | ||
public class MetricAnnotationMediator { | ||
public static MetricAnnotationMediator getInstance() { | ||
return SINGLETON; | ||
} | ||
|
||
private static final MetricAnnotationMediator SINGLETON = new MetricAnnotationMediator(); | ||
|
||
private static final String defaultLoggerKey = "_defaultLogger"; | ||
|
||
// protected instead of private for testing purposes | ||
protected static HashMap<String, MetricsLogger> loggers; | ||
|
||
private MetricAnnotationMediator() { | ||
loggers = new HashMap<>(); | ||
loggers.put(defaultLoggerKey, new MetricsLogger()); | ||
} | ||
|
||
/** @return the default logger this singleton uses */ | ||
public static MetricsLogger getDefaultLogger() { | ||
return loggers.get(defaultLoggerKey); | ||
} | ||
|
||
/** | ||
* @param name the name of the logger to get | ||
* @return the logger with the specified name if it exists, otherwise will return the default | ||
* logger | ||
* @see MetricAnnotationMediator#getDefaultLogger() getDefaultLogger() | ||
*/ | ||
public static MetricsLogger getLogger(String name) { | ||
if (name.isEmpty()) { | ||
return getDefaultLogger(); | ||
} | ||
return loggers.getOrDefault(name, getDefaultLogger()); | ||
} | ||
|
||
/** | ||
* Adds a logger to this singleton if no other logger has the same name. | ||
* | ||
* @param name the desired name of the logger | ||
* @param logger the logger to be added | ||
* @return true if the logger was successfully added and false if annother logger already has | ||
* the same name | ||
*/ | ||
public static boolean addLogger(String name, MetricsLogger logger) { | ||
if (loggers.containsKey(name)) { | ||
return false; | ||
} | ||
|
||
loggers.put(name, logger); | ||
return true; | ||
} | ||
|
||
/** Flushes all loggers added to this singleton */ | ||
public static void flushAll() { | ||
for (MetricsLogger logger : loggers.values()) { | ||
logger.flush(); | ||
} | ||
} | ||
} |
Oops, something went wrong.