Skip to content

Commit

Permalink
Add opencensus exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed May 27, 2019
1 parent d744a2b commit 82e2032
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 9 deletions.
6 changes: 5 additions & 1 deletion cmd/modern-go-application/config.go
Expand Up @@ -38,7 +38,10 @@ type configuration struct {
Instrumentation instrumentationConfig

// OpenCensus configuration
Opencensus opencensus.Config
Opencensus struct {
Exporter opencensus.ExporterConfig
Trace opencensus.TraceConfig
}

// App configuration
App struct {
Expand Down Expand Up @@ -167,6 +170,7 @@ func configure(v *viper.Viper, p *pflag.FlagSet) {
_ = v.BindEnv("instrumentation.jaeger.password")

// OpenCensus configuration
v.RegisterAlias("opencensus.exporter.serviceName", "appName")
v.SetDefault("opencensus.trace.sampling.sampler", "never")

// App configuration
Expand Down
13 changes: 13 additions & 0 deletions cmd/modern-go-application/main.go
Expand Up @@ -9,6 +9,7 @@ import (
"syscall"
"time"

"contrib.go.opencensus.io/exporter/ocagent"
"github.com/InVisionApp/go-health"
"github.com/InVisionApp/go-health/checkers"
"github.com/cloudflare/tableflip"
Expand Down Expand Up @@ -119,6 +120,18 @@ func main() {

trace.ApplyConfig(config.Opencensus.Trace.Config())

// Configure OpenCensus exporter
{
exporter, err := ocagent.NewExporter(append(
config.Opencensus.Exporter.Options(),
ocagent.WithServiceName(appName),
)...)
emperror.Panic(err)

trace.RegisterExporter(exporter)
view.RegisterExporter(exporter)
}

// configure Prometheus
if config.Instrumentation.Prometheus.Enabled {
logger.Info("prometheus exporter enabled")
Expand Down
5 changes: 5 additions & 0 deletions config.toml.dist
Expand Up @@ -15,6 +15,11 @@ enabled = false
[instrumentation.jaeger]
enabled = false

[opencensus.agent]
address = "127.0.0.1:55678"
insecure = false
reconnectPeriod = "5s"

[opencensus.trace]
sampling = { sampler = "always" }
# sampling = { sampler = "probability", fraction = 0.5 }
Expand Down
9 changes: 9 additions & 0 deletions docker-compose.override.yml.dist
Expand Up @@ -23,3 +23,12 @@ services:
ports:
- 127.0.0.1:14268:14268
- 127.0.0.1:16686:16686

oc-collector:
ports:
- 127.0.0.1:55680:55679

oc-agent:
ports:
- 127.0.0.1:55678:55678
- 127.0.0.1:55679:55679
16 changes: 16 additions & 0 deletions docker-compose.yml
Expand Up @@ -40,3 +40,19 @@ services:

jaeger:
image: jaegertracing/all-in-one:1.11

oc-collector:
image: omnition/opencensus-collector:latest
command: ["--config=/etc/opencensus/collector.yaml"]
volumes:
- ./etc/opencensus/:/etc/opencensus/:ro
depends_on:
- jaeger

oc-agent:
image: omnition/opencensus-agent:latest
command: ["--config=/etc/opencensus/agent.yaml"]
volumes:
- ./etc/opencensus/:/etc/opencensus/:ro
depends_on:
- oc-collector
7 changes: 7 additions & 0 deletions etc/opencensus/agent.yaml
@@ -0,0 +1,7 @@
receivers:
opencensus:
address: ":55678"

exporters:
opencensus:
endpoint: "oc-collector:55678"
14 changes: 14 additions & 0 deletions etc/opencensus/collector.yaml
@@ -0,0 +1,14 @@
receivers:
opencensus:
port: 55678

exporters:
queued-exporters:
jaeger-all-in-one:
num-workers: 4
queue-size: 100
retry-on-failure: true
sender-type: jaeger-thrift-http
jaeger-thrift-http:
collector-endpoint: http://jaeger:14268/api/traces
timeout: 5s
5 changes: 2 additions & 3 deletions go.mod
@@ -1,8 +1,8 @@
module github.com/sagikazarmark/modern-go-application

require (
cloud.google.com/go v0.35.1 // indirect
contrib.go.opencensus.io/exporter/jaeger v0.1.0
contrib.go.opencensus.io/exporter/ocagent v0.5.0
contrib.go.opencensus.io/exporter/prometheus v0.1.0
github.com/99designs/gqlgen v0.8.3
github.com/99designs/gqlgen-contrib v0.0.0-20190222015228-c654377d611c
Expand All @@ -17,7 +17,6 @@ require (
github.com/goph/idgen v0.3.0
github.com/goph/logur v0.11.0
github.com/goph/watermillx v0.2.0
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.7.0
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-runewidth v0.0.4 // indirect
Expand All @@ -26,7 +25,6 @@ require (
github.com/oklog/run v1.0.0
github.com/olekukonko/tablewriter v0.0.1
github.com/opencensus-integrations/ocsql v0.1.3
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
github.com/pkg/errors v0.8.1
github.com/sagikazarmark/ocmux v0.1.0
github.com/sirupsen/logrus v1.3.0
Expand All @@ -36,5 +34,6 @@ require (
github.com/stretchr/testify v1.3.0
github.com/vektah/gqlparser v1.1.2
go.opencensus.io v0.21.0
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2 // indirect
google.golang.org/grpc v1.21.0
)
8 changes: 8 additions & 0 deletions go.sum
Expand Up @@ -4,6 +4,8 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
cloud.google.com/go v0.35.1/go.mod h1:wfjPZNvXCBYESy3fIynybskMP48KVPrjSPCnXiK7Prg=
contrib.go.opencensus.io/exporter/jaeger v0.1.0 h1:WNc9HbA38xEQmsI40Tjd/MNU/g8byN2Of7lwIjv0Jdc=
contrib.go.opencensus.io/exporter/jaeger v0.1.0/go.mod h1:VYianECmuFPwU37O699Vc1GOcy+y8kOsfaxHRImmjbA=
contrib.go.opencensus.io/exporter/ocagent v0.5.0 h1:TKXjQSRS0/cCDrP7KvkgU6SmILtF/yV2TOs/02K/WZQ=
contrib.go.opencensus.io/exporter/ocagent v0.5.0/go.mod h1:ImxhfLRpxoYiSq891pBrLVhN+qmP8BTVvdH2YLs7Gl0=
contrib.go.opencensus.io/exporter/prometheus v0.1.0 h1:SByaIoWwNgMdPSgl5sMqM2KDE5H/ukPWBRo314xiDvg=
contrib.go.opencensus.io/exporter/prometheus v0.1.0/go.mod h1:cGFniUXGZlKRjzOyuZJ6mgB+PgBcCIa79kEKR8YCW+A=
dmitri.shuralyov.com/app/changes v0.0.0-20180602232624-0a106ad413e3/go.mod h1:Yl+fi1br7+Rr3LqpNJf1/uxUdtRUV+Tnj0o93V2B9MU=
Expand Down Expand Up @@ -60,6 +62,8 @@ github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBT
github.com/bugsnag/bugsnag-go v1.4.0/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8=
github.com/bugsnag/panicwrap v1.2.0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/census-instrumentation/opencensus-proto v0.2.0 h1:LzQXZOgg4CQfE6bFvXGM30YZL1WW/M337pXml+GrcZ4=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down Expand Up @@ -161,6 +165,9 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmg
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
github.com/grpc-ecosystem/grpc-gateway v1.6.2/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
github.com/grpc-ecosystem/grpc-gateway v1.8.5 h1:2+KSC78XiO6Qy0hIjfc1OD9H+hsaJdJlb8Kqsd41CTE=
github.com/grpc-ecosystem/grpc-gateway v1.8.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.9.0 h1:bM6ZAFZmc/wPFaRDi0d5L7hGEZEx/2u+Tmr2evNHDiI=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -467,6 +474,7 @@ google.golang.org/api v0.1.0 h1:K6z2u68e86TPdSdefXdzvXgR1zEMa+459vBSfWYAZkI=
google.golang.org/api v0.1.0/go.mod h1:UGEZY7KEX120AnNLIHFMKIo4obdJhkp2tPbaPlQx13Y=
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/api v0.3.2/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.5.0 h1:lj9SyhMzyoa38fgFF0oO2T6pjs5IzkLPKfVtxpyCRMM=
google.golang.org/api v0.5.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
Expand Down
28 changes: 28 additions & 0 deletions internal/platform/opencensus/exporter.go
@@ -0,0 +1,28 @@
package opencensus

import (
"time"

"contrib.go.opencensus.io/exporter/ocagent"
)

// ExporterConfig configures an OpenCensus exporter.
type ExporterConfig struct {
Address string
Insecure bool
ReconnectPeriod time.Duration
}

// Options returns a set of OpenCensus exporter options used for configuring the exporter.
func (c ExporterConfig) Options() []ocagent.ExporterOption {
options := []ocagent.ExporterOption{
ocagent.WithAddress(c.Address),
ocagent.WithReconnectionPeriod(c.ReconnectPeriod),
}

if c.Insecure {
options = append(options, ocagent.WithInsecure())
}

return options
}
Expand Up @@ -6,11 +6,6 @@ import (
"go.opencensus.io/trace"
)

// Config configures OpenCensus.
type Config struct {
Trace TraceConfig
}

// TraceConfig configures OpenCensus tracing.
type TraceConfig struct {
// Sampling describes the default sampler used when creating new spans.
Expand Down

0 comments on commit 82e2032

Please sign in to comment.