From d243c31e43b00abe34f8fb49a764dee78370030e Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Thu, 18 Oct 2018 16:21:11 -0700 Subject: [PATCH] config.yaml: categorize and reorganize YAML file Categorize namespaces of each item into: - exporters - interceptors - zpages which aesthetically looks nicer than the original but also gives users better context and control over what the agent does. For example here is the new configuration file: ```yaml interceptors: opencensus: address: "127.0.0.1:55678" exporters: stackdriver: project: "project-id" enable_tracing: true zipkin: endpoint: "http://localhost:9411/api/v2/spans" datadog: enable_tracing: false zpages: port: 55679 ``` Fixes #90 --- README.md | 11 +++--- cmd/ocagent/config.go | 49 ++++++++++++++++++++++---- cmd/ocagent/config.yaml | 18 +++++----- exporter/exporterparser/datadog.go | 39 +++++++++++--------- exporter/exporterparser/stackdriver.go | 20 ++++++----- exporter/exporterparser/zipkin.go | 23 +++++++----- 6 files changed, 106 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 2987d81a..1fecb54c 100644 --- a/README.md +++ b/README.md @@ -261,12 +261,13 @@ it with the exporter and interceptor configurations. For example, to allow trace exporting to Stackdriver and Zipkin: ```yaml -stackdriver: - project: "your-project-id" - enable_traces: true +exporters: + stackdriver: + project: "your-project-id" + enable_traces: true -zipkin: - endpoint: "http://localhost:9411/api/v2/spans" + zipkin: + endpoint: "http://localhost:9411/api/v2/spans" ``` #### Interceptors diff --git a/cmd/ocagent/config.go b/cmd/ocagent/config.go index c355427e..17e3c646 100644 --- a/cmd/ocagent/config.go +++ b/cmd/ocagent/config.go @@ -23,14 +23,36 @@ import ( "github.com/census-instrumentation/opencensus-service/exporter/exporterparser" ) +// We expect the configuration.yaml file to look like this: +// +// interceptors: +// opencensus: +// port: +// zipkin: +// reporter:
+// +// exporters: +// stackdriver: +// project: +// enable_tracing: true +// zipkin: +// endpoint: "http://localhost:9411/api/v2/spans" +// +// zpages: +// port: 55679 + const ( defaultOCInterceptorAddress = "localhost:55678" defaultZPagesPort = 55679 ) type config struct { - OpenCensusInterceptorConfig *interceptorConfig `yaml:"opencensus_interceptor"` - ZPagesConfig *zPagesConfig `yaml:"zpages"` + Interceptors *interceptors `yaml:"interceptors"` + ZPages *zPagesConfig `yaml:"zpages"` +} + +type interceptors struct { + OpenCensusInterceptor *interceptorConfig `yaml:"opencensus"` } type interceptorConfig struct { @@ -44,17 +66,21 @@ type zPagesConfig struct { } func (c *config) ocInterceptorAddress() string { - if c == nil || c.OpenCensusInterceptorConfig == nil || c.OpenCensusInterceptorConfig.Address == "" { + if c == nil || c.Interceptors == nil { + return defaultOCInterceptorAddress + } + inCfg := c.Interceptors + if inCfg.OpenCensusInterceptor == nil || inCfg.OpenCensusInterceptor.Address == "" { return defaultOCInterceptorAddress } - return c.OpenCensusInterceptorConfig.Address + return inCfg.OpenCensusInterceptor.Address } func (c *config) zPagesDisabled() bool { if c == nil { return true } - return c.ZPagesConfig != nil && c.ZPagesConfig.Disabled + return c.ZPages != nil && c.ZPages.Disabled } func (c *config) zPagesPort() (int, bool) { @@ -62,8 +88,8 @@ func (c *config) zPagesPort() (int, bool) { return -1, false } port := defaultZPagesPort - if c != nil && c.ZPagesConfig != nil && c.ZPagesConfig.Port > 0 { - port = c.ZPagesConfig.Port + if c != nil && c.ZPages != nil && c.ZPages.Port > 0 { + port = c.ZPages.Port } return port, true } @@ -94,10 +120,19 @@ func exportersFromYAMLConfig(config []byte) (traceExporters []exporter.TraceExpo if err != nil { log.Fatalf("Failed to create config for %q: %v", cfg.name, err) } + nonNilExporters := 0 for _, te := range tes { if te != nil { traceExporters = append(traceExporters, te) + nonNilExporters += 1 + } + } + if nonNilExporters > 0 { + pluralization := "exporter" + if nonNilExporters > 1 { + pluralization = "exporters" } + log.Printf("%q trace-%s enabled", cfg.name, pluralization) } for _, doneFn := range tesDoneFns { doneFns = append(doneFns, doneFn) diff --git a/cmd/ocagent/config.yaml b/cmd/ocagent/config.yaml index d5371d7d..a935c486 100644 --- a/cmd/ocagent/config.yaml +++ b/cmd/ocagent/config.yaml @@ -1,12 +1,14 @@ -opencensus_interceptor: - address: "127.0.0.1:55678" +interceptors: + opencensus: + address: "127.0.0.1:55678" -stackdriver: - project: "project-id" - enable_tracing: true +exporters: + stackdriver: + project: "project-id" + enable_tracing: true -zipkin: - endpoint: "http://localhost:9411/api/v2/spans" + zipkin: + endpoint: "http://localhost:9411/api/v2/spans" zpages: - port: 55679 + port: 55679 diff --git a/exporter/exporterparser/datadog.go b/exporter/exporterparser/datadog.go index a85f766a..b8340726 100644 --- a/exporter/exporterparser/datadog.go +++ b/exporter/exporterparser/datadog.go @@ -24,24 +24,22 @@ import ( "github.com/census-instrumentation/opencensus-service/exporter" ) -type dataDogConfig struct { - Datadog *struct { - // Namespace specifies the namespaces to which metric keys are appended. - Namespace string `yaml:"namespace,omitempty"` +type datadogConfig struct { + // Namespace specifies the namespaces to which metric keys are appended. + Namespace string `yaml:"namespace,omitempty"` - // TraceAddr specifies the host[:port] address of the Datadog Trace Agent. - // It defaults to localhost:8126. - TraceAddr string `yaml:"trace_addr,omitempty"` + // TraceAddr specifies the host[:port] address of the Datadog Trace Agent. + // It defaults to localhost:8126. + TraceAddr string `yaml:"trace_addr,omitempty"` - // MetricsAddr specifies the host[:port] address for DogStatsD. It defaults - // to localhost:8125. - MetricsAddr string `yaml:"metrics_addr,omitempty"` + // MetricsAddr specifies the host[:port] address for DogStatsD. It defaults + // to localhost:8125. + MetricsAddr string `yaml:"metrics_addr,omitempty"` - // Tags specifies a set of global tags to attach to each metric. - Tags []string `yaml:"tags,omitempty"` + // Tags specifies a set of global tags to attach to each metric. + Tags []string `yaml:"tags,omitempty"` - EnableTracing bool `yaml:"enable_tracing,omitempty"` - } `yaml:"datadog,omitempty"` + EnableTracing bool `yaml:"enable_tracing,omitempty"` } type datadogExporter struct { @@ -49,12 +47,19 @@ type datadogExporter struct { } func DatadogTraceExportersFromYAML(config []byte) (tes []exporter.TraceExporter, doneFns []func() error, err error) { - var c dataDogConfig - if err := yamlUnmarshal(config, &c); err != nil { + var cfg struct { + Exporters *struct { + Datadog *datadogConfig `yaml:"datadog"` + } `yaml:"exporters"` + } + if err := yamlUnmarshal(config, &cfg); err != nil { return nil, nil, err } + if cfg.Exporters == nil { + return nil, nil, nil + } - dc := c.Datadog + dc := cfg.Exporters.Datadog if dc == nil { return nil, nil, nil } diff --git a/exporter/exporterparser/stackdriver.go b/exporter/exporterparser/stackdriver.go index 2f53b55a..845768c5 100644 --- a/exporter/exporterparser/stackdriver.go +++ b/exporter/exporterparser/stackdriver.go @@ -26,10 +26,8 @@ import ( ) type stackdriverConfig struct { - Stackdriver *struct { - ProjectID string `yaml:"project,omitempty"` - EnableTracing bool `yaml:"enable_tracing,omitempty"` - } `yaml:"stackdriver,omitempty"` + ProjectID string `yaml:"project,omitempty"` + EnableTracing bool `yaml:"enable_tracing,omitempty"` } type stackdriverExporter struct { @@ -39,12 +37,18 @@ type stackdriverExporter struct { var _ exporter.TraceExporter = (*stackdriverExporter)(nil) func StackdriverTraceExportersFromYAML(config []byte) (tes []exporter.TraceExporter, doneFns []func() error, err error) { - var c stackdriverConfig - if err := yamlUnmarshal(config, &c); err != nil { + var cfg struct { + Exporters *struct { + Stackdriver *stackdriverConfig `yaml:"stackdriver"` + } `yaml:"exporters"` + } + if err := yamlUnmarshal(config, &cfg); err != nil { return nil, nil, err } - - sc := c.Stackdriver + if cfg.Exporters == nil { + return nil, nil, nil + } + sc := cfg.Exporters.Stackdriver if sc == nil { return nil, nil, nil } diff --git a/exporter/exporterparser/zipkin.go b/exporter/exporterparser/zipkin.go index 3f4f4652..9a68844d 100644 --- a/exporter/exporterparser/zipkin.go +++ b/exporter/exporterparser/zipkin.go @@ -28,11 +28,9 @@ import ( ) type zipkinConfig struct { - Zipkin *struct { - ServiceName string `yaml:"service_name,omitempty"` - Endpoint string `yaml:"endpoint,omitempty"` - LocalEndpointURI string `yaml:"local_endpoint,omitempty"` - } `yaml:"zipkin,omitempty"` + ServiceName string `yaml:"service_name,omitempty"` + Endpoint string `yaml:"endpoint,omitempty"` + LocalEndpointURI string `yaml:"local_endpoint,omitempty"` } type zipkinExporter struct { @@ -40,15 +38,22 @@ type zipkinExporter struct { } func ZipkinExportersFromYAML(config []byte) (tes []exporter.TraceExporter, doneFns []func() error, err error) { - var c zipkinConfig - if err := yamlUnmarshal(config, &c); err != nil { + var cfg struct { + Exporters *struct { + Zipkin *zipkinConfig `yaml:"zipkin"` + } `yaml:"exporters"` + } + if err := yamlUnmarshal(config, &cfg); err != nil { return nil, nil, err } + if cfg.Exporters == nil { + return nil, nil, nil + } - if c.Zipkin == nil { + zc := cfg.Exporters.Zipkin + if zc == nil { return nil, nil, nil } - zc := c.Zipkin endpoint := "http://localhost:9411/api/v2/spans" if zc.Endpoint != "" { endpoint = zc.Endpoint