com.deere.isg.work-tracker.examples:spring-example

A library to monitor threads and requests. It provides advanced logging capabilities, and protects the application's JVMs from too many requests and from long running requests that would eventually turn into zombies.

License

License

MIT
GroupId

GroupId

com.deere.isg.work-tracker.examples
ArtifactId

ArtifactId

spring-example
Last Version

Last Version

1.0.0-rc14
Release Date

Release Date

Type

Type

jar
Description

Description

A library to monitor threads and requests. It provides advanced logging capabilities, and protects the application's JVMs from too many requests and from long running requests that would eventually turn into zombies.

Download spring-example

How to add to project

<!-- https://jarcasting.com/artifacts/com.deere.isg.work-tracker.examples/spring-example/ -->
<dependency>
    <groupId>com.deere.isg.work-tracker.examples</groupId>
    <artifactId>spring-example</artifactId>
    <version>1.0.0-rc14</version>
</dependency>
// https://jarcasting.com/artifacts/com.deere.isg.work-tracker.examples/spring-example/
implementation 'com.deere.isg.work-tracker.examples:spring-example:1.0.0-rc14'
// https://jarcasting.com/artifacts/com.deere.isg.work-tracker.examples/spring-example/
implementation ("com.deere.isg.work-tracker.examples:spring-example:1.0.0-rc14")
'com.deere.isg.work-tracker.examples:spring-example:jar:1.0.0-rc14'
<dependency org="com.deere.isg.work-tracker.examples" name="spring-example" rev="1.0.0-rc14">
  <artifact name="spring-example" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.deere.isg.work-tracker.examples', module='spring-example', version='1.0.0-rc14')
)
libraryDependencies += "com.deere.isg.work-tracker.examples" % "spring-example" % "1.0.0-rc14"
[com.deere.isg.work-tracker.examples/spring-example "1.0.0-rc14"]

Dependencies

compile (10)

Group / Artifact Type Version
org.logback-extensions : logback-ext-spring jar 0.1.5
org.slf4j : slf4j-api jar 1.7.25
org.slf4j : jcl-over-slf4j jar 1.7.25
org.slf4j : jul-to-slf4j jar 1.7.25
org.slf4j : log4j-over-slf4j jar 1.7.25
org.springframework : spring-webmvc jar 5.0.8.RELEASE
com.deere.isg.work-tracker : work-tracker-spring jar 1.0.0-rc14
com.fasterxml.jackson.core : jackson-databind jar 2.9.8
org.codehaus.groovy : groovy-all jar 2.4.8
javax.servlet : javax.servlet-api jar 3.1.0

test (4)

Group / Artifact Type Version
org.springframework : spring-test jar 5.0.8.RELEASE
org.mockito : mockito-core jar 2.23.4
org.hamcrest : hamcrest-library jar 1.3
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Build Status Maven Central DepShield Badge

Work Tracker

A library to monitor threads and requests. It provides advanced logging capabilities, and protects the application's JVMs from too many requests and from long running requests that would eventually turn into zombies.

Features:

  1. Has the ability to log contextual thread metadata to Elasticsearch for web requests
  2. Checks if the number of threads do not exceed the maximum number of resources than the JVM permits (i.e. database connections, memory resources, etc.). RequestBouncer handles those checks and allows new threads to proceed only if they are within the Connection Limits.
  3. Kills threads that take too long to respond, aka Zombies
  4. Adds exception names to the logs of faulty requests for tracking bugs better, see RootCauseTurboFilter
  5. Provides contextual thread metadata for background tasks, see MdcExecutor

Setup

Common Configuration and Usage

The following instructions apply to all of work-tracker's support modules.

Whitelisting Jobs

If you expect a job to take longer than 5 minutes (i.e. uploading/downloading a file), you may want to whitelist that job. Use Work#setMaxTime(long) to update the time for Zombie detection.

Logback features specific to this library

This turbo filter adds the class name of the root cause and the cause to the MDC when an exception occurs. Those class names will exist until the request ends to provide an easy way to trace down a faulty request from the beginning of the exception to the end of that request.

turboFilter(RootCauseTurboFilter)

Optional configuration for field names:

//...
turboFilter(RootCauseTurboFilter) {
  causeFieldName = "cause_field_name" // Optional, if you want some other value for causeFieldName
  rootCauseFieldName = "root_cause_field_name" // Optional, if you want some other value for rootCauseFieldName
}

//...

This JSON Provider is necessary if ZombieDetector is used. This provider gets the thread name of the actual zombie work instead of the ZombieLogger class. Thus, making it easier to find out what work was zombie

Configuration:

encoder(LoggingEventCompositeJsonEncoder) {
   providers(LoggingEventJsonProviders) {
      //...
      mdc(MdcJsonProvider) {
         excludeMdcKeyName = 'thread_name' // Avoid the need to overwrite by threadName()
      }
      threadName(MdcThreadNameJsonProvider)
      //...
  }
}

See example


Logging Utilities

Using Logger to log information

private Logger logger = LoggerFactory.getLogger(CurrentClass.class);
//...
logger.info("This is an info log"); // log some information
logger.warn("This is a warn log"); // log a warning

try {
  //...
} catch(Exception e){
  logger.error("This is an error log", e); // log an error
}

Using putInContext to add context to logs

putInContext is a wrapper for the MDC (Mapped Diagnostic Context). It puts the key-value pair in the MDC and in the current work payload, thus, making sure that the ZombieDetector will have all the work metadata to log to Elasticsearch in the case where you need to track and debug your application for those Zombies.

If you want some context variables to persist for a given request across multiple logs, use the putInContext to add those context variables. For example:

// Use the OutstandingWork to put the values to the MDC for the current work
outstandingWork.putInContext("some_id", "some value");

Retrieving the OutstandingWork in the Java Module (i.e. using HttpServlet)

// Get the OutstandingWork from the Servlet Context
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    OutstandingWork<?> outstandingWork = (OutstandingWork<?>) request
        .getServletContext().getAttribute(OUTSTANDING_ATTR);
    //... add your values to the context using putInContext
}

To make life simpler, you can also have a utility class to put the context in the outstandingWork. See example, and its initialization

Retrieving the OutstandingWork in the Spring Module

// Using field injection
@Autowired
private OutstandingWork<? extends SpringWork> outstandingWork;
//...
// OR using constructor injection (use either one, but not both)
private OutstandingWork<? extends SpringWork> outstandingWork;
//...
public SomeClass(@Autowired OutstandingWork<? extends SpringWork> outstandingWork) {
  this.outstandingWork = outstandingWork;
}

Now, each of your subsequent logs will have those values in the MDC, unless you clear the MDC (and this library automatically clears the MDC at the end of each request to avoid having stale context).

Using Structured Arguments for individual log context

If some context variables are specific to a single log (i.e. the sender id for a certain message, elapsed time, etc.), use StructuredArguments.keyValue() to add those context variables. Those variables will not persist across all the logs but they will be available for that log only. For example:

logger.info("{} sent a message", keyValue("sender_id", "some sender value"));

keyValue will turn the message into "sender_id=some sender value sent a message", while also creating an index in Elasticsearch for sender_id to make it easier to search for a particular sender_id in Kibana.

Note: Make sure that the key for MDC and Structured Arguments is in snake_case (and lower case) as suggested by Elasticsearch's Naming Conventions. These naming conventions make it easy to search for those keys in Kibana as the keys will be optimized by Elastic Search.


Contributing to this library

Please see the Contribution Guidelines.

Running tests

mvn clean verify

Running example projects

Don't know how to start, have a look at these examples

Bump Version For Release

Run the following bash command and commit the change:

bash build/bump_version.sh MAJOR|MINOR|PATCH

Example:

bash build/bump_version.sh MINOR
com.deere.isg.work-tracker.examples

John Deere

Versions

Version
1.0.0-rc14
1.0.0-rc13
1.0.0-rc12
1.0.0-rc11
1.0.0-rc10
1.0.0-rc9
1.0.0-rc8
1.0.0-rc7
1.0.0-rc6
1.0.0-rc5
1.0.0-rc4
1.0.0-rc3