de.inoxio:spring-cloudwatch-metrics

A java-spring library to push metrics to cloudwatch

License

License

Categories

Categories

Metrics Application Testing & Monitoring Monitoring
GroupId

GroupId

de.inoxio
ArtifactId

ArtifactId

spring-cloudwatch-metrics
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

de.inoxio:spring-cloudwatch-metrics
A java-spring library to push metrics to cloudwatch
Project URL

Project URL

https://github.com/inoxio/spring-cloudwatch-metrics
Source Code Management

Source Code Management

http://github.com/inoxio/spring-cloudwatch-metrics/tree/master

Download spring-cloudwatch-metrics

How to add to project

<!-- https://jarcasting.com/artifacts/de.inoxio/spring-cloudwatch-metrics/ -->
<dependency>
    <groupId>de.inoxio</groupId>
    <artifactId>spring-cloudwatch-metrics</artifactId>
    <version>1.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/de.inoxio/spring-cloudwatch-metrics/
implementation 'de.inoxio:spring-cloudwatch-metrics:1.2.0'
// https://jarcasting.com/artifacts/de.inoxio/spring-cloudwatch-metrics/
implementation ("de.inoxio:spring-cloudwatch-metrics:1.2.0")
'de.inoxio:spring-cloudwatch-metrics:jar:1.2.0'
<dependency org="de.inoxio" name="spring-cloudwatch-metrics" rev="1.2.0">
  <artifact name="spring-cloudwatch-metrics" type="jar" />
</dependency>
@Grapes(
@Grab(group='de.inoxio', module='spring-cloudwatch-metrics', version='1.2.0')
)
libraryDependencies += "de.inoxio" % "spring-cloudwatch-metrics" % "1.2.0"
[de.inoxio/spring-cloudwatch-metrics "1.2.0"]

Dependencies

runtime (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter jar
org.springframework.boot : spring-boot-starter-json jar
software.amazon.awssdk : cloudwatch jar 2.10.72

Project Modules

There are no modules declared in this project.

spring-cloudwatch-metrics

A java-spring library to push metrics to cloudwatch

If you have any questions please open an issue ticket!

Usage

Add this to your build.gradle.kts

dependencies {
    implementation("de.inoxio:spring-cloudwatch-metrics:1.1.0")
}

Inject the CloudwatchDAO from your service as usual:

@Service
public class SomeServiceImpl implements SomeService {
    private final CloudwatchDAO cloudwatchDAO;
    
    @Autowired
    public SomeServiceImpl(final CloudwatchDAO cloudwatchDAO) {
        notNull(cloudwatchDAO, "CloudwatchDAO must not be null!");
        this.cloudwatchDAO = cloudwatchDAO;
    }
    
    /**
    * Optional: If you want to add one or more dimensions to your metrics 
    */
    @PostConstruct
    private void addDimension() {
        cloudwatchDAO.addDimension(dimensionKeyPairBuilder().name("DimensionName")
                                                            .value("DimensionValue")
                                                            .build());
    }
    
    /**
    * To push used heap memory and cpu usage to cloudwatch 
    * have a method like this in your service that calls
    * the CloudwatchDAO. Intended to be invoked by a scheduler.
    */
    public void pushMetrics() {
        final var memoryBean = ManagementFactory.getMemoryMXBean();
        final var operatingSystemBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        
        cloudwatchDAO.pushMetrics(metricKeyPairBuilder().withName("HeapMemoryUsed")
                                                        .withValue(memoryBean.getHeapMemoryUsage().getUsed())
                                                        .build(),
                                  metricKeyPairBuilder().withName("ProcessCpuLoad")
                                                        .withValue(operatingSystemBean.getProcessCpuLoad() * MAX_PERCENT)
                                                        .build());
    }
}

Add the following properties to your project:

aws:
  namespace: Namespace                 # Namespace where the metrics will be pushed to
  metric-prefix: AppPrefix             # All Metrics will get this prefix. Final name will be AppPrefixHeapMemoryUsed
  dashboard-name: some-dashboard-name  # Optional: Set it to the dashboard name you want graphs to be annotated on 
                                       #           server start. It will add a vertical annotation to all graphs with
                                       #           metrics that start with 'metric-prefix'

Release

Change version in build.gradle.kts, README.md and issue:

./gradlew bintrayUpload
de.inoxio

inoxio Quality Services GmbH

Versions

Version
1.2.0
1.1.0
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.1.0