×
Community Blog Monitor Your Golang Application Running on ECS

Monitor Your Golang Application Running on ECS

This article introduces how to monitor Golang applications running on ECS using Opentelemetry and highlights best practices and tools for effective monitoring.

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.

Steps to Monitor Your Application Using Opentelemetry:

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()

Some of the Best Golang Monitoring Tools

CloudMonitor
Middleware
Last9
Promethus
Grafana
Uptrace

Best Practices for Golang Monitoring are:

● 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.

0 1 0
Share on

Neel_Shah

11 posts | 1 followers

You may also like

Comments

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

Get Started for Free Get Started for Free