You need to enable JavaScript to run this app.
Application Performance Management Plus

Application Performance Management Plus

Copy page
Download PDF
Integration guide
Integrating log monitoring with Go applications
Copy page
Download PDF
Integrating log monitoring with Go applications
APMPlus supports log monitoring and provides the following capabilities on collected logs: log list, log search, and single-trace log query. This topic describes how to integrate APMPlus log monitoring with your Go applications by using OpenTelemetry.​
Prerequisites​
You have obtained service-related configuration information, such as service name, appkey, and endpoint.​
​
Variable​
Description​
{apmplus_endpoint}​
The Server-side Monitoring endpoint. This configuration varies with the reporting method. Configure this field based on your reporting method.​
  • Reporting to Otel Collector: (Recommended) Forwarding is performed by the Otel Collector. If the apmplus-opentelemetry-collector version is greater than or equal to 2.0.0, use http://apmplus-opentelemetry-gateway-service-grpc.apmplus-opentelemetry-operator:4317. If the version is lower than 2.0.0, use http://apmplus-opentelemetry-gateway-service-grpc.kube-system:4317.​
  • Reporting to Server-side Monitoring: In the integration center, obtain the public or private access endpoint for the region of the service.​
{apmplus_appkey}​
The Server-side Monitoring authentication information. This information is required for reporting to Server-side Monitoring.​
  • Reporting to Otel Collector: appkey is not required. You need to delete the appkey-related configuration.​
{service_name}​
Service name. You can obtain it from the service list.​
​
Procedure​
OpenTelemetry supports the following logging frameworks. Select a framework that meets your requirements. See the official OpenTelemetry documentation for details.​
​
Use zap
Use slog
Use slog
​
  1. Download dependencies.​
​
go get go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc​
go get go.opentelemetry.io/contrib/bridges/otelzap​
go get go.uber.org/zap​
​
  1. Configure LoggerProvider.​
tip​
For instructions on obtaining and configuring variable values, see the prerequisites.​
​
import (​
"context"​
"time"​
​
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"​
"go.opentelemetry.io/otel/log/global"​
"go.opentelemetry.io/otel/sdk/log"​
)​
​
func InitOtlLog() *log.LoggerProvider {​
ctx := context.Background()​
// Report using the gRPC protocol.​
exporter, err := otlploggrpc.New(ctx,​
otlploggrpc.WithEndpoint("{{apmplus_endpoint}}"), // Specify the endpoint to report logs to.​
otlploggrpc.WithInsecure(),​
otlploggrpc.WithHeaders(map[string]string{"x-byteapm-appkey": "{{apmplus_appkey}}"}), // appkey (Server-side Monitoring authentication information). For reporting to the Otel Collector, delete this line, as this configuration item is not required. For reporting to Server-side Monitoring, an appkey is required.​
otlploggrpc.WithTimeout(10*time.Second),​
)​
if err != nil {​
panic(err)​
}​
​
hostname, _ := os.Hostname()​
loggerProvider := log.NewLoggerProvider(​
log.WithProcessor(log.NewBatchProcessor(exporter)),​
log.WithResource(resource.NewWithAttributes(​
semconv.SchemaURL,​
semconv.ServiceNameKey.String("{{service_name}}"), // The service name.​
semconv.HostNameKey.String(hostname)),​
),​
)​
​
global.SetLoggerProvider(loggerProvider)​
// Properly handle the shutdown process to ensure no leaks.​
return loggerProvider​
}​
​
// Put it in the initialization path to ensure the following logic is executed.​
loggerProvider := initOtlLog()​
defer loggerProvider.Shutdown(context.Background())​
​
  1. Initialize zap and use it to print logs.​
​
package zap_demo​
​
import (​
"context"​
​
"go.opentelemetry.io/contrib/bridges/otelzap"​
"go.opentelemetry.io/otel"​
"go.opentelemetry.io/otel/baggage"​
"go.opentelemetry.io/otel/log/global"​
"go.uber.org/zap"​
)​
​
var (​
tracer = otel.Tracer("otel-demo-tracer")​
// AddCaller ensures the file name and line number are printed.​
logger = zap.New(otelzap.NewCore("my/demo/pkg", otelzap.WithLoggerProvider(global.GetLoggerProvider())), zap.AddCaller())​
)​
​
// Call the Call method and print logs.​
func Call(ctx context.Context) {​
// Generate a new span based on ctx.​
ctx, span := tracer.Start(ctx, "otelLogCall")​
defer span.End()​
​
ctx = context.WithValue(ctx, "key", "value")​
// Use ctx to report traceId and spanId. Additionally, the keys and values in Fields are also reported.​
logger.Info("this is zap info demo", zap.Any("context", ctx), zap.String("method", "zapCall"))​
logger.Info("this is zap info demo without context")​
}​
​
​
What to do next​
After you integrate your Go application with log monitoring, use the OpenTelemetry SDK to report printed logs to APMPlus. You can go to the Server-side Monitoring page to query logs. Supported capabilities include log query and single-trace log details. For details, see Log monitoring.​
Last updated: 2026.09.20 10:55:53