metricsgen

command module
v0.0.0-...-32eb673 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2018 License: BSD-3-Clause Imports: 11 Imported by: 0

README

Metricsgen

GoDoc Build Status

Metricsgen generates interface implementation that measures execution time of all methods using prometheus SDK.

Installation

go get -u github.com/gojuno/metricsgen

Usage

Imagine you have the following interface:

type Example interface {
	Do(a, b string) error
	Another(c string)
}

Here is how to generate metrics decorator for this interface:

metricsgen -i github.com/gojuno/metricsgen/tests.Example -o ./tests/

The result will be:

package tests

/*
DO NOT EDIT!
This code was generated automatically using github.com/gojuno/metricsgen v1.0
The original interface "Example" can be found in github.com/gojuno/metricsgen/tests
*/
import (
	"time"

	"github.com/prometheus/client_golang/prometheus"
)

type ExampleMetrics struct {
	next     Example
	summary  *prometheus.SummaryVec
	instance string
}

func NewExampleMetricsSummary(metricName string) *prometheus.SummaryVec {
	sv := prometheus.NewSummaryVec(
		prometheus.SummaryOpts{
			Name: metricName,
			Help: metricName,
		},
		[]string{"instance", "method"},
	)

	prometheus.MustRegister(sv)

	return sv
}

func NewExampleMetricsWithSummary(next Example, instance string, sv *prometheus.SummaryVec) *ExampleMetrics {
	return &ExampleMetrics{
		next:     next,
		summary:  sv,
		instance: instance,
	}
}

func (m *ExampleMetrics) Another(p string) {
	defer m.observe("Another", time.Now())

	m.next.Another(p)
}

func (m *ExampleMetrics) Do(p string, p1 string) (r error) {
	defer m.observe("Do", time.Now())

	return m.next.Do(p, p1)
}

func (m *ExampleMetrics) observe(method string, startedAt time.Time) {
	duration := time.Since(startedAt)
	m.summary.WithLabelValues(m.instance, method).Observe(duration.Seconds())
}
Metrics

Decorator creates prometheus summary vector with two labels:

  • instance. it's used to separate different instances of single interface to avoid metric name collisions.
  • method. Interface method name.
Constructor
//create and register summary vector
sv := NewExampleMetricsSummary("example_metric_name")

//create two implementations of the Example interface decorated with methods' execution time metrics
ex1 := NewExampleMetricsWithSummary(next, sv, "instance_1")
ex2 := NewExampleMetricsWithSummary(next, sv, "instance_2")

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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