Add custom business tags

Updated at:

To query trace information using unique identifiers such as phone numbers or serial numbers, you can add this information to a span as a custom tag.

Prerequisites

  • The application must use the SOFABoot technology stack.

  • The tracer must meet the following conditions:

    • Version 3.0.5 or later

    • The active reporting mode must be used.

    • The business code must have an explicit dependency on the tracer.

      Note

      • Active reporting mode: This mode is enabled by default for tracer versions 3.0.5 and later. If you use an earlier version, upgrade it.

      • Explicit dependency on the tracer: This means the business class code must call a tracer class, such as SofaTraceContext. This operation is intrusive. Encapsulate the code properly.

Add method

To set a custom tag and its value, call a tracer class from your business class. The following code shows an example:

public void handlerXxx(Map<String, String> request) {
    SofaTraceContext ctx = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan span = ctx.getCurrentSpan();
// An explicit dependency on the tracer is intrusive. Ensure proper encapsulation.
    if (span != null) {
// Add the parameter to the custom tag: xxx_phone
        span.setTag("xxx_phone", request.get("phone"));
    }

// Process business logic
// ...
}

Verify the result

You can verify that a custom tag was added by entering the custom tag and its parameters under Business Custom Search Item in the Trace Search feature.

For example, set the custom tag to xxx_phone and the value to 123, as shown below:

Then, on the details page for the target trace, hover over the Service Information column. The Service Information Details pop-up window appears and displays the custom tag and its value, as shown below: