This is the 6th day of my participation in the August More Text Challenge

background

TraceId is often used in BUG debugging recently. The general scenario is like this: when a system developer calls your service, he finds that the interface data return is abnormal, so you need to help solve the problem.

But how do you find the logs for the interface calls? How do you chain the whole call chain?

The answer is SkyWalking, although there are other tools that do the same thing and are better, but SkyWalking for today.

SkyWalking profile

An overview of the

SkyWalking is an observability analysis platform and application performance management system. Integrated tracking, metrics and recording solutions.

SkyWalking is an application performance monitoring tool for distributed systems designed for microservices, cloud native, and container-based (Docker, Kubernetes, Mesos) architectures.

function

  • Service performance monitoring, including network, disk, interface response, etc.
  • Multi-language automatic probe. Supports Java,.net Core, PHP, NodeJS, Golang, LUA, C++, Python.
  • Lightweight and efficient. No big data platform, and a lot of server resources.
  • Excellent visual solutions, service topology, etc

SkyWalking use

Environment introduction

  1. Docker, docker-compose based on Mac. If you need to use it, please install it by yourself. Other operating system functions are similar.

  2. For springBoot-based projects, there is no excessive integration of components only SpringBoot.

  3. The project Java version is 11, and Java8 is a bit old if you can upgrade it.

SkyWalking structures,

Use Docker directly to start the project, but here are a few caveats:

  • The Configuration of Skywalking as default can be modified by mapping the configuration file
  • Skywalking’s default storage engine is H2, and since this is just for demonstration purposes, production chooses ES
  • The Skywalking UI is powerful and worth exploring slowly
version: '3'
services:
  oap:
    image: Apache/skywalking - oap - server: 8.4.0 - es6
    container_name: oap
    restart: always
    ports:
      - 11800: 11800 # gRPC port for agent to report data
      - 12800: 12800 HTTP port for UI to read data
  skywaling-ui:
    image: Apache/skywalking - UI: 8.4.0
    container_name: ui
    depends_on:
      - oap
    links:
      - oap
    ports:
      - 8080: 8080
    environment:
      - SW_OAP_ADDRESS=oap:12800
Copy the code

After successful startup, you should open http://localhost:8080/ to take you to the SkyWalking administration page.

The diagram below:

SpringBoot access

    1. The SpringBoot project is initialized

The steps to initialize the SpringBoot project are not described here. If you need to initialize the SpringBoot project, you can see SpringBoot Quickstart.

    1. Configuration SkyWalking Agent

Download Skywalking Agent package:

Wget HTTP: / / https://archive.apache.org/dist/skywalking/8.4.0/apache-skywalking-apm-8.4.0.tar.gzCopy the code

Decompressed configuration file:

/agent/ skywalk-agent.jar

# 2. Modify the config file agent/config/agent. The config

Configure the service name
agent.service_name=${SW_AGENT_NAME:winter}

Service collection address
collector.backend_service=The ${SW_AGENT_COLLECTOR_BACKEND_SERVICES: 127.0.0.1:11800}
Copy the code
    1. Modify Log4j configuration

The main thing is to change the format of log printing, and the rest is as configured. The modifications are placed below:

<Properties>
    <! Format output: %date indicates the date, %thread indicates the name of the thread, %-5level indicates the level of the log message, % MSG indicates the log message, %n indicates the newline character -- -->
    <! -- %logger{36} indicates a maximum of 36 characters in a logger name -->
    <! Write traceId -->
    <property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] %traceId %-5level %logger{36} - %msg%n" />
    <! Define log store path -->
    <property name="FILE_PATH" value="./logs" />
    <property name="FILE_NAME" value="winter" />
</Properties>

<appenders>

    <console name="Console" target="SYSTEM_OUT">
        <! -- Output log format -->
        <PatternLayout pattern="${LOG_PATTERN}"/>
        <! -- The console only outputs messages of level and above (onMatch). The rest will be rejected (onMismatch) -->
        <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
    </console>
        <! -- There are many more -->
        <! -- There are many more -->
        <! -- There are many more -->
        <! -- There are many more -->
    
<appenders>
Copy the code
    1. Start the project

There are two ways to start a project, but the specific parameters are the same. The detailed parameters are as follows:

# Skywalking - Agent location
-javaagent:./apache-skywalking-apm-bin/agent/skywalking-agent.jar

# service name
-Dskywalking.agent.service_name=winter

The back-end address of the collection- Dskywalking. Collector. Backend_service = 127.0.0.1:11800Copy the code

Idea startup configuration is shown as follows:

The Jar package is deployed as follows:

java \ -javaagent:./apache-skywalking-apm-bin/agent/skywalking-agent.jar \ -Dskywalking.agent.service_name=winter \ - Dskywalking. Collector. Backend_service = 127.0.0.1:11800 - jar \. / target/winter - 0.0.1 - the SNAPSHOT. The jarCopy the code

Results show

SkyWalking Management interface

Take a look at the SkyWalking DashBoard, and it’s pretty impressive:

Of course, there are many other features in SkyWalking’s management interface that you can explore on your own.

Log file format

The data in the red box below is what we want traceId to look like:

Note: The best way to centrally manage logs is in Kibana. Elasticsearch allows you to query logs efficiently and locate problems quickly.