codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

54 / 72 topics
51Docker for Go Applications52Kubernetes with Go53CI/CD Pipelines for Go Projects54Monitoring and Logging in Go Applications
Tutorials/Go (Golang)/Monitoring and Logging in Go Applications
🐹Go (Golang)

Monitoring and Logging in Go Applications

Updated 2026-04-20
3 min read

Introduction

In modern software development, especially for production-level applications, monitoring and logging are crucial components of the DevOps pipeline. They help developers understand application behavior, diagnose issues, and ensure high availability. This tutorial will guide you through setting up effective monitoring and logging in Go applications.

Why Monitoring and Logging?

  • Monitoring: Provides real-time insights into the health and performance of your application.
  • Logging: Captures events that happen during runtime, which is essential for debugging and auditing.

Setting Up Logging

1. Choose a Logging Library

Go has several popular logging libraries:

  • logrus: A structured logger for Go.
  • zap: Another fast, structured, leveled logging library in Go.
  • go-logr: An interface for structured logging in Go.

For this tutorial, we'll use logrus due to its simplicity and flexibility.

2. Install logrus

go get github.com/sirupsen/logrus

3. Basic Usage of logrus

Here's a simple example of how to set up logging in your Go application:

package main

import (
    "github.com/sirupsen/logrus"
)

func main() {
    // Set the log level
    logrus.SetLevel(logrus.InfoLevel)

    // Log a message with fields
    logrus.WithFields(logrus.Fields{
        "animal": "walrus",
        "size":   10,
    }).Info("A group of walrus emerges from the ocean")

    // Log an error
    err := someFunctionThatMightFail()
    if err != nil {
        logrus.WithError(err).Error("Failed to do something")
    }
}

func someFunctionThatMightFail() error {
    return fmt.Errorf("something went wrong")
}

4. Best Practices for Logging

  • Structured Logs: Use structured logs (JSON format) for easier parsing and analysis.
  • Consistent Log Levels: Use consistent log levels to categorize the importance of logs.
  • Avoid Sensitive Information: Never log sensitive information like passwords or personal data.

Setting Up Monitoring

1. Choose a Monitoring Solution

Popular monitoring solutions include:

  • Prometheus: An open-source systems monitoring and alerting toolkit.
  • Grafana: An open-source platform for analytics and monitoring.
  • Datadog: A comprehensive monitoring and security solution.

For this tutorial, we'll use Prometheus with Grafana.

2. Install Prometheus

You can download Prometheus from its official website or use Docker:

docker run -d --name prometheus -p 9090:9090 prom/prometheus

3. Configure Prometheus

Create a prometheus.yml configuration file to scrape metrics from your Go application.

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'go_app'
    static_configs:
      - targets: ['localhost:8080']

4. Expose Metrics in Your Go Application

Use the prometheus/client_golang library to expose metrics:

go get github.com/prometheus/client_golang/prometheus
go get github.com/prometheus/client_golang/prometheus/promhttp

Here's how you can set up a simple HTTP server to expose metrics:

package main

import (
    "net/http"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
    httpRequestsTotal = prometheus.NewCounterVec(
        prometheus.CounterOpts{
            Name: "http_requests_total",
            Help: "Total number of HTTP requests.",
        },
        []string{"method", "path"},
    )
)

func init() {
    // Register the metrics with Prometheus's default registry.
    prometheus.MustRegister(httpRequestsTotal)
}

func handler(w http.ResponseWriter, r *http.Request) {
    httpRequestsTotal.WithLabelValues(r.Method, r.URL.Path).Inc()
    w.Write([]byte("Hello, world!"))
}

func main() {
    http.HandleFunc("/", handler)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe(":8080", nil)
}

5. Set Up Grafana

Install Grafana using Docker:

docker run -d --name grafana -p 3000:3000 grafana/grafana

Access Grafana at http://localhost:3000 and add Prometheus as a data source.

6. Create Dashboards in Grafana

You can create custom dashboards to visualize metrics from your Go application. Use the Prometheus query language (PromQL) to fetch and display metrics.

Best Practices for Monitoring

  • Define SLIs and SLOs: Set Service Level Indicators (SLIs) and Service Level Objectives (SLOs) to measure performance.
  • Alerting: Configure alerts based on critical metrics to notify you of issues.
  • Visualization: Use dashboards to visualize key metrics and trends.

Conclusion

Effective monitoring and logging are essential for maintaining the health and reliability of your Go applications. By following this tutorial, you've learned how to set up basic logging with logrus and expose metrics using Prometheus. Combining these tools with Grafana provides a robust solution for monitoring and analyzing your application's performance.

Remember, continuous improvement is key in DevOps. Regularly review and refine your monitoring and logging strategies to adapt to the evolving needs of your applications.


PreviousCI/CD Pipelines for Go ProjectsNext Security Best Practices for Go Apps

Recommended Gear

CI/CD Pipelines for Go ProjectsSecurity Best Practices for Go Apps