After you use SkyWalking to instrument an application and report the trace data to the Managed Service for OpenTelemetry console, Managed Service for OpenTelemetry starts to monitor the application. Then, you can view the monitoring data of the application, such as the application topology, traces, abnormal transactions, slow transactions, and SQL analysis. This topic describes how to use SkyWalking Agent for Go to automatically instrument a Go application and report the trace data.
ARMS provides a commercially supported, self-developed agent for Go applications. This agent enables non-intrusive instrumentation and provides richer feature set and enhanced stability.
Prerequisites
Agent types
Go2Sky is an old agent version officially provided by SkyWalking for Go. The Go2Sky agent is used by many users but is intrusive to user code. To resolve this issue, SkyWalking officially releases the skywalking-go agent. This new agent version is non-intrusive to user code. This topic describes how to use the Go2Sky and skywalking-go agents to report data to the Managed Service for OpenTelemetry console.
Go2Sky
SkyWalking discontinues support for the Go2Sky agent after the skywalking-go agent is officially released.
On the official website of SkyWalking, the Go2Sky agent has been moved to the Retired category and is no longer maintained.
The Go2Sky agent is intrusive to user code and requires you to add a hook for each plug-in to your project.
The Go2Sky agent is used by many users.
Supported instrumentation libraries:
skywalking-go
The skywalking-go agent is a new agent version officially provided by SkyWalking for Go. SkyWalking provides stable support for the skywalking-go agent.
The skywalking-go agent is non-intrusive to user code.
The skywalking-go agent is easy to use. You need to only specify the skywalking-go agent by using the
-toolexec
parameter when you compile a Go project.The number of users of the skywalking-go agent is smaller than that of the Go2Sky agent.
Supported instrumentation libraries:
Recommended agent
We recommend that you use the skywalking-go agent. The skywalking-go agent offers the following benefits:
Ease of use: The skywalking-go agent is easier to use. You need to only perform a few simple steps to integrate the skywalking-go agent and implement automatic instrumentation. The skywalking-go agent is non-intrusive to user code, whereas the Go2Sky agent requires you to add a hook for each plug-in.
Community support: SkyWalking has discontinued support for the Go2Sky agent and no longer accepts pull requests for the Go2Sky agent.
Plug-in ecology: SkyWalking is gradually porting Go2Sky plug-ins to the skywalking-go agent. Compared with the Go2Sky agent, the skywalking-go agent supports instrumentation for the Google Remote Procedure Call (gRPC) framework.
Sample code
For more information about the sample code repository, see skywalking-demo at GitHub.
Use the skywalking-go agent to report trace data
Download the skywalking-go agent.
Build the skywalking-go agent.
cd skywalking-go && make build
Generate an executable file in the skywalking-go/bin directory.
The executable file varies based on your operating system. For example, use skywalking-go-agent--darwin-amd64 for the macOS operating system.
Open the Go project and import the SkyWalking module into the main package.
Method 1
package main import ( _ "github.com/apache/skywalking-go" )
Method 2
skywalking-go/bin/skywalking-go-agent--darwin-amd64 -inject path/to/your-project
Configure the config.yaml file.
You can configure the config.yaml file by referring to the skywalking-go/tools/go-agent/config/config.default.yaml file in the sample code.
Two methods are provided for you to configure parameters. For example, to configure the service_name parameter, use one of the following methods:
Method 1 (recommended): Add the service_name parameter to the config.yaml file
agent: service_name: ${SW_AGENT_NAME:<your_service_name>}
Method 2: Configure a system environment variable
export SW_AGENT_NAME=<your_service_name>
To connect the skywalking-go agent to the Managed Service for OpenTelemetry console, you must configure the following parameters:
service_name: ${SW_AGENT_NAME:Your_ApplicationName}
: the name of the Go application.backend_service: ${SW_AGENT_REPORTER_GRPC_BACKEND_SERVICE:127.0.0.1:11800}
: the endpoint of the server.authentication: ${SW_AGENT_REPORTER_GRPC_AUTHENTICATION:}
: the authentication token for accessing the server.
By default, the skywalking-go agent automatically instruments all plug-ins. To disable automatic instrumentation for specific plug-ins, you can specify the excluded parameter. Examples:
# Disable automatic instrumentation for the SQL plug-in. plugin: excluded: ${SW_AGENT_PLUGIN_EXCLUDES:sql} # Disable automatic instrumentation for multiple plug-ins. Separate the plug-ins with commas (,). plugin: excluded: ${SW_AGENT_PLUGIN_EXCLUDES:sql,gorm}
Rebuild the project.
# Specify the -tolexec parameter. sudo go build -toolexec "path/to/skywalking-go-agent -config path/to/config.yaml" -a
path/to/skywalking-go-agent
: the absolute path to the executable file that is generated in Step 3 in the "Use the skywalking-go agent to report trace data" section.path/to/config.yaml
: the absolute path to the config.yaml file of the skywalking-go agent.
Start the project. Then, SkyWalking reports data to the Managed Service for OpenTelemetry console.
Appendix
The following table describes some environment variables of the skywalking-go agent. A value of NULL indicates that no default value is specified.
Environment variable | Description | Default value |
SW_AGENT_NAME | The name of the Go application. | NULL |
SW_AGENT_INSTANCE_NAME | The name of the application instance. | The system automatically generates a name. |
SW_AGENT_SAMPLE | The sample rate. Valid values: 0 to 1. | 1 |
SW_AGENT_REPORTER_GRPC_BACKEND_SERVICE | The endpoint of the server to which the skywalking-go agent reports trace data over gRPC. | 127.0.0.1:11800 |
SW_AGENT_REPORTER_GRPC_AUTHENTICATION | The authentication token for accessing the server to which the skywalking-go agent reports trace data over gRPC. | NULL |
SW_AGENT_PLUGIN_EXCLUDES | The plug-ins for which automatic instrumentation is to be disabled. | NULL |
Use the Go2Sky agent to report trace data
The Go2Sky agent allows you to hard-code parameters into your project or use environment variables to configure parameters.
Hard-code parameters into the project
### Use the reporter.WithParameter() method to import parameters. report, err := reporter.NewGRPCReporter( <your-backend-server-address>, reporter.WithAuthentication(<your-auth-token>))
Use environment variables to configure parameters
### The Go2Sky agent can obtain the values of the following parameters from environment variables: SW_AGENT_AUTHENTICATION SW_AGENT_LAYER SW_AGENT_COLLECTOR_HEARTBEAT_PERIOD SW_AGENT_COLLECTOR_GET_AGENT_DYNAMIC_CONFIG_INTERVAL SW_AGENT_COLLECTOR_BACKEND_SERVICES SW_AGENT_COLLECTOR_MAX_SEND_QUEUE_SIZE SW_AGENT_PROCESS_STATUS_HOOK_ENABLE SW_AGENT_PROCESS_LABELS ### Configure environment variables. In this example, macOS is used. # Method 1: Write an environment variable configuration file. The environment variables configured by using this method are permanently valid. vim ~/.bash_profile export SW_AGENT_COLLECTOR_BACKEND_SERVICES=<your-collector-address> source ~/.bash_profile # Method 2: Open a terminal and configure environment variables in the command line. The environment variables configured by using this method are temporarily valid and become invalid when you open another terminal. export SW_AGENT_COLLECTOR_BACKEND_SERVICES=<your-collector-address>
Configure the ServiceName parameter to specify an application.
ServiceName := <your-service-name> tracer, err := go2sky.NewTracer(ServiceName, go2sky.WithReporter(report))
Add hooks for Go2Sky plug-ins.
The Go2Sky agent provides plug-ins for many libraries. You need to add hooks for these plug-ins to the source code of your project. For more information about how to add hooks, see the go2sky-plugins repository at GitHub. A README.md file is provided in the plugin folder of each plug-in to describe how to use the plug-in.
In this example, the gin framework is used.
Go to the /gin folder and view the /gin/v3/README.md file.
Add a hook for middleware: v3.Middleware(r, tracer).
package main import ( "log" "github.com/SkyAPM/go2sky" v3 "github.com/SkyAPM/go2sky-plugins/gin/v3" "github.com/SkyAPM/go2sky/reporter" "github.com/gin-gonic/gin" ) func main() { // Use gRPC reporter for production re, err := reporter.NewLogReporter() if err != nil { log.Fatalf("new reporter error %v \n", err) } defer re.Close() tracer, err := go2sky.NewTracer("gin-server", go2sky.WithReporter(re)) if err != nil { log.Fatalf("create tracer error %v \n", err) } gin.SetMode(gin.ReleaseMode) r := gin.New() //Use go2sky middleware with tracing r.Use(v3.Middleware(r, tracer)) // do something }
Restart the application.
Appendix
The following table describes the environment variables of the Go2Sky agent. A value of NULL indicates that no default value is specified.
Environment variable | Description | Default value |
SW_AGENT_NAME | The name of the Go application. | NULL |
SW_AGENT_LAYER | Instance belong layer name which define in the backend The name of the layer to which the Go application instance belongs. | NULL |
SW_AGENT_INSTANCE_NAME | The name of the Go application instance. | The system generates a random name. |
SW_AGENT_SAMPLE | The sample rate. A value of 1 specifies that full data is sampled. | 1 |
SW_AGENT_COLLECTOR_BACKEND_SERVICES | The endpoint of the server to which the Go2Sky agent reports trace data. | NULL |
SW_AGENT_AUTHENTICATION | The authentication token for accessing the server to which the Go2Sky agent reports trace data. | NULL |
SW_AGENT_COLLECTOR_HEARTBEAT_PERIOD | The heartbeat interval of the Go2Sky agent. Unit: seconds. | 20 |
SW_AGENT_COLLECTOR_GET_AGENT_DYNAMIC_CONFIG_INTERVAL | The interval at which the configurations of the Go2Sky agent are dynamically obtained. Unit: seconds. | 20 |
SW_AGENT_COLLECTOR_MAX_SEND_QUEUE_SIZE | The size of the queue buffer for sending spans. | 30000 |
SW_AGENT_PROCESS_STATUS_HOOK_ENABLE | Specifies whether to enable the process status hook feature. | False |
SW_AGENT_PROCESS_LABELS | The process labels. Separate multiple process labels with commas (,). | NULL |
FAQ
What do I do if the error message shown in the following figure appears when I use the skywalking-go agent?
If the inject method fails, you can use the import method to import the SkyWalking module into the main package.
Why does the Managed Service for OpenTelemetry console fail to display the correct trace data for a cross-process service call when I use the Go2Sky agent to report trace data?
Managed Service for OpenTelemetry connects traces based on trace IDs. Trace IDs are carried and transmitted by HTTP requests. If invalid trace data is displayed, the trace IDs are not correctly transmitted. In this case, you must configure appropriate spans for instrumentation.
You can call the following two important operations to connect traces for a cross-process service call:
CreateEntrySpan: creates an entry span. You can call this operation to extract the trace analysis context, including the trace ID, from an HTTP request.
CreateExitSpan: creates an exit span. You can call this operation to inject the trace analysis context, including the trace ID, into an HTTP request.
// Call the CreateLocalSpan operation to create a span in a process. span, ctx, err := tracer.CreateLocalSpan(context.Background()) subSpan, newCtx, err := tracer.CreateLocalSpan(ctx) // For a cross-process service call, call the CreateEntrySpan operation to extract the trace analysis context from the HTTP request, and call the CreateExitSpan operation to inject the trace analysis context into the HTTP request. span, ctx, err := tracer.CreateEntrySpan(r.Context(), "/api/login", func(key string) (string, error) { return r.Header.Get(key), nil }) span, err := tracer.CreateExitSpan(req.Context(), "/service/validate", "tomcat-service:8080", func(key, value string) error { req.Header.Set(key, value) return nil })
For a cross-process service call, trace IDs must be transmitted across processes to connect the traces. Therefore, you must call the preceding operations to inject the trace analysis context into the HTTP requests that are transmitted across processes.