Skip to content

Commit

Permalink
config.yaml: categorize and reorganize YAML file
Browse files Browse the repository at this point in the history
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 census-instrumentation#90
  • Loading branch information
odeke-em committed Oct 18, 2018
1 parent 3cd221b commit d243c31
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 54 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

#### <a name="agent-config-interceptors"></a>Interceptors
Expand Down
49 changes: 42 additions & 7 deletions cmd/ocagent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: <port>
// zipkin:
// reporter: <address>
//
// exporters:
// stackdriver:
// project: <project_id>
// 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 {
Expand All @@ -44,26 +66,30 @@ 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) {
if c.zPagesDisabled() {
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
}
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 10 additions & 8 deletions cmd/ocagent/config.yaml
Original file line number Diff line number Diff line change
@@ -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
39 changes: 22 additions & 17 deletions exporter/exporterparser/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,42 @@ 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 {
exporter *datadog.Exporter
}

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
}
Expand Down
20 changes: 12 additions & 8 deletions exporter/exporterparser/stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
23 changes: 14 additions & 9 deletions exporter/exporterparser/zipkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,32 @@ 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 {
exporter *zipkin.Exporter
}

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
Expand Down

0 comments on commit d243c31

Please sign in to comment.