Go

How to send metrics using GO

Sending a metric via TCP

import (
    "fmt"
    "net"
)

...
conn, err := net.Dial("tcp", "YOUR-UID.carbon.hostedgraphite.com:2003")
_, err = fmt.Fprintf(conn, "YOUR-API-KEY.foo.go-tcp 1.2\n")
// don't forget to handle the error and close connection

Sending a metric via UDP

import (
    "fmt"
    "net"
)

...
conn, err := net.Dial("udp", "YOUR-UID.carbon.hostedgraphite.com:2003")
_, err = fmt.Fprintf(conn, "YOUR-API-KEY.foo.go-udp 1.2\n")
// don't forget to handle the error and close connection

Sending a metric using HTTP

import (
    "bytes"
    "encoding/base64"
    "net/http"
)

...
req, err := http.NewRequest("PUT", "https://www.hostedgraphite.com/api/v1/sink", bytes.NewBufferString("foo.go-http 1.2\n"))
base64_apikey := base64.StdEncoding.EncodeToString([]byte("YOUR-API-KEY"))
req.Header.Add( "Authorization", "Basic "+ base64_apikey )

client := http.Client{}
reply, errdo := client.Do(req)
// don't forget to handle the error and close connection

Your API key can be found on your account home page.

Last updated