No description
Find a file
hashicorp-copywrite[bot] 52113dcc97
[COMPLIANCE] Add Copyright and License Headers (#1)
Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2026-03-10 14:05:50 +05:30
api [COMPLIANCE] Add Copyright and License Headers (#1) 2026-03-10 14:05:50 +05:30
checkmgr doc: update comment 2018-08-15 14:47:37 -04:00
.gitignore ignore vendor dirctory 2018-02-07 14:13:29 -05:00
CHANGELOG.md v2.2.4 2018-09-28 14:15:05 -04:00
circonus-gometrics.go upd: remove redundant return statement 2018-02-07 14:16:34 -05:00
circonus-gometrics_test.go wrap errors 2017-10-13 17:30:44 -04:00
counter.go add: GetCounterTest for testing counters in code 'using' cgm (rather than having to mock up a full submission) 2017-08-18 11:39:02 -04:00
counter_test.go add: GetCounterTest for testing counters in code 'using' cgm (rather than having to mock up a full submission) 2017-08-18 11:39:02 -04:00
gauge.go remove obsolete code 2017-10-13 17:29:05 -04:00
gauge_test.go fix: missing arg to t.Fatalf 2018-02-07 12:48:42 -05:00
Gopkg.lock v2.1.2 2018-07-19 00:13:34 -04:00
Gopkg.toml add: dep for dependencies 2018-02-07 14:13:53 -05:00
histogram.go add RecordCountForValue to handle encoded histograms 2017-10-13 17:21:58 -04:00
histogram_test.go add RecordCountForValue to handle encoded histograms 2017-10-13 17:21:58 -04:00
LICENSE LICENSE and copyright added (#22) 2016-08-22 13:17:41 -04:00
metrics.go new: SetMetricTags and AddMetricTags 2016-10-21 12:28:58 -04:00
OPTIONS.md docs instructions for sending metrics to a local circonus-agent with the SubmissionURL 2017-10-14 15:55:16 -04:00
README.md doc: remove obsolete link 2018-09-27 10:38:53 -04:00
submit.go updated to support breaking change in latest go-retryablehttp 2019-04-05 15:10:49 +00:00
submit_test.go add: expose Metrics and Metric types for easier external use. 2017-09-14 11:37:12 -04:00
text.go LICENSE and copyright added (#22) 2016-08-22 13:17:41 -04:00
text_test.go LICENSE and copyright added (#22) 2016-08-22 13:17:41 -04:00
tools.go remove obsolete code 2017-10-13 17:28:30 -04:00
util.go fix: do not reset counter|gauge|text funcs after each snapshot (only on explicit call to Reset) 2018-08-15 14:23:08 -04:00
util_test.go expose gauge interface{} values for quick original type determination 2017-10-11 12:33:06 -04:00

Circonus metrics tracking for Go applications

This library supports named counters, gauges and histograms. It also provides convenience wrappers for registering latency instrumented functions with Go's builtin http server.

Initializing only requires setting an API Token at a minimum.

Options

See OPTIONS.md for information on all of the available cgm options.

Example

Bare bones minimum

A working cut-n-past example. Simply set the required environment variable CIRCONUS_API_TOKEN and run.

package main

import (
    "log"
    "math/rand"
    "os"
    "os/signal"
    "syscall"
    "time"

    cgm "github.com/circonus-labs/circonus-gometrics"
)

func main() {

    logger := log.New(os.Stdout, "", log.LstdFlags)

    logger.Println("Configuring cgm")

    cmc := &cgm.Config{}
    cmc.Debug = false // set to true for debug messages
    cmc.Log = logger

    // Circonus API Token key (https://login.circonus.com/user/tokens)
    cmc.CheckManager.API.TokenKey = os.Getenv("CIRCONUS_API_TOKEN")

    logger.Println("Creating new cgm instance")

    metrics, err := cgm.NewCirconusMetrics(cmc)
    if err != nil {
        logger.Println(err)
        os.Exit(1)
    }

    src := rand.NewSource(time.Now().UnixNano())
    rnd := rand.New(src)

    logger.Println("Adding ctrl-c trap")
    c := make(chan os.Signal, 2)
    signal.Notify(c, os.Interrupt, syscall.SIGTERM)
    go func() {
        <-c
        logger.Println("Received CTRL-C, flushing outstanding metrics before exit")
        metrics.Flush()
        os.Exit(0)
    }()

    logger.Println("Starting to send metrics")

    // number of "sets" of metrics to send
    max := 60

    for i := 1; i < max; i++ {
        logger.Printf("\tmetric set %d of %d", i, 60)
        metrics.Timing("foo", rnd.Float64()*10)
        metrics.Increment("bar")
        metrics.Gauge("baz", 10)
        time.Sleep(time.Second)
    }

    metrics.SetText("fini", "complete")

    logger.Println("Flushing any outstanding metrics manually")
    metrics.Flush()
}

A more complete example

A working, cut-n-paste example with all options available for modification. Also, demonstrates metric tagging.

package main

import (
    "log"
    "math/rand"
    "os"
    "os/signal"
    "syscall"
    "time"

    cgm "github.com/circonus-labs/circonus-gometrics"
)

func main() {

    logger := log.New(os.Stdout, "", log.LstdFlags)

    logger.Println("Configuring cgm")

    cmc := &cgm.Config{}

    // General

    cmc.Interval = "10s"
    cmc.Log = logger
    cmc.Debug = false
    cmc.ResetCounters = "true"
    cmc.ResetGauges = "true"
    cmc.ResetHistograms = "true"
    cmc.ResetText = "true"

    // Circonus API configuration options
    cmc.CheckManager.API.TokenKey = os.Getenv("CIRCONUS_API_TOKEN")
    cmc.CheckManager.API.TokenApp = os.Getenv("CIRCONUS_API_APP")
    cmc.CheckManager.API.URL = os.Getenv("CIRCONUS_API_URL")
    cmc.CheckManager.API.TLSConfig = nil

    // Check configuration options
    cmc.CheckManager.Check.SubmissionURL = os.Getenv("CIRCONUS_SUBMISSION_URL")
    cmc.CheckManager.Check.ID = os.Getenv("CIRCONUS_CHECK_ID")
    cmc.CheckManager.Check.InstanceID = ""
    cmc.CheckManager.Check.DisplayName = ""
    cmc.CheckManager.Check.TargetHost = ""
    //  if hn, err := os.Hostname(); err == nil {
    //      cmc.CheckManager.Check.TargetHost = hn
    //  }
    cmc.CheckManager.Check.SearchTag = ""
    cmc.CheckManager.Check.Secret = ""
    cmc.CheckManager.Check.Tags = ""
    cmc.CheckManager.Check.MaxURLAge = "5m"
    cmc.CheckManager.Check.ForceMetricActivation = "false"

    // Broker configuration options
    cmc.CheckManager.Broker.ID = ""
    cmc.CheckManager.Broker.SelectTag = ""
    cmc.CheckManager.Broker.MaxResponseTime = "500ms"
    cmc.CheckManager.Broker.TLSConfig = nil

    logger.Println("Creating new cgm instance")

    metrics, err := cgm.NewCirconusMetrics(cmc)
    if err != nil {
        logger.Println(err)
        os.Exit(1)
    }

    src := rand.NewSource(time.Now().UnixNano())
    rnd := rand.New(src)

    logger.Println("Adding ctrl-c trap")
    c := make(chan os.Signal, 2)
    signal.Notify(c, os.Interrupt, syscall.SIGTERM)
    go func() {
        <-c
        logger.Println("Received CTRL-C, flushing outstanding metrics before exit")
        metrics.Flush()
        os.Exit(0)
    }()

    // Add metric tags (append to any existing tags on specified metric)
    metrics.AddMetricTags("foo", []string{"cgm:test"})
    metrics.AddMetricTags("baz", []string{"cgm:test"})

    logger.Println("Starting to send metrics")

    // number of "sets" of metrics to send
    max := 60

    for i := 1; i < max; i++ {
        logger.Printf("\tmetric set %d of %d", i, 60)

        metrics.Timing("foo", rnd.Float64()*10)
        metrics.Increment("bar")
        metrics.Gauge("baz", 10)

        if i == 35 {
            // Set metric tags (overwrite current tags on specified metric)
            metrics.SetMetricTags("baz", []string{"cgm:reset_test", "cgm:test2"})
        }

        time.Sleep(time.Second)
    }

    logger.Println("Flushing any outstanding metrics manually")
    metrics.Flush()

}

HTTP Handler wrapping

http.HandleFunc("/", metrics.TrackHTTPLatency("/", handler_func))

HTTP latency example

package main

import (
    "os"
    "fmt"
    "net/http"
    cgm "github.com/circonus-labs/circonus-gometrics"
)

func main() {
    cmc := &cgm.Config{}
    cmc.CheckManager.API.TokenKey = os.Getenv("CIRCONUS_API_TOKEN")

    metrics, err := cgm.NewCirconusMetrics(cmc)
    if err != nil {
        panic(err)
    }

    http.HandleFunc("/", metrics.TrackHTTPLatency("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
    }))
    http.ListenAndServe(":8080", http.DefaultServeMux)
}

Unless otherwise noted, the source files are distributed under the BSD-style license found in the LICENSE file.