From d6fe86a3c2fbe3787769dca11c5c28143aff853a Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Mon, 8 Oct 2018 16:29:32 -1000 Subject: [PATCH] cmd/ocagent: OpenCensus interceptor bind address configurable via config.yaml Use the config.yaml file to configure the OpenCensus interceptor listener's address, instead of using commandline flags. This is because all the other configurations are performed using the config.yaml file so an extra option is just confusing. The default port to run on is 55678 if unspecified, but otherwise YAML parseable by key `opencensus_interceptor`, and then using the field name `address`. For example: ```yaml opencensus_interceptor: address: ":55777" ``` and when run with ```shell ocagent ``` which will then produce: ```shell 2018/10/08 16:36:47 Running OpenCensus interceptor as a gRPC service at "localhost:55777" ``` Fixes #23 --- README.md | 47 +++++++++++++++++++++++++++++------------ cmd/ocagent/config.go | 45 +++++++++++++++++++++++++++++++++++++++ cmd/ocagent/config.yaml | 3 +++ cmd/ocagent/main.go | 16 ++++++++------ 4 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 cmd/ocagent/config.go diff --git a/README.md b/README.md index 278e27844ec7..7ba780f77601 100644 --- a/README.md +++ b/README.md @@ -160,18 +160,21 @@ backend (e.g Jaeger Thrift Span), and then push them to corresponding backend or First, install ocagent if you haven't. -``` +```shell $ go get github.com/census-instrumentation/opencensus-service/cmd/ocagent ``` +### Configuration file + Create a config.yaml file in the current directory and modify -it with the exporter configuration. For example, following -configuration exports both to Stackdriver and Zipkin. +it with the exporter and interceptor configurations. -config.yaml: +#### Exporters -``` +For example, to allow trace exporting to Stackdriver and Zipkin: + +```yaml stackdriver: project: "your-project-id" enableTraces: true @@ -180,22 +183,37 @@ zipkin: endpoint: "http://localhost:9411/api/v2/spans" ``` -Run the example application that collects traces and exports -to the daemon if it is running. +#### Interceptors -``` -$ go run "$(go env GOPATH)/src/github.com/census-instrumentation/opencensus-service/example/main.go" +To modify the address that the OpenCensus interceptor runs on, please use the +YAML field name `opencensus_interceptor` and it takes fields like `address`. +For example: + +```yaml +opencensus_interceptor: + address: "localhost:55678" ``` -Run ocagent: +### Running an end-to-end example/demo -``` +Run the example application that collects traces and exports them +to the daemon. + +Firstly run ocagent: + +```shell $ ocagent ``` +Next run the demo application: + +```shell +$ go run "$(go env GOPATH)/src/github.com/census-instrumentation/opencensus-service/example/main.go" +``` + You should be able to see the traces in Stackdriver and Zipkin. -If you stop the ocagent, example application will stop exporting. -If you run it again, it will start exporting again. +If you stop the ocagent, the example application will stop exporting. +If you run it again, exporting will resume. ## OpenCensus Collector @@ -281,8 +299,9 @@ $ go run "$(go env GOPATH)/src/github.com/census-instrumentation/opencensus-serv Run ocagent: -``` +```shell $ ocagent +2018/10/08 21:38:00 Running OpenCensus interceptor as a gRPC service at "127.0.0.1:55678" ``` You should be able to see the traces in the configured tracing backend. diff --git a/cmd/ocagent/config.go b/cmd/ocagent/config.go new file mode 100644 index 000000000000..de9ed6a0aea7 --- /dev/null +++ b/cmd/ocagent/config.go @@ -0,0 +1,45 @@ +// Copyright 2018, OpenCensus Authors +// +// 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. + +package main + +import ( + yaml "gopkg.in/yaml.v2" +) + +const defaultOCInterceptorAddress = "localhost:55678" + +type topLevelConfig struct { + OpenCensusInterceptorConfig *config `yaml:"opencensus_interceptor"` +} + +type config struct { + // The address to which the OpenCensus interceptor will be bound and run on. + Address string `yaml:"address"` +} + +func (tcfg *topLevelConfig) openCensusInterceptorAddressOrDefault() string { + if tcfg == nil || tcfg.OpenCensusInterceptorConfig == nil || tcfg.OpenCensusInterceptorConfig.Address == "" { + return defaultOCInterceptorAddress + } + return tcfg.OpenCensusInterceptorConfig.Address +} + +func parseOCAgentConfig(yamlBlob []byte) (*topLevelConfig, error) { + cfg := new(topLevelConfig) + if err := yaml.Unmarshal(yamlBlob, cfg); err != nil { + return nil, err + } + return cfg, nil +} diff --git a/cmd/ocagent/config.yaml b/cmd/ocagent/config.yaml index 574f1a01ed76..834bcd4428ac 100644 --- a/cmd/ocagent/config.yaml +++ b/cmd/ocagent/config.yaml @@ -1,3 +1,6 @@ +opencensus_interceptor: + address: "127.0.0.1:55678" + stackdriver: project: "project-id" enableTraces: true diff --git a/cmd/ocagent/main.go b/cmd/ocagent/main.go index d8542d3a9dc4..b9dba5427131 100644 --- a/cmd/ocagent/main.go +++ b/cmd/ocagent/main.go @@ -37,21 +37,26 @@ import ( ) func main() { - ocInterceptorPort := flag.Int("oci-port", 55678, "The port on which the OpenCensus interceptor is run") - flag.Parse() - exportersYAMLConfigFile := flag.String("exporters-yaml", "config.yaml", "The YAML file with the configurations for the various exporters") + flag.Parse() yamlBlob, err := ioutil.ReadFile(*exportersYAMLConfigFile) if err != nil { log.Fatalf("Cannot read the YAML file %v error: %v", exportersYAMLConfigFile, err) } + ownConfig, err := parseOCAgentConfig(yamlBlob) + if err != nil { + log.Fatalf("Failed to parse own configuration %v error: %v", exportersYAMLConfigFile, err) + } + + ocInterceptorAddr := ownConfig.openCensusInterceptorAddressOrDefault() + traceExporters, _, closeFns := exporterparser.ExportersFromYAMLConfig(yamlBlob) commonSpanReceiver := exporter.OCExportersToTraceExporter(traceExporters...) // Add other interceptors here as they are implemented - ocInterceptorDoneFn, err := runOCInterceptor(*ocInterceptorPort, commonSpanReceiver) + ocInterceptorDoneFn, err := runOCInterceptor(ocInterceptorAddr, commonSpanReceiver) if err != nil { log.Fatal(err) } @@ -74,13 +79,12 @@ func main() { <-signalsChan } -func runOCInterceptor(ocInterceptorPort int, sr spanreceiver.SpanReceiver) (doneFn func(), err error) { +func runOCInterceptor(addr string, sr spanreceiver.SpanReceiver) (doneFn func(), err error) { oci, err := ocinterceptor.New(sr, ocinterceptor.WithSpanBufferPeriod(800*time.Millisecond)) if err != nil { return nil, fmt.Errorf("Failed to create the OpenCensus interceptor: %v", err) } - addr := fmt.Sprintf("localhost:%d", ocInterceptorPort) ln, err := net.Listen("tcp", addr) if err != nil { return nil, fmt.Errorf("Cannot bind to address %q: %v", addr, err)