This topic includes instructions on how to send custom metrics to Logz.io from your GO application.

The included example uses the OpenTelemetry GO SDK and the OpenTelemetry Cortex exporter, which are both in alpha/preview.

Quick start

Before you begin, you’ll need: Go 1.x or higher

Installation

go get -u go.opentelemetry.io/contrib/exporters/metric/cortex

Add instruments to your application:

Set the variables in the following code snippet:

Environment variable Description
Endpoint The Logz.io Listener URL for for your region, configured to use port 8052 for http traffic, or port 8053 for https traffic. Replace <<LISTENER-HOST>> with the host for your region. For example, listener.logz.io if your account is hosted on AWS US East, or listener-nl.logz.io if hosted on Azure West Europe.
BearerToken Your Logz.io Prometheus Metrics account token. Replace <<PROMETHEUS-METRICS-SHIPPING-TOKEN>> with a token for the Metrics account you want to ship to.
Here’s how to look up your Metrics token.
p8s_logzio_name Label to add to all custom metrics
package main

import (
	"context"
	"fmt"
	"math/rand"
	"time"

	"go.opentelemetry.io/contrib/exporters/metric/cortex"
	"go.opentelemetry.io/otel/label"
	"go.opentelemetry.io/otel/metric"
	controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
	"go.opentelemetry.io/otel/sdk/resource"
)

func main() {
	// Create a new Config struct.
	config := cortex.Config{
		Endpoint:      "<<LISTENER-HOST>>",
		RemoteTimeout: 30 * time.Second,
		PushInterval:  15 * time.Second,
		BearerToken:   "<<PROMETHEUS-METRICS-SHIPPING-TOKEN>>",
	}
	// Create and install the exporter. Additionally, set the push interval to 15 seconds
	// and add a resource to the controller.
	pusher, err := cortex.InstallNewPipeline(config, controller.WithCollectPeriod(15*time.Second), controller.WithResource(resource.NewWithAttributes(label.String("p8s_logzio_name", "<<label-value>>"))))
	if err != nil {
		fmt.Printf("Error: %v", err)
	}
	ctx := context.Background()
	defer handleErr(pusher.Stop(ctx))
	// Create a counter and a value recorder
	meter := pusher.MeterProvider().Meter("go_metrics")
	// Create instruments
	counter := metric.Must(meter).NewInt64Counter(
		"go_metrics.counter",
		metric.WithDescription("Counts things"),
	)
    // Start the metrics pipline
	pusher.Start(ctx)
	// Record values to the instruments and add labels
    counter.Add(ctx, 10, label.String("key", "value"))
}
func handleErr(err error) {
	if err != nil {
		fmt.Println("Encountered error: ", err.Error())
	}
}
Types of metric instruments

For more information, see the OpenTelemetry documentation.

Name Behavior Default aggregation
Counter Metric value can only go up or be reset to 0, calculated per counter.Add(context,value,labels) request. Sum
UpDownCounter Metric value can arbitrarily increment or decrement, calculated per updowncounter.Add(context,value,labels) request. Sum
ValueRecorder Metric values captured by the valuerecorder.Record(context,value,labels) function, calculated per request. TBD
SumObserver Metric value can only go up or be reset to 0, calculated per push interval. Sum
UpDownSumObserver Metric value can arbitrarily increment or decrement, calculated per push interval. Sum
ValueObserver Metric values captured by the callback function, calculated per push interval. LastValue
Counter
// create counter instruments
counter := metric.Must(meter).NewInt64Counter(
	"go_metrics.counter",
	metric.WithDescription("your metric description"),
)
float_counter := metric.Must(meter).NewFloat64Counter(
	"go_metrics.float_counter",
	metric.WithDescription("your metric description"),
)
// Record values to the instruments and add labels
counter.Add(ctx, int64(10), label.String("key", "value"))
float_counter.Add(ctx, float64(8.3), label.String("key", "value"))
UpDownCounter
// create updowncounter instruments
updowncounter := metric.Must(meter).NewInt64UpDownCounter(
	"go_metrics.updowncounter",
	metric.WithDescription("your metric description"),
)
float_updowncounter := metric.Must(meter).NewFloat64UpDownCounter(
	"go_metrics.float_updowncounter",
	metric.WithDescription("your metric description"),
)
// Record values to the instruments and add labels
updowncounter.Add(ctx, int64(-3), label.String("key", "value"))
float_updowncounter.Add(ctx, float64(8.3), label.String("key", "value"))
ValueRecorder
// create ValueRecorder instruments
valuerecorder := metric.Must(meter).NewInt64ValueRecorder(
	"go_metrics.valuerecorder",
	metric.WithDescription("your metric description"),
)
float_valuerecorder := metric.Must(meter).NewFloat64ValueRecorder(
	"go_metrics.float_valuerecorder",
	metric.WithDescription("your metric description"),
)
// Record values to the instruments and add labels
valuerecorder.Record(ctx, int(83), label.String("key", "value"))
float_valuerecorder.Record(ctx, float64(8.3), label.String("key", "value"))
SumObserver
// Create callback for your SumObserver instrument
observerCallback := func(_ context.Context, result metric.Int64ObserverResult) {
	result.Observe(1, label.String("key", "value"))
}
// Start the metrics pipline
pusher.Start(ctx)
// create SumObserver instruments
_ = metric.Must(meter).NewInt64SumObserver("sum_observer", observerCallback,
	metric.WithDescription("A SumObserver set to 1"),
)
UpDownSumObserver
// Create callback for your UpDownSumObserver instrument
observerCallback := func(_ context.Context, result metric.Int64ObserverResult) {
	result.Observe(-5, label.String("key", "value"))
}
// Start the metrics pipline
pusher.Start(ctx)
// create SumObserver instruments
_ = metric.Must(meter).NewInt64UpDownSumObserver("updown_sum_observer", observerCallback,
	metric.WithDescription("A UpDownSumObserver set to -5"),
)
ValueObserver
// Create callback for your UpDownSumObserver instrument
observerCallback := func(_ context.Context, result metric.Int64ObserverResult) {
	result.Observe(83, label.String("key", "value"))
}
// Start the metrics pipline
pusher.Start(ctx)
// create SumObserver instruments
_ = metric.Must(meter).NewInt64ValueObserver("valueobserver", observerCallback,
	metric.WithDescription("A valueobserver set to 83"),
)
Check Logz.io for your metrics

Give your data some time to get from your system to ours, then log in to your Logz.io Metrics account, and open the Logz.io Metrics tab.