2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-11-22 06:46:05 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-03-02 23:54:57 +02:00
|
|
|
package dogstatsd // import "go.opentelemetry.io/otel/exporters/metric/dogstatsd"
|
2019-11-22 06:46:05 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-01-02 20:41:21 +02:00
|
|
|
"time"
|
2019-11-22 06:46:05 +02:00
|
|
|
|
2020-01-02 20:41:21 +02:00
|
|
|
"go.opentelemetry.io/otel/api/global"
|
2020-03-02 23:54:57 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/metric/internal/statsd"
|
2020-01-02 20:41:21 +02:00
|
|
|
|
2019-11-22 06:46:05 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2020-01-02 20:41:21 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/batcher/ungrouped"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/controller/push"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
|
2019-11-22 06:46:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2020-01-03 19:48:45 +02:00
|
|
|
Config = statsd.Config
|
2019-11-22 06:46:05 +02:00
|
|
|
|
|
|
|
// Exporter implements a dogstatsd-format statsd exporter,
|
|
|
|
// which encodes label sets as independent fields in the
|
|
|
|
// output.
|
|
|
|
//
|
|
|
|
// TODO: find a link for this syntax. It's been copied out of
|
|
|
|
// code, not a specification:
|
|
|
|
//
|
|
|
|
// https://github.com/stripe/veneur/blob/master/sinks/datadog/datadog.go
|
|
|
|
Exporter struct {
|
|
|
|
*statsd.Exporter
|
|
|
|
|
2020-03-24 18:30:12 +02:00
|
|
|
labelEncoder *statsd.LabelEncoder
|
2019-11-22 06:46:05 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-03-24 18:30:12 +02:00
|
|
|
_ export.Exporter = &Exporter{}
|
2019-11-22 06:46:05 +02:00
|
|
|
)
|
|
|
|
|
2020-01-02 20:41:21 +02:00
|
|
|
// NewRawExporter returns a new Dogstatsd-syntax exporter for use in a pipeline.
|
2020-01-03 19:48:45 +02:00
|
|
|
func NewRawExporter(config Config) (*Exporter, error) {
|
2019-11-22 06:46:05 +02:00
|
|
|
exp := &Exporter{
|
2020-03-24 18:30:12 +02:00
|
|
|
labelEncoder: statsd.NewLabelEncoder(),
|
2019-11-22 06:46:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2020-01-03 19:48:45 +02:00
|
|
|
exp.Exporter, err = statsd.NewExporter(config, exp)
|
2019-11-22 06:46:05 +02:00
|
|
|
return exp, err
|
|
|
|
}
|
|
|
|
|
2020-01-02 20:41:21 +02:00
|
|
|
// InstallNewPipeline instantiates a NewExportPipeline and registers it globally.
|
|
|
|
// Typically called as:
|
2020-02-19 07:43:03 +02:00
|
|
|
//
|
|
|
|
// pipeline, err := dogstatsd.InstallNewPipeline(dogstatsd.Config{...})
|
|
|
|
// if err != nil {
|
|
|
|
// ...
|
|
|
|
// }
|
|
|
|
// defer pipeline.Stop()
|
|
|
|
// ... Done
|
2020-01-03 19:48:45 +02:00
|
|
|
func InstallNewPipeline(config Config) (*push.Controller, error) {
|
2020-02-29 03:08:30 +02:00
|
|
|
controller, err := NewExportPipeline(config, time.Minute)
|
2020-01-02 20:41:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return controller, err
|
|
|
|
}
|
|
|
|
global.SetMeterProvider(controller)
|
|
|
|
return controller, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewExportPipeline sets up a complete export pipeline with the recommended setup,
|
|
|
|
// chaining a NewRawExporter into the recommended selectors and batchers.
|
2020-02-29 03:08:30 +02:00
|
|
|
func NewExportPipeline(config Config, period time.Duration) (*push.Controller, error) {
|
2020-01-02 20:41:21 +02:00
|
|
|
selector := simple.NewWithExactMeasure()
|
2020-01-03 19:48:45 +02:00
|
|
|
exporter, err := NewRawExporter(config)
|
2020-01-02 20:41:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The ungrouped batcher ensures that the export sees the full
|
|
|
|
// set of labels as dogstatsd tags.
|
2020-03-24 18:30:12 +02:00
|
|
|
batcher := ungrouped.New(selector, exporter.labelEncoder, false)
|
2020-01-02 20:41:21 +02:00
|
|
|
|
2020-02-29 03:08:30 +02:00
|
|
|
pusher := push.New(batcher, exporter, period)
|
2020-01-02 20:41:21 +02:00
|
|
|
pusher.Start()
|
|
|
|
|
|
|
|
return pusher, nil
|
|
|
|
}
|
|
|
|
|
2019-11-22 06:46:05 +02:00
|
|
|
// AppendName is part of the stats-internal adapter interface.
|
|
|
|
func (*Exporter) AppendName(rec export.Record, buf *bytes.Buffer) {
|
|
|
|
_, _ = buf.WriteString(rec.Descriptor().Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendTags is part of the stats-internal adapter interface.
|
|
|
|
func (e *Exporter) AppendTags(rec export.Record, buf *bytes.Buffer) {
|
2020-03-24 18:30:12 +02:00
|
|
|
encoded := rec.Labels().Encoded(e.labelEncoder)
|
2019-11-22 06:46:05 +02:00
|
|
|
_, _ = buf.WriteString(encoded)
|
|
|
|
}
|