Skip to content

Monitoring

Tomasz Kowalczewski edited this page Jul 2, 2021 · 2 revisions

Introduction

Tjahzi tracks its operational statistics in many internal counters. It tracks http errors, timeouts and tcp errors in separate counters. It also records exceptions and their stack traces. If you want to get access to them or expose them to your monitoring system this is the instruction on how to do that.

Enable monitoring in Log4j

  1. First you need to get hold of the appender from Log4j logging system:

    Click to toggle code example

    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.apache.logging.log4j.core.LoggerContext;
    
    ...
    
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    LokiAppender loki = context.getConfiguration().getAppender("Loki");

    Where "Loki" is the name of the appender you specified in the configuration file:

    <appenders>
            <Loki name="Loki">
            ...
            </Loki>
    </appenders>
  2. Then you can install a new MonitoringModule

    loki.setMonitoringModule(...)
  3. You can use simple StandardMonitoringModule that tracks events in counters that will have access to.

      StandardMonitoringModule monitoringModule = new StandardMonitoringModule()
      loki.setMonitoringModule(monitoringModule)
  4. You can implement your own or use Dropwizard based implementation.

Enable monitoring in Logback

  1. First you need to get hold of the appender from Logback logging system. It is a little more involved than with Log4

    Click to toggle code example

    import ch.qos.logback.classic.LoggerContext;
    import pl.tkowalcz.tjahzi.logback.LokiAppender;
    
    import java.util.Spliterators;
    import java.util.stream.StreamSupport;
    
    ...
    
        public static LokiAppender getLokiAppender(LoggerContext context) {
            return (LokiAppender) context
                    .getLoggerList()
                    .stream()
                    .flatMap(
                            logger ->
                                    StreamSupport.stream(
                                            Spliterators.spliteratorUnknownSize(logger.iteratorForAppenders(), 0),
                                            false
                                    )
                    )
                    .filter(appender -> appender instanceof LokiAppender)
                    .findAny()
                    .orElseThrow(() -> new AssertionError("Expected to find Loki appender"));
        }

    Where "Loki" is the name of the appender you specified in the configuration file:

    <appender name="Loki" class="pl.tkowalcz.tjahzi.logback.LokiAppender">
        ...
    </appender>
  2. Follow steps 2+ from Log4j section.

Integration with Dropwizard library

If you are using Dropwizard library for monitoring your application then Tjahzi comes with implmeentation of monitoring module that integrates with MetricRegistry.

    loki.setMonitoringModule(
                new DropwizardMonitoringModule(
                        metricRegistry,
                        "appender.loki"
                )
        );

Where "appender.loki is a prefix added to all metric names that Tjahzi will create.