Source File
prometheus.go
Belonging Package
contrib.go.opencensus.io/exporter/prometheus
package prometheus // import "contrib.go.opencensus.io/exporter/prometheus"
import (
)
type Options struct {
Namespace string
Registry *prometheus.Registry
OnError func(err error)
ConstLabels prometheus.Labels // ConstLabels will be set as labels on all views.
}
func ( Options) (*Exporter, error) {
if .Registry == nil {
.Registry = prometheus.NewRegistry()
}
:= newCollector(, .Registry)
:= &Exporter{
opts: ,
g: .Registry,
c: ,
handler: promhttp.HandlerFor(.Registry, promhttp.HandlerOpts{}),
}
.ensureRegisteredOnce()
return , nil
}
var _ http.Handler = (*Exporter)(nil)
reader *metricexport.Reader
}
func ( *collector) ( chan<- *prometheus.Desc) {
:= &descExporter{c: , descCh: }
.reader.ReadAndExport()
}
func ( *collector) ( chan<- prometheus.Metric) {
:= &metricExporter{c: , metricCh: }
.reader.ReadAndExport()
}
func ( Options, *prometheus.Registry) *collector {
return &collector{
reg: ,
opts: ,
reader: metricexport.NewReader()}
}
func ( *collector) ( *metricdata.Metric) *prometheus.Desc {
return prometheus.NewDesc(
metricName(.opts.Namespace, ),
.Descriptor.Description,
toPromLabels(.Descriptor.LabelKeys),
.opts.ConstLabels)
}
type metricExporter struct {
c *collector
metricCh chan<- prometheus.Metric
}
func ( *metricExporter) ( context.Context, []*metricdata.Metric) error {
for , := range {
:= .c.toDesc()
for , := range .TimeSeries {
:= toLabelValues(.LabelValues)
for , := range .Points {
, := toPromMetric(, , , )
if != nil {
.c.opts.onError()
} else if != nil {
.metricCh <-
}
}
}
}
return nil
}
type descExporter struct {
c *collector
descCh chan<- *prometheus.Desc
}
func ( *descExporter) ( context.Context, []*metricdata.Metric) error {
for , := range {
:= .c.toDesc()
.descCh <-
}
return nil
}
func ( []metricdata.LabelKey) ( []string) {
for , := range {
= append(, sanitize(.Key))
}
return
}
func ( string, *metricdata.Metric) string {
var string
if != "" {
= + "_"
}
return + sanitize(.Descriptor.Name)
}
func (
*prometheus.Desc,
*metricdata.Metric,
metricdata.Point,
[]string) (prometheus.Metric, error) {
switch .Descriptor.Type {
case metricdata.TypeCumulativeFloat64, metricdata.TypeCumulativeInt64:
, := toPromValue()
if != nil {
return nil,
}
return prometheus.NewConstMetric(, prometheus.CounterValue, , ...)
case metricdata.TypeGaugeFloat64, metricdata.TypeGaugeInt64:
, := toPromValue()
if != nil {
return nil,
}
return prometheus.NewConstMetric(, prometheus.GaugeValue, , ...)
case metricdata.TypeCumulativeDistribution:
switch v := .Value.(type) {
case *metricdata.Distribution:
:= uint64(0)
for , := range .BucketOptions.Bounds {
+= uint64(.Buckets[].Count)
[] =
}
return prometheus.NewConstHistogram(, uint64(.Count), .Sum, , ...)
default:
return nil, typeMismatchError()
}
return nil, nil
default:
return nil, fmt.Errorf("aggregation %T is not yet supported", .Descriptor.Type)
}
}
func ( []metricdata.LabelValue) ( []string) {
for , := range {
if .Present {
= append(, .Value)
} else {
= append(, "")
}
}
return
}
func ( metricdata.Point) error {
return fmt.Errorf("point type %T does not match metric type", )
}
func ( metricdata.Point) (float64, error) {
switch v := .Value.(type) {
case float64:
return , nil
case int64:
return float64(), nil
default:
return 0.0, typeMismatchError()
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |