You understand the value of tracking and evaluating your applications' performance as a Golang developer. The stability and general well-being of your Golang applications depend on the implementation of strong observability solutions, which are essential given the growing popularity of distributed systems and microservices architectures.
Opentelemetry is the baseline behind anything around observability.
Step 1:
Create a docker container by making the docker-compose file.
Commands:
mkdir golang
cd golang
Create a docker-compose.yaml file
version: '3'
services:
otel-collector:
image: otel/opentelemetry-collector-dev:latest
ports:
- 4317:4317
- 55680:55680
app:
build: .
ports:
- 8080:8080
Create a DockerFile
FROM golang:latest
WORKDIR /app
COPY . .
RUN go build -o main .
CMD ["./main"]
Make the application running by running the container
docker-compose up
Step 2:
Let’s start implementing the Opentelemetry for GO application
import (
"context"
"fmt"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/trace"
)
Now let’s setup the exporter properly to get the data easily.
exporter, err := otlp.NewExporter(context.TODO(),
otlp.WithInsecure(),
otlp.WithEndpoint("http://otel-collector:4317"),
otlp.WithHTTPClient(otlptracehttp.NewClient()),
)
if err != nil {
log.Fatalf("Failed to create exporter: %v", err)
}
defer exporter.Shutdown(context.Background())
Next step is to initialize the tracer.
exporter, err := otlp.NewExporter(context.TODO(),
otlp.WithInsecure(),
otlp.WithEndpoint("http://otel-collector:4317"),
otlp.WithHTTPClient(otlptracehttp.NewClient()))
if err != nil {
log.Fatalf("Failed to create exporter: %v", err)
}
defer exporter.Shutdown(context.Background())
provider := otel.GetTracerProvider()
tracer := provider.Tracer("example")
Now let us create the trace provider and register the exporter.
otel.SetTracerProvider(otel.NewTracerProvider(
otel.TracerProviderOptions{
Resource: resource.NewWithAttributes(
resource.Attributes{
"service.name": "my-service",
},
),
BatchExporter: exporter,
},
))
Now you can probably able to see the output after making the span.
ctx, span := otel.Tracer("my-component").Start(context.Background(), "my-operation")
defer span.End()
● CloudMonitor
● Middleware
● Last9
● Promethus
● Grafana
● Uptrace
● Instrument your code
● Implement effective logging
● Choose the right monitoring tool
● Alerting on key metrics
● Automate Monitoring
Disclaimer: The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.
Alibaba Cloud Native Community - April 26, 2024
Amber Wang - August 6, 2018
Alex - February 14, 2020
Alibaba Cloud Native Community - March 14, 2023
Alex - January 22, 2020
Alibaba Cloud Serverless - December 17, 2020
A quotation service that establishes stable, high-quality connections to exchanges all around the world at ultra-low latency.
Learn MoreMore Posts by Neel_Shah
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
Get Started for Free Get Started for Free