metrics

package
v0.0.0-...-c2add7f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2023 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package metrics provides client REST API for metrics collector (server).

Index

Examples

Constants

View Source
const (
	KindCounter = "counter"
	KindGauge   = "gauge"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

type Counter int64

Counter represents always growing numeric metric, e.g. count of HTTP requests.

func ToCounter

func ToCounter(value string) (Counter, error)

ToCounter creates new Counter metric object from string value.

func (Counter) Kind

func (c Counter) Kind() string

func (Counter) String

func (c Counter) String() string

type Gauge

type Gauge float64

Gauge represents numeric metric value which could go up or down, e.g. count of currently allocated virtual memory.

func ToGauge

func ToGauge(value string) (Gauge, error)

ToGauge creates new Gauge metric object from string value.

func (Gauge) Kind

func (g Gauge) Kind() string

func (Gauge) String

func (g Gauge) String() string

type Metric

type Metric interface {
	// Kind return kind of this metric, e.g. "counter".
	Kind() string

	// String provides string representation of metrics value.
	String() string
}

A Metric is common representation of all supported metrics kinds.

type MetricReq

type MetricReq struct {
	// Name of a metric.
	ID string `json:"id"`

	// One of supported metric kinds (e.g. counter, gauge), see constants.
	MType string `json:"type"`

	// Metric value if type is counter, must not be set for other types.
	Delta *Counter `json:"delta,omitempty"`

	// Metric value if type is gauge, must not be set for other types.
	Value *Gauge `json:"value,omitempty"`

	// Hash value of the data, may be omitted if signature validation is
	// disabled on server-side.
	Hash string `json:"hash,omitempty"`
}

MetricReq represents info regarding particular metric name, type and value. Used in REST API requests/responses to/from metrics collector.

func NewGetCounterReq

func NewGetCounterReq(name string) MetricReq

NewGetCounterReq creates new MetricReq structure to be used for retrieving of counter metric.

func NewGetGaugeReq

func NewGetGaugeReq(name string) MetricReq

NewGetGaugeReq creates new MetricReq structure to be used for retrieving of gauge metric.

func NewUpdateCounterReq

func NewUpdateCounterReq(name string, value Counter) MetricReq

NewUpdateCounterReq creates new MetricReq structure to be used for updating counter metric.

Example

This is an example of pushing counter metric to metrics collector.

package main

import (
	"bytes"
	"encoding/json"
	"log"
	"net/http"
	"net/http/httptest"

	"github.com/alkurbatov/metrics-collector/pkg/metrics"
)

func main() {
	// Record a counter metric.
	value := metrics.Counter(10)

	// Create new update counter request.
	data := metrics.NewUpdateCounterReq("ExampleCounter", value)

	// This is a HTTP handler for testing purposes.
	handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})

	// This is a HTTP server for testing purposes.
	srv := httptest.NewServer(handler)
	defer srv.Close()

	// Create JSON payload.
	payload, err := json.Marshal(data)
	if err != nil {
		log.Fatal(err)
	}

	body := bytes.NewReader(payload)

	// Send an HTTP request to the test server.
	req, err := http.NewRequest(http.MethodPost, srv.URL+"/update", body)
	if err != nil {
		log.Fatal(err)
	}

	req.Header.Set("Content-Type", "application/json")

	resp, err := srv.Client().Do(req)
	if err != nil {
		log.Fatal(err)
	}

	// For this example we don't care about the response.
	if err := resp.Body.Close(); err != nil {
		log.Fatal(err)
	}
}
Output:

func NewUpdateGaugeReq

func NewUpdateGaugeReq(name string, value Gauge) MetricReq

NewUpdateGaugeReq creates new MetricReq structure to be used for updating gauge metric.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL