Spring MVC instrumentation

更新时间:
复制 MD 格式

This document describes how to use SOFATracer to add instrumentation to Spring MVC.

If you have a simple Spring Web project built on SOFABoot, follow these steps:

  1. Import the Tracer dependency

  2. Add a controller

  3. Run the project

  4. View logs

Import the Tracer dependency

Add the following Tracer dependency to your SOFABoot Web project:

<dependency>
      <groupId>com.alipay.sofa</groupId>
      <artifactId>tracer-enterprise-sofa-boot-starter</artifactId>
</dependency>

After you add the Tracer starter dependency, you can customize the behavior of Tracer by adding configuration items to the SOFABoot global configuration file. For more information, see Tracer configuration items.

Add a controller

If your Web project does not have a controller built on the Spring MVC framework, you can add one as shown in the following example. If you already have a controller, you can access the service directly.

@RestController
public class SampleRestController {

    private static final String template = "Hello, %s!";

    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        Greeting greeting = new Greeting();
        greeting.setSuccess(true);
        greeting.setId(counter.incrementAndGet());
        greeting.setContent(String.format(template, name));
        return greeting;
    }

    public static class Greeting {

        private boolean success = false;
        private long id;
        private String content;

        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }
}

Run the project

Import the SOFABoot project into an IDE. After the project compiles, run the `main` method to start the application. For example, to access the REST service with the controller added in the previous step, enter http://localhost:8080/greeting in your browser. The result is similar to the following:

{
    success:true,
    id:1,
    content:"Hello, World!"
}

View logs

You can define the log output directory in the `application.properties` configuration file. For example, set the log directory to `./logs` and the application name to `spring.application.name=mvc-client`. You will then see log files in the project's root directory with a structure similar to the following:

-- tracelog
|-- spring-mvc-digest.log
|-- spring-mvc-stat.log

You can open `spring-mvc-digest.log` to view the output. The following is an example of a log record:

2018-07-1720:01:34.719,mvc-client,0a0fe91a1531828894436100149692,0,http://localhost:8080/greeting,GET,200,-1B,49B,281ms,http-nio-8080-exec-1,

For a description of each output field, see Log format > Spring MVC logs.

Tracer configuration items

SOFATracer configuration item

Description

Default value

logging.path

Log output directory

SOFATracer prioritizes outputting to the `logging.path` directory. If this directory is not configured, logs are output to `${user.home}` by default.

com.alipay.sofa.tracer.disableDigestLog

Disables digest log printing for all components integrated with SOFATracer.

false

com.alipay.sofa.tracer.disableConfiguration[${logType}]

Disables digest log printing for a specific SOFATracer component log type. `${logType}` refers to a specific log type, such as `spring-mvc-digest.log`.

false

com.alipay.sofa.tracer.tracerGlobalRollingPolicy

The rolling policy for SOFATracer logs.

`.yyyy-MM-dd`: Rolls logs daily. `.yyyy-MM-dd_HH`: Rolls logs hourly. If not configured, logs roll daily by default.

com.alipay.sofa.tracer.tracerGlobalLogReserveDay

The number of days to retain SOFATracer logs.

The default retention period is 7 days.

com.alipay.sofa.tracer.statLogInterval

The interval for statistical logs, in seconds.

Statistical logs are output every 60 seconds by default.

com.alipay.sofa.tracer.baggageMaxLength

Maximum pass-through data length

The default value is 1024.

com.alipay.sofa.tracer.springmvc.filterOrder

The order in which the SOFATracer filter for Spring MVC takes effect.

-2147483647 (org.springframework.core.Ordered#HIGHEST_PRECEDENCE + 1)

com.alipay.sofa.tracer.springmvc.urlPatterns

The URL pattern path for which the SOFATracer filter for Spring MVC takes effect.

/* Applies to all. */