SLF4J Metrics publisher

SLF4J Metrics metrics

License

License

Categories

Categories

SLF4J Application Layer Libs Logging Metrics Application Testing & Monitoring Monitoring
GroupId

GroupId

com.societegenerale.commons
ArtifactId

ArtifactId

slf4j-metrics-publisher
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

SLF4J Metrics publisher
SLF4J Metrics metrics
Project URL

Project URL

https://github.com/societe-generale/slf4j-metrics-publisher
Source Code Management

Source Code Management

https://github.com/societe-generale/slf4j-metrics-publisher

Download slf4j-metrics-publisher

How to add to project

<!-- https://jarcasting.com/artifacts/com.societegenerale.commons/slf4j-metrics-publisher/ -->
<dependency>
    <groupId>com.societegenerale.commons</groupId>
    <artifactId>slf4j-metrics-publisher</artifactId>
    <version>1.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.societegenerale.commons/slf4j-metrics-publisher/
implementation 'com.societegenerale.commons:slf4j-metrics-publisher:1.0.2'
// https://jarcasting.com/artifacts/com.societegenerale.commons/slf4j-metrics-publisher/
implementation ("com.societegenerale.commons:slf4j-metrics-publisher:1.0.2")
'com.societegenerale.commons:slf4j-metrics-publisher:jar:1.0.2'
<dependency org="com.societegenerale.commons" name="slf4j-metrics-publisher" rev="1.0.2">
  <artifact name="slf4j-metrics-publisher" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.societegenerale.commons', module='slf4j-metrics-publisher', version='1.0.2')
)
libraryDependencies += "com.societegenerale.commons" % "slf4j-metrics-publisher" % "1.0.2"
[com.societegenerale.commons/slf4j-metrics-publisher "1.0.2"]

Dependencies

provided (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.30

test (5)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.2.3
ch.qos.logback : logback-core jar 1.2.3
com.google.guava : guava jar 29.0-jre
org.junit.jupiter : junit-jupiter-engine jar 5.6.2
org.assertj : assertj-core jar 3.16.1

Project Modules

There are no modules declared in this project.

slf4j-metrics-publisher

Build Status Coverage Status Maven Central

Context

This library is used when using Elastic stack, and sending the logs to Logstash using Logback with a config similar to this :

  <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>${LOGSTASH_URL}</destination>
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
           <customFields>{"service.name":"my-service-name",
              "service.hostname":"\${HOSTNAME}",
              "service.version": "${project_version}"}
           </customFields>
        </encoder>
        <keepAliveDuration>1 minutes</keepAliveDuration>
  </appender> 

With above configuration and a properly configured Elastic stack, your applicative logs are sent to logstash "wrapped" as a Json document, and you'll be able to browse them in Kibana.

But what if in addition to the applicative logs, you would also like to send custom metrics ?

Getting started

This library contains only a single class, but packaging it like this brings consistency to the projects that use it (instead of copy/pasting the class in all your projects).

Import the library in your Maven project by adding this dependency :

<dependency>
  <groupId>com.societegenerale.commons</groupId>
  <artifactId>slf4j-metrics-publisher</artifactId>
  <version>1.0.0</version>
</dependency>

Publishing the metric and its value(s)

Once the library is added to your classpath, creating and publishing events is as simple as this :

  Metric userLoggedInMetric=Metric.functional("user-logged-in");
  userLoggedInMetric.addAttribute("duration",duration);
  userLoggedInMetric.publish(); 
  • We first create the metric, giving it a type (functional) and a name (user-logged-in)
  • We then add an attribute to it (duration) : it's a key/value pair, the value being a String. The attributes are stored in a Map, and we can add as many entries as we want.
  • When "publishing" the metric, the attributes entries are read, and put in the MDC (see here for more infos), and a regular logging call is made. But it is "enriched" with all the key/value pairs from the MDC, ie our metric and its attributes.

Receiving the metric

The metric is "piggy-backing" on a regular log event, encoded in Json and sent to Logstash. It has special attributes that enable to configure a filter in Logstash and redirect the Json document to a special Elastic index (if that's what you want to do).

The Json document will have these basic attributes :

  • metricName : in our example above, it's user-logged-in
  • metricType : typically, TECHNICAL or FUNCTIONAL - but you can create metrics with custom types.

but also all the attributes that you've added in it.

Outcome

By storing these "metric documents" in a special ElasticSearch index, you can then make a Kibana dashboard showing them in few minutes, so for example the number of "user-logged-in" events, and the min/max/average/percentiles duration for it.

com.societegenerale.commons

Société Générale

Versions

Version
1.0.2
1.0.1
1.0.0