public final class HiTrace extends Object
You can set FLAG constants to trace synchronous or asynchronous service calls,
obtain the service call timestamps at the sender and receiver,
and display trace information such as HiTraceId in logs.
Example code for starting the trace of a specified service flow by using the default trace flags:
HiTraceId traceId = HiTrace.begin("MyServiceName", HiTrace.HITRACE_FLAG_DEFAULT);
......
HiTrace.end(traceId);
HiTraceId| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
HITRACE_FLAG_DEFAULT |
Indicates default trace flags of service calls.
|
static int |
HITRACE_FLAG_DONOT_CREATE_SPAN |
Creates no spans in a trace task.
|
static int |
HITRACE_FLAG_DONOT_ENABLE_LOG |
Provides no trace information in log.
|
static int |
HITRACE_FLAG_INCLUDE_ASYNC |
Traces asynchronous service calls.
|
static int |
HITRACE_FLAG_NO_BE_INFO |
Provides no prompt information about the start and end of a trace task.
|
static int |
HITRACE_FLAG_TP_INFO |
Enables event tracking.
|
static int |
HITRACE_SLICE_CLIENT |
Indicates the beginning of a time slice trace event on the client side in distributed call chain trace.
|
static int |
HITRACE_SLICE_GENERAL |
Indicates the beginning of a general time slice trace event in distributed call chain trace.
|
static int |
HITRACE_SLICE_SERVER |
Indicates the beginning of a time slice trace event on the server side in distributed call chain trace.
|
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
static HiTraceId |
begin(String name,
int flags) |
Starts tracing a service call chain.
|
static void |
beginSlice(HiTraceId traceId,
int type,
String name,
String args) |
Starts a distributed time slice trace event.
|
static void |
beginSlice(HiTraceId traceId,
String name,
String args) |
Starts a general time slice trace event.
|
static void |
clearId() |
Clears the trace ID of the current thread.
|
static HiTraceId |
createSpan() |
Creates a trace ID for the next span based on the trace ID of the current thread.
|
static void |
end(HiTraceId hiTraceId) |
Stops tracing an ongoing service call chain based on
HiTraceId. |
static void |
endSlice() |
Ends a time slice trace event.
|
static HiTraceId |
getId() |
Get trace id of current thread, and return a invalid trace id if no
trace id belong to current thread.
|
static boolean |
isTraceEnabled() |
Checks whether HiTrace is enabled for the current application.
|
static void |
setId(HiTraceId id) |
Sets the trace ID of the current thread.
|
static void |
traceValue(HiTraceId traceId,
String name,
long value) |
Outputs an integer value trace event.
|
public static final int HITRACE_FLAG_DEFAULT
Value: 0
By default, only synchronous service calls are traced, spans are created, prompt information about the trace start and end is provided, and event tracking information is not provided.
public static final int HITRACE_FLAG_DONOT_CREATE_SPAN
Value: 0x0002
When this flag is set, no spans are created.
By default, spans are created within a trace of synchronous and asynchronous service calls.
public static final int HITRACE_FLAG_DONOT_ENABLE_LOG
Value: 0x0010
When this flag is set, trace information is not printed in log.
By default, trace information is printed in log.
public static final int HITRACE_FLAG_INCLUDE_ASYNC
Value: 0x0001.
When this flag is set, both synchronous and asynchronous service calls can be traced.
By default, only synchronous service calls are traced.
public static final int HITRACE_FLAG_NO_BE_INFO
Value: 0x0008
When this flag is set, prompt information is not printed.
By default, prompt information about the start and end of a trace task is printed.
public static final int HITRACE_FLAG_TP_INFO
Value: 0x0004
When this flag is set for debugging, event tracking is automatically enabled for synchronous and asynchronous service calls at the sender and receiver, and event tracking information and timestamp are generated. Event tracking can be client send (CS), server receive (SR), server send (SS), or client receive (CR). In a synchronous service call, information about all the four event tracking types (CS/SR/SS/SS) is provided, whereas in an asynchronous service call, information about only the three event tracking types (CS/SR/SS) is provided.
By default, event tracking is not enabled in a trace task.
public static final int HITRACE_SLICE_CLIENT
This constant is a flag of the type parameter in the beginSlice API.
public static final int HITRACE_SLICE_GENERAL
This constant is a flag of the type parameter in the beginSlice API.
public static final int HITRACE_SLICE_SERVER
This constant is a flag of the type parameter in the beginSlice API.
public static HiTraceId begin(String name, int flags)
If a trace task is started for a service flow based on the service flow name and trace flags,
HiTraceId is returned. If begin is called for an ongoing trace task,
an invalid HiTraceId is returned.
name - Indicates the name of the service flow to trace. This parameter can be null.flags - Indicates the flags set when a trace task is started. For example,
you can set a flag to trace only synchronous flows or trace both synchronous and asynchronous flows.
For details, see the descriptions of FLAG constants.HiTraceId if the trace task is started successfully;
returns an invalid HiTraceId otherwise. The valid HiTraceId can be used for stopping
a trace task by calling the end method.HiTraceIdpublic static void end(HiTraceId hiTraceId)
HiTraceId.
If HiTraceId is invalid or does not match an ongoing trace task,
the ongoing trace task cannot be stopped.
hiTraceId - Indicates HiTraceId returned when begin is called.begin(java.lang.String,int)public static HiTraceId getId()
public static void setId(HiTraceId id)
id - Indicates the trace ID of the current thread. If the specified ID is invalid, nothing will be done.public static void clearId()
If the specified trace ID is valid, this method will clear the trace ID of the current thread and set the trace ID of the current thread as invalid.
public static HiTraceId createSpan()
public static void beginSlice(HiTraceId traceId, String name, String args)
This method starts a time slice trace event of the default HITRACE_SLICE_GENERAL type.
It is usually used before the code to be traced.
It combines parameters such as traceId, name, and args to generate
the time slice start information and passes the information to the trace data.
traceId - Indicates the trace ID of the service flow to which the time slice trace event belongs.
Generally, the value is the trace ID returned by begin or
the trace ID passed from the client to the server in distributed call chain trace.
If the value of this parameter is null or invalid, the output trace information will not
contain the traceId information.name - Indicates the name of the time slice trace event.
It is used to identify the code to be traced. The parameter cannot be null.args - Indicates the parameter string of the time slice trace event.
It is used to output the additional parameter information of the traced code. The value can be null.
If this parameter is a JSON string, the IDE can parse the JSON string into a parameter list.public static void beginSlice(HiTraceId traceId, int type, String name, String args)
This method starts a time slice trace event of the specified type.
It is generally called before the code to be traced.
If the type parameter is set to HITRACE_SLICE_GENERAL,
this method is equivalent to the beginSlice method without the type parameter.
It combines parameters such as traceId, type, name, and args to generate
the time slice start information and passes the information to the trace data.
traceId - Indicates the trace ID of the service flow to which the time slice trace event belongs.
Generally, the value is the trace ID returned by begin or
the trace ID passed from the client to the server in distributed call chain trace.
If the value of this parameter is null or invalid, the output trace information will not
contain the traceId information.type - Indicates the type of the time slice trace event.
The value can be HITRACE_SLICE_CLIENT, HITRACE_SLICE_SERVER, or HITRACE_SLICE_GENERAL.
HITRACE_SLICE_GENERAL indicates the beginning of a general time slice.
HITRACE_SLICE_CLIENT indicates beginning of a time slice trace event on the client side
in distributed call chain trace, where the output time slice information contains the client ID.
HITRACE_SLICE_SERVER indicates beginning of a time slice trace event on the server side
in distributed call chain trace, where the output time slice information contains the server ID.
The IDE matches the client and server IDs in the two time slice trace events that contain
the same trace ID and parses them into a distributed call chain.name - Indicates the name of the time slice trace event.
It is used to identify the code to be traced. The parameter cannot be null.args - Indicates the parameter string of the time slice trace event.
It is used to output the additional parameter information of the traced code. The value can be null.
If this parameter is a JSON string, the IDE can parse the JSON string into a parameter list.public static void endSlice()
To end a distributed time slice trace event, this method must be used with the beginSlice method.
It is generally called after the code to be traced.
public static void traceValue(HiTraceId traceId, String name, long value)
This method outputs an integer value for a given trace event.
It is usually used to trace the value of an integer variable that changes over time.
It combines parameters such as traceId, name, and value to generate
the integer value trace information and passes the information to the trace data.
traceId - Indicates the trace ID of the service flow to which the integer value trace event belongs.
Generally, the value is the trace ID returned by begin or the trace ID passed from
the client to the server in a distributed call. If the value of this parameter is null or invalid,
the output trace information will not contain the traceId information.name - Indicates the name of the integer value trace event.
It identifies the integer value to be traced.
The parameter cannot be null. Otherwise, no trace information will be output.value - Indicates the integer value of the given trace event.public static boolean isTraceEnabled()
true if HiTrace is enabled; returns false otherwise.