prometheus

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2019 License: Apache-2.0 Imports: 16 Imported by: 0

README

prometheus-go-metrics-exporter

OpenCensus Prometheus Go Metrics exporter

Documentation

Overview

Example
package main

import (
	"context"
	"log"
	"net/http"
	"time"

	"github.com/orijtech/prometheus-go-metrics-exporter"

	metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
	"github.com/golang/protobuf/ptypes/timestamp"
)

func main() {
	pe, err := prometheus.New(prometheus.Options{})
	if err != nil {
		log.Fatalf("Failed to create new exporter: %v", err)
	}

	mux := http.NewServeMux()
	// Expose the Prometheus Metrics exporter for scraping on route /metrics.
	mux.Handle("/metrics", pe)

	// Now run the server.
	go func() {
		http.ListenAndServe(":8888", mux)
	}()

	// And finally in your client application, use the
	// OpenCensus-Go Metrics Prometheus exporter to record metrics.
	for {
		pe.ExportMetric(context.Background(), nil, nil, metric1)
		// Introducing a fake pause/period.
		<-time.After(350 * time.Millisecond)
	}
}

var (
	startTimestamp = &timestamp.Timestamp{
		Seconds: 1543160298,
		Nanos:   100000090,
	}
	endTimestamp = &timestamp.Timestamp{
		Seconds: 1543160298,
		Nanos:   100000997,
	}
)

var metric1 = &metricspb.Metric{
	Descriptor_: &metricspb.Metric_MetricDescriptor{
		MetricDescriptor: &metricspb.MetricDescriptor{
			Name:        "this/one/there(where)",
			Description: "Extra ones",
			Unit:        "1",
			LabelKeys: []*metricspb.LabelKey{
				{Key: "os", Description: "Operating system"},
				{Key: "arch", Description: "Architecture"},
			},
		},
	},
	Timeseries: []*metricspb.TimeSeries{
		{
			StartTimestamp: startTimestamp,
			LabelValues: []*metricspb.LabelValue{
				{Value: "windows"},
				{Value: "x86"},
			},
			Points: []*metricspb.Point{
				{
					Timestamp: endTimestamp,
					Value: &metricspb.Point_Int64Value{
						Int64Value: 99,
					},
				},
			},
		},
		{
			StartTimestamp: startTimestamp,
			LabelValues: []*metricspb.LabelValue{
				{Value: "darwin"},
				{Value: "386"},
			},
			Points: []*metricspb.Point{
				{
					Timestamp: endTimestamp,
					Value: &metricspb.Point_DoubleValue{
						DoubleValue: 49.5,
					},
				},
			},
		},
	},
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Exporter

type Exporter struct {
	// contains filtered or unexported fields
}

Exporter is the type that converts OpenCensus Proto-Metrics into Prometheus metrics.

func New

func New(o Options) (*Exporter, error)

New is the constructor to make an Exporter with the defined Options.

func (*Exporter) ExportMetric

func (exp *Exporter) ExportMetric(ctx context.Context, node *commonpb.Node, rsc *resourcepb.Resource, metric *metricspb.Metric) error

ExportMetric is the method that the exporter uses to convert OpenCensus Proto-Metrics to Prometheus metrics.

func (*Exporter) ServeHTTP

func (exp *Exporter) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Options

type Options struct {
	Namespace   string
	OnError     func(err error)
	ConstLabels prometheus.Labels // ConstLabels will be set as labels on all views.
	Registry    *prometheus.Registry
}

Options customizes a created Prometheus Exporter.

Jump to

Keyboard shortcuts

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