Documentation ¶
Overview ¶
Package metrics provides client REST API for metrics collector (server).
Index ¶
Examples ¶
Constants ¶
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.
type Gauge ¶
type Gauge float64
Gauge represents numeric metric value which could go up or down, e.g. count of currently allocated virtual memory.
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 ¶
NewGetCounterReq creates new MetricReq structure to be used for retrieving of counter metric.
func NewGetGaugeReq ¶
NewGetGaugeReq creates new MetricReq structure to be used for retrieving of gauge metric.
func NewUpdateCounterReq ¶
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 ¶
NewUpdateGaugeReq creates new MetricReq structure to be used for updating gauge metric.