This documentation provides a comprehensive guide to monitoring the benhall/golang-demo project in a production environment. It focuses on the specifics of deployment and monitoring aspects that are critical for ensuring application reliability and performance.

Monitoring Overview

In a production environment, monitoring is essential to track application health, performance metrics, and error rates. It allows developers to respond to issues before they impact users. The following steps outline how to monitor the benhall/golang-demo application effectively.

Step 1: Setup Metrics Collection

Prometheus Integration

To monitor the benhall/golang-demo application, integrating with Prometheus is a common practice. You will need to expose relevant metrics from your Go application. Use the prometheus/client_golang package to facilitate this.

Code Example

First, add the Prometheus client library to your go.mod:

require (
    github.com/prometheus/client_golang v1.12.0 // replace with the latest version
)

Now, in your main application file, initialize a metrics handler:

package main

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

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

func init() {
    prometheus.MustRegister(requestCounter)
}

func handler(w http.ResponseWriter, r *http.Request) {
    requestCounter.WithLabelValues(r.Method, r.URL.Path).Inc()
    // Handle your request here
}

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

In this code, a counter metric is set up to track the total number of HTTP requests per method and endpoint. The /metrics endpoint exposes these metrics for Prometheus to scrape.

Step 2: Deploy with Docker

In your Docker setup, ensure that the Dockerfile exposes port 8080 to allow access to the application’s metrics.

Dockerfile Example

# First stage: build the application
FROM golang:1.21-alpine AS builder

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download && go mod tidy

COPY . .

RUN go build -o myapp

# Second stage: create the runtime image
FROM alpine:latest

WORKDIR /root/

COPY --from=builder /app/myapp .

EXPOSE 8080

CMD ["./myapp"]

This setup ensures that your application runs in a Docker container with the necessary configuration to operate and expose metrics on port 8080.

Step 3: Set Up Prometheus Server

To collect metrics from the benhall/golang-demo application running in Docker, configure your Prometheus server to scrape metrics from your application.

Prometheus Configuration Example

You need to edit the prometheus.yml configuration file to define your targets:

scrape_configs:
  - job_name: 'myapp'
    static_configs:
      - targets: ['docker_host_ip:8080'] # Replace with actual IP address of the host running Docker

Ensure that the Docker container running the application is accessible from the Prometheus server.

Step 4: Visualizing Metrics with Grafana

Once Prometheus is collecting metrics, you can visualize this data using Grafana. Set up a Grafana instance and configure it to connect to your Prometheus server.

Grafana Configuration

  • Add Prometheus as a data source in Grafana.
  • Create dashboards to represent the metrics being scraped from your application. For instance, create a graph to display http_requests_total categorized by HTTP method and endpoint.

Step 5: Alerting on Metrics

Implement alerting to notify your team of potential issues detected through the metrics collected.

Alert Rule Example

In your prometheus.yml, you can define alert rules:

groups:
  - name: example_alert
    rules:
      - alert: HighRequestRate
        expr: rate(http_requests_total[5m]) > 10 # More than 10 requests in the last 5 minutes
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "High request rate detected"
          description: "Request rate is above 10 requests per minute."

Source of Information

This documentation is based on the configurations and practices relevant to the benhall/golang-demo project as outlined in the provided Dockerfile and principles of Go application monitoring.