The background,

As microservices become more and more popular, the invocation relationships between our services become more and more complex, and we urgently need an APM tool to analyze the various performance indicators and invocation relationships in the system. At present, the mainstream APM tools are CAT, Zipkin, Pinpoint and SkyWalking, this article mainly briefly introduces the construction of SkyWalking.

2. Composition of SkyWalking

The main building blocks of SkyWalking. 1. Agent is mainly responsible for collecting various indicators and link data from the system and sending them to OAP service. 2. The OAP service receives the data sent by the Agent, stores it, performs analysis, and provides query and alarm functions. Storage and UI are responsible for storing data and viewing data.

Use Docker-compose to build an OAP and UI service

Version: '3' services: elasticsearch7: image: docker. Elastic. Co/elasticsearch/elasticsearch: 7.5.0 container_name: elasticsearch7 restart: always ports: - 9023:9200 environment: - discovery.type=single-node - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - TZ=Asia/Shanghai ulimits:  memlock: soft: -1 hard: -1 networks: - skywalking volumes: - elasticsearch7:/usr/share/elasticsearch/data oap: image: Apache/Skywalking -oap-server:8.0.1-es7 container_name: oAP depends_on: -ElasticSearch7 links: - elasticsearch7 restart: always ports: - 9022:11800 - 9021:12800 networks: - skywalking volumes: -./ext-config:/skywalking/ext-config UI: image: apache/skywalking- UI :8.0.1 Container_name: UI Depends_on: -oap links: - oap restart: always ports: - 9020:8080 environment: SW_OAP_ADDRESS: oap:12800 networks: - skywalking networks: skywalking: driver: bridge volumes: elasticsearch7: driver: localCopy the code

1, docker-compose file directory

Skywalking ├── ext-config │ ├─ ch.pdf │ ├─ ch.pdfCopy the code

2, access,

http://localhost:9020

3. Matters needing attention

If we want to overwrite the configuration files in the /skywalking/config directory in the OAP image, we can mount a /skywalking/ext-config directory in docker and drop the configuration files into this directory. 2, if we want to overwrite the JAR in the/Skywalking /oap-libs directory in the OAP image, we can mount a/Skywalking /ext-libs directory in docker and throw the new JAR package into this directory, but the existing JAR package cannot be overwritten. 3. Version 8.0.1 is used, and data is persisted to ES7

Use traceId for global log tracing

Solution a:

1. Introduce dependencies

<dependency> <groupId>org.apache.skywalking</groupId> <artifactId>apm-toolkit-logback-1.x</artifactId> The < version > 8.0.1 < / version > < / dependency >Copy the code

2. Modify the logback. XML file

<? The XML version = "1.0" encoding = "utf-8"? > <! -- Logback Configuration. --> <configuration debug="false"> <! - ConsoleAppender: The log output to the console - > < appender name = "STDOUT" class = "ch. Qos. Logback. Core. ConsoleAppender" > < encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.TraceIdMDCPatternLogbackLayout"> <Pattern><! [CDATA[ %clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} [%X{tid}] %clr(${LOG_LEVEL_PATTERN:-%5p}) % the CLR (${PID: -}) {magenta} % CLR (-) {abbreviation} % CLR ([% 15.15 t]) {abbreviation} % CLR (% 40.40 logger {39}) CLR (:) {abbreviation} {cyan} % %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx} ]]></Pattern> </layout> </encoder> </appender> <root level="INFO"> <appender-ref ref="STDOUT"/> </root> </configuration>Copy the code

Scheme 2

See the following link github.com/apache/skyw…

Use in idea or JAR

Run the following command to fold the agentJar into a line: java-javaAgent :(Location of the AgentJar package)(eg: /Users/huan/soft/apache-skywalking-apm-bin-es7/agent/skywalking-agent.jar ) - Dskywalking. Agent. The service_name = XXXXX - service - Dskywalking. Collector. Backend_service = 127.0.0.1:9022 - jar XXXX. The jarCopy the code

-javaagentSpecify the location of the Agent JAR package

-Dskywalking.agent.service_nameSpecifying a service name

-Dskywalking.collector.backend_serviceSpecifies the ADDRESS of the OAP service

Skywalking directory explanation

Apache-skywalking -apm-bin-es7 ├── LICENSE ├─ NOTICE ├─ readme.txt ├─ Agent ├─ activations ├─ bootstrap-plugins ├─ Config -- agent configuration file, As we step on using the Dskywalking. Agent. Service_name configuration of these ├ ─ ─ logs ├ ─ ─ optional plugins - optional plug-in (will be optional - jars on the plugins plugins directory ├─ Skywalk-agent.jar ├─ Skywalk-agent.jar ├─ oap/ UI Config config file ├── licenses ├── OAP-Libs ├─ Tools ├─ webAppCopy the code

Access the SkyWalking interface

Eight, in actual combat

1. Ignore certain urls that are not tracked

Step 1: Move apm-trace-ignore-plugin-8.0.1.jar from optional-plugins to plugins directory

2. Configure the ignore URL

Method 1: Create the apm-trace-ignore-plugin.config file in the agent/config directory

Trace.ignore_path =${SW_AGENT_TRACE_IGNORE_PATH: URL to ignore} eg: trace.ignore_path=${SW_AGENT_TRACE_IGNORE_PATH:/ XXX /**}Copy the code

Method 2: Directly use the environment variable -dSkywalk.trace.ignore_path = THE URL path to be ignored

Note: 1, ignore path is supported ant style. 2. Ignore multiple urls and use commas to separate them.

2, trace the child thread information

1. Introduce dependencies

<dependency> <groupId>org.apache.skywalking</groupId> <artifactId>apm-toolkit-trace</artifactId> The < version > 8.0.1 < / version > < / dependency >Copy the code

2, use the @ TraceCrossThread comment or use SupplierWrapper/RunnableWrapper/TraceCrossThread

@getMapping ("tractThread") public String tractThread() {log.info(" prepare your thread information "); New Thread(runnableWrapper.of (() -> log.info(" child Thread information ")).start(); return "trace thread"; }Copy the code

This demoRunnableWrapper.ofpackagingRunnableThreads.

3, if a method SkyWalking does not trace, but wants to trace and output some additional tag information, etc

1. Introduce dependencies

<dependency> <groupId>org.apache.skywalking</groupId> <artifactId>apm-toolkit-trace</artifactId> The < version > 8.0.1 < / version > < / dependency >Copy the code

2. Use the @Trace annotation for any method you want to add

@GetMapping("tractAnnotation") public User traceAnnotation( @RequestParam("name") String name ) { Log.info (" received parameter from front end :[{}]", name); User user = trace(name); ActiveSpan.tag("new-tag", user.toString()); Activespan.info (" Output information "); log.info("tractId:[{}]", TraceContext.traceId()); return user; } @tag ({@tag (key = "arg[0]"), @tag (key = "arg[0]"), @tag (key = "arg[0]"), Value = "returnedobj. name")}) private User trace(String name) {log.info(" if this method was not collected by SkyWalking, but needs to be collected again, You can annotate it with @trace "); User user = new User(); User.setname (" created name "); return user; }Copy the code

4. Customize display service instances

The default service instanceName is uuid@hostname, which is not always easy to distinguish at some point because we want a custom instanceName name

1. Default implementation

2. Custom implementation

use- Dskywalking. Agent. Instance_name = custom service nameCan.

5. Set the record expiration time

Modify theapplication.ymlInformation in the configuration file.

6. Processing when other agents are used

1, problem,

When we use it with other agents, such as Arthas, the other agents may not work so well.

2. Solutions

Add the following JVM parameters to the jar program startup command

-Dskywalking.agent.is_cache_enhanced_class=true -Dskywalking.agent.class_cache_mode=MEMORY
Copy the code

3. Reference documents

Github.com/apache/skyw…

Nine, the project source code

Docker – compose. Yml file gitee.com/huan1993/co… Java code gitee.com/huan1993/sk…

10. Reference links

The docker SkyWalking website skywalking.apache.org/zh/ SkyWalking github.com/apache/skyw making address… Elasticsearch www.elastic.co/guide/en/el… Skywalking skyapm Chinese documents. Making. IO/document – cn… The agent config github.com/apache/skyw… Skywalking is used with other agents