Microservice architecture | 10.2 using Papertrail to realize log aggregation

Posted by zgkhoo on Sun, 06 Feb 2022 05:53:14 +0100

preface

reference material:
<Spring Microservices in Action>
Principle and practice of Spring Cloud Alibaba microservice
"Spring cloud framework development tutorial in Silicon Valley of station B" Zhou Yang

Papertrail is a cloud based service (based on free value-added), which allows developers to aggregate log data from multiple sources into a single searchable database. The solutions that developers can choose for log aggregation include internal deployment solutions, cloud based solutions, open source solutions and commercial solutions;

1. Papertrail Basics

1.1 Papertrail features

  • free
  • Free value-added model / layered pricing model;
  • It is very easy to create and work with Docker container;
  • Cloud based;

1.2 what is Papertrail

  • Papertrail is a log analyzer of Windows, which can automatically scan log data. When scanning log data, you can select the information you want the scanning results to display. For example, you can choose whether the scan includes IP address, e-mail address, GUID/UUID, HTTP(s)URL, domain, host, file name and reference text;
  • A key point of Papertrail is the resolution of events. To help you find the cause of security events faster, you can filter log events by time, source or selected custom fields. Filtering logs in this way can eliminate irrelevant data and focus on the most important data;
  • Another similar filtering option provided by Papertrail allows you to detect the trend of log data. Events can be filtered by source, data, severity level, tool or message content. After the filtered search is completed, you will be able to view the result chart at the bottom of the screen;
  • Papertrail is ideal for an easy to deploy log analyzer. It provides a free plan that allows you to monitor up to 100MB of data per month;

2. Example of log aggregation using Papertrail

2.1 create Papertrail account and configure syslog connector

  • visit: https://papertrailapp.com
  • Register an account;
  • Create a log record;
  • Complete the creation;
    • You need to pay attention to your logs will go to logs papertrailapp. com:43858 and appear in Events. This sentence needs to be configured in 2.2;

Redirect Papertrail output to Docker2

  • The Docker daemon passes a file called Docker Unix socket of sock to communicate with all Docker containers;
  • Each container can be connected to docker Sock and receive all messages generated by all other containers running on the server;
  • You need to send a message to docker compose Add related configuration to YML file:
  logspout:
    image: gliderlabs/logspout
    command: syslog://logs5.papertrailapp.com:43858
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
  • Notice the logspout The command attribute needs to be consistent with that provided by Papertrail above;
  • Start the Docker environment and you can see the following log aggregation:

2.3 search for the tracking ID of Spring Cloud Sleuth in Papertrail

  • Search Sleuth's tracking ID in the search box at the bottom of the figure above to see the relevant log information of all services;

2.4 add Association ID to HTTP response using Zuul

1. In the POM of Zuul Add Sleuth dependency in XML configuration file

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

2. Modify post filter

@Component
public class ResponseFilter extends ZuulFilter{
    private static final int  FILTER_ORDER=1;
    private static final boolean  SHOULD_FILTER=true;
    private static final Logger logger = LoggerFactory.getLogger(ResponseFilter.class);

    //Entry point to access tracking ID and span ID information
    @Autowired
    Tracer tracer;
    @Override
    public String filterType() {
        return "post";
    }
    @Override
    public int filterOrder() {
        return FILTER_ORDER;
    }
    @Override
    public boolean shouldFilter() {
        return SHOULD_FILTER;
    }
    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        //Add a new HTTP response header TMX correlation ID, which contains the tracking ID of Spring Cloud Sleuth
        ctx.getResponse().addHeader("tmx-correlation-id", tracer.getCurrentSpan().traceIdString());
        return null;
    }
}

last

Newcomer production, if there are mistakes, welcome to point out, thank you very much! Welcome to the official account and share some more everyday things. If you need to reprint, please mark the source!

Topics: Spring Cloud microservice