Go Metrics

Learn how to setup metrics in your Go application.

Setup

Open the Vigilant dashboard and generate an API key.

Install

Install the Vigilant SDK in your Go application.

go get github.com/vigilant-run/vigilant-golang/v2

Initialize

Initialize the Vigilant SDK in your Go application. Make sure to shutdown the SDK before your application exits.

package main

import (
  "github.com/vigilant-run/vigilant-golang/v2"
)

func main() {
  config := vigilant.NewConfigBuilder().
    WithName("backend").
    WithToken("tk_1234567890"). // Use your API key from the dashboard
    Build()

  vigilant.Init(config)
  defer vigilant.Shutdown()
}

Metrics Functions

The Vigilant SDK provides several functions to emit metrics.

import (
  "github.com/vigilant-run/vigilant-golang/v2"
)

func function() {
  // Create a counter metric 
  vigilant.MetricCounter("user_login_count", 1.0)

  // Create a counter metric with a tag
  vigilant.MetricCounter("user_login_count", 1.0, vigilant.Tag("env", "production"))

  // Create a gauge metric
  vigilant.MetricGauge("active_users", 1.0, vigilant.GaugeModeSet)

  // Create a gauge metric with a tag
  vigilant.MetricGauge("active_users", 1.0, vigilant.GaugeModeSet, vigilant.Tag("env", "production"))

  // Create a histogram metric
  vigilant.MetricHistogram("request_duration", 123.4)

  // Create a histogram metric with a tag
  vigilant.MetricHistogram("request_duration", 123.4, vigilant.Tag("env", "production"))
}

Optional Setup

The Vigilant SDK exposes a few setup options when you initialize it.

package main

import (
  "github.com/vigilant-run/vigilant-golang/v2"
)

func main() {
  config := vigilant.NewConfigBuilder().
    // Disable all functionality
    WithNoop(true).
    // Disable logging to the console
    WithPassthrough(false)

  vigilant.Init(config.Build())
  defer vigilant.Shutdown()
}