Go prometheus client example

From UVOO Tech Wiki
Revision as of 02:02, 21 February 2025 by Busk (talk | contribs) (Created page with "# Simple client ``` set -a MIMIR_USERNAME=myuser MIMIR_PASSWORD=mypass MIMIR_URL=https://mimir.example.com/api/v1/push ``` promql ``` instance=go-client ``` ``` package main...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Simple client

set -a
MIMIR_USERNAME=myuser
MIMIR_PASSWORD=mypass
MIMIR_URL=https://mimir.example.com/api/v1/push

promql

instance=go-client
package main

import (
        "bytes"
        "fmt"
        "log"
        "net/http"
        "os"
        "time"

        "github.com/golang/protobuf/proto"
        "github.com/golang/snappy"
        pb "github.com/prometheus/prometheus/prompb"
)

// Function to create a sample metric
func createMetric() *pb.WriteRequest {
        return &pb.WriteRequest{
                Timeseries: []pb.TimeSeries{
                        {
                                Labels: []pb.Label{
                                        {Name: "__name__", Value: "test_metric"},
                                        {Name: "instance", Value: "go-client"},
                                },
                                Samples: []pb.Sample{
                                        {Value: 42.0, Timestamp: time.Now().UnixMilli()},
                                },
                        },
                },
        }
}

// Function to push the metric to Mimir
func pushToMimir(url, username, password string) error {
        metric := createMetric()

        // Serialize Protobuf
        data, err := proto.Marshal(metric)
        if err != nil {
                return fmt.Errorf("failed to serialize Protobuf: %v", err)
        }

        // Compress using Snappy
        compressedData := snappy.Encode(nil, data)

        // Create HTTP request
        req, err := http.NewRequest("POST", url, bytes.NewReader(compressedData))
        if err != nil {
                return fmt.Errorf("failed to create request: %v", err)
        }

        // Set Headers
        req.Header.Set("Content-Type", "application/x-protobuf")
        req.Header.Set("Content-Encoding", "snappy")

        // Add basic authentication (if credentials are provided)
        if username != "" && password != "" {
                req.SetBasicAuth(username, password)
        }

        // Send request
        client := &http.Client{}
        resp, err := client.Do(req)
        if err != nil {
                return fmt.Errorf("failed to send request: %v", err)
        }
        defer resp.Body.Close()

        // Check response status
        if resp.StatusCode != http.StatusOK {
                return fmt.Errorf("server returned: %v", resp.Status)
        }

        log.Println("Metric successfully pushed to Mimir")
        return nil
}

func main() {
        // Read environment variables with default values
        mimirURL := os.Getenv("MIMIR_URL")
        if mimirURL == "" {
                mimirURL = "https://examplemimir.example.com/api/v1/push"
        }

        username := os.Getenv("MIMIR_USERNAME")
        if username == "" {
                username = "your_username"
        }

        password := os.Getenv("MIMIR_PASSWORD")
        if password == "" {
                password = "your_password"
        }

        err := pushToMimir(mimirURL, username, password)
        if err != nil {
                log.Fatalf("Error pushing to Mimir: %v", err)
        }
}