Project: SkyWalking (Observability and Analysis / Observability)
File: backend-trace-profiling.md

Trace Profiling is bound within the auto-instrument agent and corresponds to . It is delivered to the agent in the form of a task, allowing for dynamic enabling or disabling. Trace Profiling tasks can be created when an `endpoint` within a service experiences high latency. When the agent receives the task, it periodically samples the thread stack related to the endpoint when requested. Once the sampling is complete, the thread stack within the endpoint can be analyzed to determine the specific line of business code causing the performance issue. Lean more about the trace profiling, . OAP and the agent use a brand-new protocol to exchange Trace Profiling data, so it is necessary to start OAP with the following configuration: ```yaml receiver-profile: selector: ${SWRECEIVERPROFILE:default} default: ``` To use the Trace Profiling feature, please follow these steps: Create profiling task: Use the UI or CLI tool to create a task. Generate requests: Ensure that the service has generated requests. Query task details: Check that the created task has Trace data generated. Analyze the data: Analyze the Trace data to determine where performance bottlenecks exist in the service. Creating a Trace Profiling task is used to notify all agent nodes that execute the service entity which endpoint needs to perform the Trace Profiling feature. This Endpoint is typically an HTTP request or an RPC request address. When creating a task, the following configuration fields are required: Service: Which agent under the service needs to be monitored. Endpoint: The specific endpoint name, such as "POST:/path/to/request." Start Time: The start time of the task, which can be executed immediately or at a future time. Duration: The duration of the task execution. Min Duration Threshold: The monitoring will only be triggered when the specified endpoint's execution time exceeds this threshold. This effectively prevents the collection of ineffective data due to short execution times. Dump Period: The thread stack collection period, which will trigger thread sampling every specified number of milliseconds. Max Sampling Count: The maximum number of traces that can be collected in a task. This effectively prevents the program execution from being affected by excessive trace sampling, such as the Stop The World situation in Java. When the Agent receives a Trace Profiling task from OAP, it automatically generates a log to notify that the task has been acknowledged. The log contains the following field information: Instance: The name of the instance where the Agent is located. Type: Supports "NOTIFIED" and "EXECUTION_FINISHED", with the current log displaying "NOTIFIED". Time: The time when the Agent received the task. At this point, Tracing requests matching the specified Endpoint and other conditions would undergo Profiling. Notice, whether profiling is thread sensitive, it relies on the agent side

The Java Agent already supports cross-thread requests, so when a request involves cross-thread operations, it would also be periodically sampled for thread stack. Once the Tracing request is completed, we can query the Tracing data associated with this Trace Profiling task, which includes the following information: TraceId: The Trace ID of the current request. Instance: The instance to which the current profiling data belongs. Duration: The total time taken by the current instance to process the Tracing request. Spans: The list of Spans associated with the current Tracing. SpanId: The ID of the current span. Parent Span Id: The ID of the parent span, allowing for a tree structure. SegmentId: The ID of the segment to which the span belongs. Refs: References of the current span, note that it only includes "CROSS_THREAD" type references. Service: The service entity information to which the current span belongs. Instance: The instance entity information to which the current span belongs. Time: The start and end time of the current span. Endpoint Name: The name of the current Span. Type: The type of the current span, either "Entry", "Local", or "Exit". Peer: The remote network address. Component: The name of the component used by the current span. Layer: The layer to which the current span belongs. Tags: The tags information contained in the current span. Logs: The log information in the current span. Profiled: Whether the current span supports Profiling data analysis. Once we know which segments can be analyzed for profiling, we can then determine the time ranges available for thread stack analysis based on the "profiled" field in the span. Next, we can provide the following query content to analyze the data: segmentId: The segment to be analyzed. Segments are usually bound to individual threads, so we can determine which thread needs to be analyzed. time range: Includes the start and end time. By combining the segmentId with the time range, we can confirm the data for a specific thread during a specific time period. This allows us to merge the thread stack data from the specified thread and time range and analyze which lines of code take longer to execute. The following fields help you understand the program execution: Id: Used to identify the current thread stack frame. Parent Id: Combined with "id" to determine the hierarchical relationship. Code Signature: The method signature of the current thread stack frame. Duration: The total time consumed by the current thread stack frame. Duration Child Excluded: Excludes the child method calls of the current method, only obtaining the time consumed by the current method. Count: The number of times the current thread stack frame was sampled. If you want to learn more about the thread stack merging mechanism, please read . If you find that the results of profiling data are not correct, you can report an issue through .