OpenTelemetry Go (LoongSuite)¶
LoongSuite Go is an OpenTelemetry-based compile-time automatic instrumentation tool for Go. It does not require modifying business code – simply replace go build with otel go build to inject the OpenTelemetry SDK and instrumentation logic for supported frameworks and components during compilation.
This document uses the DataKit OpenTelemetry collector to receive traces and metrics reported by LoongSuite via OTLP and forward them to TrueWatch:
!!! note
LoongSuite's "zero-code" means no changes to business code, but it does not mean no rebuild is required. Instrumentation happens at compile time; an already-built Go binary cannot be retrofitted with LoongSuite. You must recompile with `otel go build` and deploy the new binary.
Prerequisites¶
- DataKit is installed and connected to the target TrueWatch workspace.
- Network reachability from the Go application to DataKit: OTLP/HTTP uses DataKit HTTP port
9529, OTLP/gRPC defaults to4317. - The application can be compiled normally with
go build. - Go version, OS, and architecture meet LoongSuite's compatibility requirements.
- The frameworks or components used by the application are in LoongSuite's supported libraries list.
This document uses a Linux AMD64 host as an example and does not cover Kubernetes deployment.
1. Enable the OpenTelemetry Collector¶
Navigate to the conf.d/opentelemetry directory under the DataKit installation directory. If the collector configuration does not exist, copy the sample file:
Ensure that opentelemetry.conf includes at least the following receiver configuration:
[[inputs.opentelemetry]]
# If you want to keep custom attributes as tags, add them to the allowlist.
# Dots in attribute names are converted to underscores, e.g. team.name -> team_name.
customer_tags = ["team", "project"]
[inputs.opentelemetry.http]
http_status_ok = 200
trace_api = "/otel/v1/traces"
metric_api = "/otel/v1/metrics"
[inputs.opentelemetry.grpc]
addr = "127.0.0.1:4317"
max_payload = 16777216
The configuration above enables the following receiver endpoints:
| Protocol | Data Type | DataKit Receiver Endpoint |
|---|---|---|
| OTLP/HTTP + Protobuf | Trace | http://<DataKit-IP>:9529/otel/v1/traces |
| OTLP/HTTP + Protobuf | Metric | http://<DataKit-IP>:9529/otel/v1/metrics |
| OTLP/gRPC | Trace, Metric | http://<DataKit-IP>:4317 |
If the application is not on the same host as DataKit, adjust the DataKit HTTP listener address, firewall, or other network access controls accordingly. When using OTLP/gRPC, change addr to a listener address accessible by the application, e.g., 0.0.0.0:4317. Do not expose OTLP receiver ports directly to the public internet.
Restart DataKit to apply the configuration:
Verify that the DataKit HTTP service is reachable:
2. Instrument the Application with OpenTelemetry¶
Install LoongSuite¶
Download the Linux AMD64 executable from the official LoongSuite GitHub Release:
sudo curl -fL \
https://github.com/alibaba/loongsuite-go/releases/latest/download/otel-linux-amd64 \
-o /usr/local/bin/otel
sudo chmod +x /usr/local/bin/otel
For ARM64 hosts, replace the filename with otel-linux-arm64. For other OS and architectures, select the appropriate file from LoongSuite Releases.
Confirm the tool works:
For production environments, it is recommended to pin the LoongSuite version by using a Release download URL with an explicit version number, and check compatibility, Release Notes, and perform compilation and trace regression testing before upgrading.
Pre-build Check¶
First, confirm that the project compiles normally with the original command:
If the project already directly depends on the OpenTelemetry Go API, SDK, or Contrib instrumentation libraries, check whether these dependencies are compatible with the current LoongSuite version:
LoongSuite injects SDK initialization logic and instruments OpenTelemetry itself. If the project already has incompatible OpenTelemetry dependencies or duplicate SDK initialization logic, compilation failures, duplicate spans, or context breaks may occur. In such cases, unify the dependency versions according to the official compatibility table. If the application needs to control the SDK itself, consider using the OpenTelemetry Go SDK instead.
Build with LoongSuite¶
Navigate to the Go project directory and prepend otel to the original build command:
Other common build patterns retain the original go build arguments, for example:
LoongSuite compilation adds pre-processing, instrumentation, and dependency handling phases. The first build is usually significantly slower than native go build. You can configure a reusable Go build cache to speed up subsequent builds:
You can also specify the cache via environment variables for CI or single builds:
export OTELTOOL_GO_CACHE="/var/tmp/loongsuite-go-cache"
otel go build -o ./bin/order-service ./cmd/order-service
!!! warning
Subsequent deployment processes must use the binary produced by `otel go build`. If you overwrite the artifact with a regular `go build`, the runtime will not include LoongSuite's automatic instrumentation.
Configure OTLP/HTTP and Start¶
The following example reports traces and metrics to the local DataKit via OTLP/HTTP + Protobuf. After setting the common endpoint to http://127.0.0.1:9529/otel, the exporter appends /v1/traces and /v1/metrics respectively, which correspond to DataKit's /otel/v1/* receiver paths.
export OTEL_SERVICE_NAME="order-service"
export OTEL_RESOURCE_ATTRIBUTES="deployment.environment.name=prod,service.version=1.0.0,team=backend"
export OTEL_TRACES_EXPORTER="otlp"
export OTEL_METRICS_EXPORTER="otlp"
export OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"
export OTEL_EXPORTER_OTLP_ENDPOINT="http://127.0.0.1:9529/otel"
export OTEL_EXPORTER_OTLP_INSECURE="true"
# 1.0 means full sampling; only recommended during integration verification.
export OTEL_TRACE_SAMPLER="1.0"
./bin/order-service
The runtime parameters must be configured in the actual runtime environment of the instrumented binary, not just on the build host. After startup, request an endpoint provided by a supported framework, triggering database, HTTP client, or message queue calls to produce verifiable spans and metrics.
Use OTLP/gRPC¶
To switch to OTLP/gRPC, replace the protocol and endpoint. The gRPC address must not include HTTP paths like /v1/traces:
export OTEL_EXPORTER_OTLP_PROTOCOL="grpc"
export OTEL_EXPORTER_OTLP_ENDPOINT="http://127.0.0.1:4317"
export OTEL_EXPORTER_OTLP_INSECURE="true"
3. Data Reporting Parameters¶
LoongSuite injects OpenTelemetry SDK initialization logic during compilation. The instrumented application can adjust the exporter, endpoint, sampling, and resource attributes via environment variables at runtime.
Resource and Exporter Parameters¶
| Environment Variable | Description | Suggested Value or Example |
|---|---|---|
OTEL_SERVICE_NAME |
Service name, the core field for APM service identification in TrueWatch. | order-service, must be explicitly set in production. |
OTEL_RESOURCE_ATTRIBUTES |
Resource attributes, comma-separated key=value. |
deployment.environment.name=prod,service.version=1.0.0,team=backend |
OTEL_TRACES_EXPORTER |
Trace exporter; supports none, console, zipkin, otlp. Multiple values can be comma-separated. |
Set to otlp when reporting to DataKit. |
OTEL_METRICS_EXPORTER |
Metrics exporter; supports none, console, prometheus, otlp. Multiple values can be comma-separated. |
Set to otlp when reporting to DataKit; set to none if not collecting metrics. |
OTEL_EXPORTER_OTLP_PROTOCOL |
Shared OTLP protocol for traces and metrics. | http/protobuf or grpc; default is http/protobuf. |
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL |
OTLP protocol for traces only, overrides the shared protocol. | http/protobuf or grpc. |
OTEL_EXPORTER_OTLP_ENDPOINT |
Shared OTLP endpoint for traces and metrics. | HTTP: http://datakit-host:9529/otel; gRPC: http://datakit-host:4317. |
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT |
Endpoint for traces only, overrides the shared endpoint. | HTTP: http://datakit-host:9529/otel/v1/traces. |
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT |
Endpoint for metrics only, overrides the shared endpoint. | HTTP: http://datakit-host:9529/otel/v1/metrics. |
OTEL_EXPORTER_OTLP_HEADERS |
Headers carried by all OTLP requests, multiple values separated by commas. | x-tenant=tenant-a; must match DataKit expected_headers. |
OTEL_EXPORTER_OTLP_INSECURE |
Whether to use a connection without TLS. | Set to true when using plain HTTP addresses as described in this document. |
When using the generic HTTP endpoint, set it to http://<DataKit-IP>:9529/otel. If using signal-specific endpoints, you must include the full path containing /otel/v1/traces or /otel/v1/metrics. DataKit's OTLP/HTTP collector only supports Protobuf; do not configure http/json.
Sampling and Metric Parameters¶
| Environment Variable | Description | Default or Example |
|---|---|---|
OTEL_TRACE_SAMPLER |
Trace sampling rate for the SDK injected by LoongSuite, range 0.0 to 1.0; defaults to full sampling based on parent context. |
0.1 means 10% sampling of root traces. |
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE |
OTLP metric aggregation temporality. | cumulative (default), delta, or lowmemory. |
OTEL_EXPORTER_PROMETHEUS_PORT |
Listener port when using the Prometheus metrics exporter. | Default 9464; not needed when reporting to DataKit OTLP. |
!!! warning
LoongSuite currently uses the environment variable `OTEL_TRACE_SAMPLER`, which differs from the common OpenTelemetry SDK names `OTEL_TRACES_SAMPLER` and `OTEL_TRACES_SAMPLER_ARG`. Refer to the [SDK configuration documentation](https://github.com/alibaba/loongsuite-go/blob/main/docs/user/sdk-config.md){:target="_blank"} for the installed LoongSuite version.
In production, adjust the sampling rate based on traffic, data budget, and troubleshooting needs. Upstream and downstream services must maintain compatible W3C Trace Context propagation to avoid broken spans across service calls.
LoongSuite Build Parameters¶
The following variables only control the LoongSuite build tool, not the data reporting of the instrumented binary:
| Environment Variable | Corresponding otel set Flag |
Description |
|---|---|---|
OTELTOOL_GO_CACHE |
-gocache |
Specifies the reusable Go build cache directory. |
OTELTOOL_DEBUG |
-debug |
Outputs debug information; enable only when troubleshooting compilation or instrumentation issues. |
OTELTOOL_VERBOSE |
-verbose |
Outputs more detailed build process information. |
OTELTOOL_RULE_JSON_FILES |
-rule |
Specifies one or more custom instrumentation rule files. |
OTELTOOL_DISABLE_RULES |
-disable |
Disables specified default rules; multiple rules separated by commas. |
For example, to temporarily output detailed build logs:
export OTELTOOL_DEBUG="true"
export OTELTOOL_VERBOSE="true"
otel go build -o ./bin/order-service ./cmd/order-service
After troubleshooting, disable Debug and Verbose to reduce log volume.
Field Mapping¶
OTLP span attributes reported by LoongSuite are converted to trace fields by the DataKit OpenTelemetry collector. Common mappings are as follows:
| OpenTelemetry Attribute | DataKit Field |
|---|---|
db.system, db.system.name |
db_system |
db.operation, db.operation.name |
db_operation |
db.query.text |
db_statement |
db.namespace |
db_name |
db.collection.name |
db_collection |
http.request.method |
http_method |
http.response.status_code |
http_status_code |
network.protocol.name |
net_protocol_name |
network.protocol.version |
net_protocol_version |
messaging.system |
messaging_system |
messaging.operation.name |
messaging_operation |
messaging.message.id |
messaging_message_id |
rpc.system.name |
rpc_system |
rpc.method |
rpc_method |
rpc.grpc.status_code |
rpc_grpc_status_code |
To promote other attributes reported by LoongSuite to tags, configure customer_tags in the DataKit opentelemetry.conf. customer_tags supports regular expressions; matched attribute names have dots converted to underscores:
[[inputs.opentelemetry]]
customer_tags = [
"reg:^db\\.query\\.parameter\\.",
"reg:^kratos\\.service\\.meta\\.",
"reg:^gen_ai\\.other_input\\.",
"reg:^gen_ai\\.other_output\\.",
]
Do not promote high-cardinality values such as user IDs or order numbers in bulk to tags, and do not report sensitive information such as passwords, tokens, or full database connection strings.
Verify Integration¶
- Compile and start the new binary using
otel go build. - Request an endpoint handled by a supported web framework, triggering database, HTTP client, or message queue calls.
- When using OTLP/HTTP, check the receiver logs on the DataKit host:
If you see POST requests to /otel/v1/traces or /otel/v1/metrics with a 200 response code, DataKit has received the data. Then go to Application Performance Monitoring > Traces in TrueWatch and query by service:order-service. For metrics, wait at least one export cycle before querying.
If no data is received, check the following in order:
- Is the deployed binary the one produced by
otel go build? - Are
OTEL_SERVICE_NAME, exporter, protocol, and endpoint configured in the actual runtime process? - Is the framework and version used by the application in the LoongSuite supported libraries list?
- Is the LoongSuite version compatible with the project's existing OpenTelemetry dependencies?
- Is the DataKit OpenTelemetry collector enabled and restarted?
- Is the network and port from the application to DataKit reachable?
If go build succeeds but otel go build fails, temporarily enable OTELTOOL_DEBUG=true and OTELTOOL_VERBOSE=true to identify the specific failing instrumentation rule. If necessary, temporarily disable the problematic rule with otel set -disable=<rule-name> and report the issue to LoongSuite Issues.