1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-20 03:30:02 +02:00

s/options/config/ (#410)

Co-authored-by: Joshua MacDonald <jmacd@users.noreply.github.com>
This commit is contained in:
Liz Fong-Jones 2020-01-03 12:48:45 -05:00 committed by Joshua MacDonald
parent 71f38454c8
commit 4c9bf78ab3
14 changed files with 88 additions and 88 deletions

View File

@ -203,7 +203,7 @@ func TestDefaultSDK(t *testing.T) {
counter.Add(ctx, 1, labels1)
in, out := io.Pipe()
pusher, err := stdout.InstallNewPipeline(stdout.Options{
pusher, err := stdout.InstallNewPipeline(stdout.Config{
Writer: out,
DoNotPrintTime: true,
})

View File

@ -53,7 +53,7 @@ func initTracer() {
}
func initMeter() *push.Controller {
pusher, err := metricstdout.InstallNewPipeline(metricstdout.Options{
pusher, err := metricstdout.InstallNewPipeline(metricstdout.Config{
Quantiles: []float64{0.5, 0.9, 0.99},
PrettyPrint: false,
})

View File

@ -34,7 +34,7 @@ var (
)
func initMeter() *push.Controller {
pusher, hf, err := prometheus.InstallNewPipeline(prometheus.Options{})
pusher, hf, err := prometheus.InstallNewPipeline(prometheus.Config{})
if err != nil {
log.Panicf("failed to initialize prometheus exporter %v", err)
}

View File

@ -28,7 +28,7 @@ import (
)
type (
Options = statsd.Options
Config = statsd.Config
// Exporter implements a dogstatsd-format statsd exporter,
// which encodes label sets as independent fields in the
@ -55,26 +55,26 @@ var (
// This type implements the metric.LabelEncoder interface,
// allowing the SDK's unique label encoding to be pre-computed
// for the exporter and stored in the LabelSet.
func NewRawExporter(options Options) (*Exporter, error) {
func NewRawExporter(config Config) (*Exporter, error) {
exp := &Exporter{
LabelEncoder: statsd.NewLabelEncoder(),
}
var err error
exp.Exporter, err = statsd.NewExporter(options, exp)
exp.Exporter, err = statsd.NewExporter(config, exp)
return exp, err
}
// InstallNewPipeline instantiates a NewExportPipeline and registers it globally.
// Typically called as:
// pipeline, err := dogstatsd.InstallNewPipeline(dogstatsd.Options{...})
// pipeline, err := dogstatsd.InstallNewPipeline(dogstatsd.Config{...})
// if err != nil {
// ...
// }
// defer pipeline.Stop()
// ... Done
func InstallNewPipeline(options Options) (*push.Controller, error) {
controller, err := NewExportPipeline(options)
func InstallNewPipeline(config Config) (*push.Controller, error) {
controller, err := NewExportPipeline(config)
if err != nil {
return controller, err
}
@ -84,9 +84,9 @@ func InstallNewPipeline(options Options) (*push.Controller, error) {
// NewExportPipeline sets up a complete export pipeline with the recommended setup,
// chaining a NewRawExporter into the recommended selectors and batchers.
func NewExportPipeline(options Options) (*push.Controller, error) {
func NewExportPipeline(config Config) (*push.Controller, error) {
selector := simple.NewWithExactMeasure()
exporter, err := NewRawExporter(options)
exporter, err := NewRawExporter(config)
if err != nil {
return nil, err
}

View File

@ -52,7 +52,7 @@ func TestDogstatsLabels(t *testing.T) {
checkpointSet.Add(desc, cagg, key.New("A").String("B"))
var buf bytes.Buffer
exp, err := dogstatsd.NewRawExporter(dogstatsd.Options{
exp, err := dogstatsd.NewRawExporter(dogstatsd.Config{
Writer: &buf,
})
require.Nil(t, err)

View File

@ -38,7 +38,7 @@ func ExampleNew() {
}()
// Create a meter
pusher, err := dogstatsd.NewExportPipeline(dogstatsd.Options{
pusher, err := dogstatsd.NewExportPipeline(dogstatsd.Config{
// The Writer field provides test support.
Writer: writer,

View File

@ -34,8 +34,8 @@ import (
)
type (
// Options supports common options that apply to statsd exporters.
Options struct {
// Config supports common configuration that applies to statsd exporters.
Config struct {
// URL describes the destination for exporting statsd data.
// e.g., udp://host:port
// tcp://host:port
@ -57,7 +57,7 @@ type (
// exporters.
Exporter struct {
adapter Adapter
options Options
config Config
conn net.Conn
writer io.Writer
buffer bytes.Buffer
@ -88,17 +88,17 @@ var (
// NewExport returns a common implementation for exporters that Export
// statsd syntax.
func NewExporter(options Options, adapter Adapter) (*Exporter, error) {
if options.MaxPacketSize <= 0 {
options.MaxPacketSize = MaxPacketSize
func NewExporter(config Config, adapter Adapter) (*Exporter, error) {
if config.MaxPacketSize <= 0 {
config.MaxPacketSize = MaxPacketSize
}
var writer io.Writer
var conn net.Conn
var err error
if options.Writer != nil {
writer = options.Writer
if config.Writer != nil {
writer = config.Writer
} else {
conn, err = dial(options.URL)
conn, err = dial(config.URL)
if conn != nil {
writer = conn
}
@ -108,7 +108,7 @@ func NewExporter(options Options, adapter Adapter) (*Exporter, error) {
// Start() and Stop() API.
return &Exporter{
adapter: adapter,
options: options,
config: config,
conn: conn,
writer: writer,
}, err
@ -171,7 +171,7 @@ func (e *Exporter) Export(_ context.Context, checkpointSet export.CheckpointSet)
return
}
if buf.Len() < e.options.MaxPacketSize {
if buf.Len() < e.config.MaxPacketSize {
return
}
if before == 0 {

View File

@ -113,11 +113,11 @@ timer.B.D:%s|ms
t.Run(nkind.String(), func(t *testing.T) {
ctx := context.Background()
writer := &testWriter{}
options := statsd.Options{
config := statsd.Config{
Writer: writer,
MaxPacketSize: 1024,
}
exp, err := statsd.NewExporter(options, adapter)
exp, err := statsd.NewExporter(config, adapter)
if err != nil {
t.Fatal("New error: ", err)
}
@ -274,12 +274,12 @@ func TestPacketSplit(t *testing.T) {
t.Run(tcase.name, func(t *testing.T) {
ctx := context.Background()
writer := &testWriter{}
options := statsd.Options{
config := statsd.Config{
Writer: writer,
MaxPacketSize: 1024,
}
adapter := newWithTagsAdapter()
exp, err := statsd.NewExporter(options, adapter)
exp, err := statsd.NewExporter(config, adapter)
if err != nil {
t.Fatal("New error: ", err)
}

View File

@ -50,8 +50,8 @@ type Exporter struct {
var _ export.Exporter = &Exporter{}
var _ http.Handler = &Exporter{}
// Options is a set of options for the tally reporter.
type Options struct {
// Config is a set of configs for the tally reporter.
type Config struct {
// Registry is the prometheus registry that will be used as the default Registerer and
// Gatherer if these are not specified.
//
@ -81,35 +81,35 @@ type Options struct {
// NewRawExporter returns a new prometheus exporter for prometheus metrics
// for use in a pipeline.
func NewRawExporter(opts Options) (*Exporter, error) {
if opts.Registry == nil {
opts.Registry = prometheus.NewRegistry()
func NewRawExporter(config Config) (*Exporter, error) {
if config.Registry == nil {
config.Registry = prometheus.NewRegistry()
}
if opts.Registerer == nil {
opts.Registerer = opts.Registry
if config.Registerer == nil {
config.Registerer = config.Registry
}
if opts.Gatherer == nil {
opts.Gatherer = opts.Registry
if config.Gatherer == nil {
config.Gatherer = config.Registry
}
if opts.OnError == nil {
opts.OnError = func(err error) {
if config.OnError == nil {
config.OnError = func(err error) {
fmt.Println(err.Error())
}
}
e := &Exporter{
handler: promhttp.HandlerFor(opts.Gatherer, promhttp.HandlerOpts{}),
registerer: opts.Registerer,
gatherer: opts.Gatherer,
defaultSummaryQuantiles: opts.DefaultSummaryQuantiles,
handler: promhttp.HandlerFor(config.Gatherer, promhttp.HandlerOpts{}),
registerer: config.Registerer,
gatherer: config.Gatherer,
defaultSummaryQuantiles: config.DefaultSummaryQuantiles,
}
c := newCollector(e)
if err := opts.Registerer.Register(c); err != nil {
opts.OnError(fmt.Errorf("cannot register the collector: %w", err))
if err := config.Registerer.Register(c); err != nil {
config.OnError(fmt.Errorf("cannot register the collector: %w", err))
}
return e, nil
@ -117,15 +117,15 @@ func NewRawExporter(opts Options) (*Exporter, error) {
// InstallNewPipeline instantiates a NewExportPipeline and registers it globally.
// Typically called as:
// pipeline, hf, err := prometheus.InstallNewPipeline(prometheus.Options{...})
// pipeline, hf, err := prometheus.InstallNewPipeline(prometheus.Config{...})
// if err != nil {
// ...
// }
// http.HandleFunc("/metrics", hf)
// defer pipeline.Stop()
// ... Done
func InstallNewPipeline(options Options) (*push.Controller, http.HandlerFunc, error) {
controller, hf, err := NewExportPipeline(options)
func InstallNewPipeline(config Config) (*push.Controller, http.HandlerFunc, error) {
controller, hf, err := NewExportPipeline(config)
if err != nil {
return controller, hf, err
}
@ -135,9 +135,9 @@ func InstallNewPipeline(options Options) (*push.Controller, http.HandlerFunc, er
// NewExportPipeline sets up a complete export pipeline with the recommended setup,
// chaining a NewRawExporter into the recommended selectors and batchers.
func NewExportPipeline(options Options) (*push.Controller, http.HandlerFunc, error) {
func NewExportPipeline(config Config) (*push.Controller, http.HandlerFunc, error) {
selector := simple.NewWithExactMeasure()
exporter, err := NewRawExporter(options)
exporter, err := NewRawExporter(config)
if err != nil {
return nil, nil, err
}

View File

@ -19,7 +19,7 @@ import (
)
func TestPrometheusExporter(t *testing.T) {
exporter, err := prometheus.NewRawExporter(prometheus.Options{
exporter, err := prometheus.NewRawExporter(prometheus.Config{
DefaultSummaryQuantiles: []float64{0.5, 0.9, 0.99},
})
if err != nil {

View File

@ -11,7 +11,7 @@ import (
func ExampleNewExportPipeline() {
// Create a meter
pusher, err := stdout.NewExportPipeline(stdout.Options{
pusher, err := stdout.NewExportPipeline(stdout.Config{
PrettyPrint: true,
DoNotPrintTime: true,
})

View File

@ -34,13 +34,13 @@ import (
)
type Exporter struct {
options Options
config Config
}
var _ export.Exporter = &Exporter{}
// Options are the options to be used when initializing a stdout export.
type Options struct {
// Config is the configuration to be used when initializing a stdout export.
type Config struct {
// Writer is the destination. If not set, os.Stdout is used.
Writer io.Writer
@ -87,34 +87,34 @@ type expoQuantile struct {
}
// NewRawExporter creates a stdout Exporter for use in a pipeline.
func NewRawExporter(options Options) (*Exporter, error) {
if options.Writer == nil {
options.Writer = os.Stdout
func NewRawExporter(config Config) (*Exporter, error) {
if config.Writer == nil {
config.Writer = os.Stdout
}
if options.Quantiles == nil {
options.Quantiles = []float64{0.5, 0.9, 0.99}
if config.Quantiles == nil {
config.Quantiles = []float64{0.5, 0.9, 0.99}
} else {
for _, q := range options.Quantiles {
for _, q := range config.Quantiles {
if q < 0 || q > 1 {
return nil, aggregator.ErrInvalidQuantile
}
}
}
return &Exporter{
options: options,
config: config,
}, nil
}
// InstallNewPipeline instantiates a NewExportPipeline and registers it globally.
// Typically called as:
// pipeline, err := stdout.InstallNewPipeline(stdout.Options{...})
// pipeline, err := stdout.InstallNewPipeline(stdout.Config{...})
// if err != nil {
// ...
// }
// defer pipeline.Stop()
// ... Done
func InstallNewPipeline(options Options) (*push.Controller, error) {
controller, err := NewExportPipeline(options)
func InstallNewPipeline(config Config) (*push.Controller, error) {
controller, err := NewExportPipeline(config)
if err != nil {
return controller, err
}
@ -124,9 +124,9 @@ func InstallNewPipeline(options Options) (*push.Controller, error) {
// NewExportPipeline sets up a complete export pipeline with the recommended setup,
// chaining a NewRawExporter into the recommended selectors and batchers.
func NewExportPipeline(options Options) (*push.Controller, error) {
func NewExportPipeline(config Config) (*push.Controller, error) {
selector := simple.NewWithExactMeasure()
exporter, err := NewRawExporter(options)
exporter, err := NewRawExporter(config)
if err != nil {
return nil, err
}
@ -142,7 +142,7 @@ func (e *Exporter) Export(_ context.Context, checkpointSet export.CheckpointSet)
// to be duplicates of the same error.
var aggError error
var batch expoBatch
if !e.options.DoNotPrintTime {
if !e.config.DoNotPrintTime {
ts := time.Now()
batch.Timestamp = &ts
}
@ -196,11 +196,11 @@ func (e *Exporter) Export(_ context.Context, checkpointSet export.CheckpointSet)
expose.Min = min.AsInterface(kind)
}
if dist, ok := agg.(aggregator.Distribution); ok && len(e.options.Quantiles) != 0 {
summary := make([]expoQuantile, len(e.options.Quantiles))
if dist, ok := agg.(aggregator.Distribution); ok && len(e.config.Quantiles) != 0 {
summary := make([]expoQuantile, len(e.config.Quantiles))
expose.Quantiles = summary
for i, q := range e.options.Quantiles {
for i, q := range e.config.Quantiles {
var vstr interface{}
if value, err := dist.Quantile(q); err != nil {
aggError = err
@ -227,7 +227,7 @@ func (e *Exporter) Export(_ context.Context, checkpointSet export.CheckpointSet)
} else {
expose.LastValue = value.AsInterface(kind)
if !e.options.DoNotPrintTime {
if !e.config.DoNotPrintTime {
expose.Timestamp = &timestamp
}
}
@ -250,14 +250,14 @@ func (e *Exporter) Export(_ context.Context, checkpointSet export.CheckpointSet)
var data []byte
var err error
if e.options.PrettyPrint {
if e.config.PrettyPrint {
data, err = json.MarshalIndent(batch, "", "\t")
} else {
data, err = json.Marshal(batch)
}
if err == nil {
fmt.Fprintln(e.options.Writer, string(data))
fmt.Fprintln(e.config.Writer, string(data))
} else {
return err
}

View File

@ -32,11 +32,11 @@ type testFixture struct {
output *bytes.Buffer
}
func newFixture(t *testing.T, options stdout.Options) testFixture {
func newFixture(t *testing.T, config stdout.Config) testFixture {
buf := &bytes.Buffer{}
options.Writer = buf
options.DoNotPrintTime = true
exp, err := stdout.NewRawExporter(options)
config.Writer = buf
config.DoNotPrintTime = true
exp, err := stdout.NewRawExporter(config)
if err != nil {
t.Fatal("Error building fixture: ", err)
}
@ -60,7 +60,7 @@ func (fix testFixture) Export(checkpointSet export.CheckpointSet) {
}
func TestStdoutInvalidQuantile(t *testing.T) {
_, err := stdout.NewRawExporter(stdout.Options{
_, err := stdout.NewRawExporter(stdout.Config{
Quantiles: []float64{1.1, 0.9},
})
require.Error(t, err, "Invalid quantile error expected")
@ -69,12 +69,12 @@ func TestStdoutInvalidQuantile(t *testing.T) {
func TestStdoutTimestamp(t *testing.T) {
var buf bytes.Buffer
exporter, err := stdout.NewRawExporter(stdout.Options{
exporter, err := stdout.NewRawExporter(stdout.Config{
Writer: &buf,
DoNotPrintTime: false,
})
if err != nil {
t.Fatal("Invalid options: ", err)
t.Fatal("Invalid config: ", err)
}
before := time.Now()
@ -123,7 +123,7 @@ func TestStdoutTimestamp(t *testing.T) {
}
func TestStdoutCounterFormat(t *testing.T) {
fix := newFixture(t, stdout.Options{})
fix := newFixture(t, stdout.Config{})
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
@ -140,7 +140,7 @@ func TestStdoutCounterFormat(t *testing.T) {
}
func TestStdoutGaugeFormat(t *testing.T) {
fix := newFixture(t, stdout.Options{})
fix := newFixture(t, stdout.Config{})
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
@ -157,7 +157,7 @@ func TestStdoutGaugeFormat(t *testing.T) {
}
func TestStdoutMinMaxSumCount(t *testing.T) {
fix := newFixture(t, stdout.Options{})
fix := newFixture(t, stdout.Config{})
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
@ -175,7 +175,7 @@ func TestStdoutMinMaxSumCount(t *testing.T) {
}
func TestStdoutMeasureFormat(t *testing.T) {
fix := newFixture(t, stdout.Options{
fix := newFixture(t, stdout.Config{
PrettyPrint: true,
})
@ -231,7 +231,7 @@ func TestStdoutEmptyDataSet(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
fix := newFixture(t, stdout.Options{})
fix := newFixture(t, stdout.Config{})
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
@ -248,7 +248,7 @@ func TestStdoutEmptyDataSet(t *testing.T) {
}
func TestStdoutGaugeNotSet(t *testing.T) {
fix := newFixture(t, stdout.Options{})
fix := newFixture(t, stdout.Config{})
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())

View File

@ -24,7 +24,7 @@ import (
)
func ExampleNew() {
pusher, err := stdout.NewExportPipeline(stdout.Options{
pusher, err := stdout.NewExportPipeline(stdout.Config{
PrettyPrint: true,
DoNotPrintTime: true, // This makes the output deterministic
})