Skip to content

Commit

Permalink
Unify trace and metric exporter helpers (#944)
Browse files Browse the repository at this point in the history
* Adjust Jaeger and Zipkin exporters helper methods

* Update and add tests, examples, various minor improvements

* Update changelog

* Correct the Zipkin example

- wait for the spans to be exported
- rebuild the example

* Zipkin service name as argument

* Rework Jaeger and Zipkin tests

* Include more detailed Changelog

Co-authored-by: ET <[email protected]>
Co-authored-by: Liz Fong-Jones <[email protected]>
Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
4 people authored Jul 22, 2020
1 parent c5d77d2 commit f31d8ec
Show file tree
Hide file tree
Showing 9 changed files with 452 additions and 163 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Changed

- Jaeger exporter helpers: added InstallNewPipeline and removed RegisterGlobal option instead. (#944)
- Zipkin exporter helpers: pipeline methods introduced, new exporter method adjusted. (#944)

## [0.9.0] - 2020-07-20

### Added
Expand Down
3 changes: 1 addition & 2 deletions example/jaeger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// initTracer creates a new trace provider instance and registers it as global trace provider.
func initTracer() func() {
// Create and install Jaeger export pipeline
_, flush, err := jaeger.NewExportPipeline(
flush, err := jaeger.InstallNewPipeline(
jaeger.WithCollectorEndpoint("http://localhost:14268/api/traces"),
jaeger.WithProcess(jaeger.Process{
ServiceName: "trace-demo",
Expand All @@ -39,7 +39,6 @@ func initTracer() func() {
kv.Float64("float", 312.23),
},
}),
jaeger.RegisterAsGlobal(),
jaeger.WithSDK(&sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
)
if err != nil {
Expand Down
30 changes: 11 additions & 19 deletions example/zipkin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,20 @@ var logger = log.New(os.Stderr, "zipkin-example", log.Ldate|log.Ltime|log.Llongf

// initTracer creates a new trace provider instance and registers it as global trace provider.
func initTracer(url string) {
// Create Zipkin Exporter
exporter, err := zipkin.NewExporter(
url,
"zipkin-example",
zipkin.WithLogger(logger),
)
if err != nil {
log.Fatal(err)
}

// Create Zipkin Exporter and install it as a global tracer.
//
// For demoing purposes, always sample. In a production application, you should
// configure this to a trace.ProbabilitySampler set at the desired
// configure the sampler to a trace.ProbabilitySampler set at the desired
// probability.
tp, err := sdktrace.NewProvider(
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
sdktrace.WithBatcher(exporter,
sdktrace.WithBatchTimeout(5),
sdktrace.WithMaxExportBatchSize(10),
),
err := zipkin.InstallNewPipeline(
url,
"zipkin-test",
zipkin.WithLogger(logger),
zipkin.WithSDK(&sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
)
if err != nil {
log.Fatal(err)
}
global.SetTraceProvider(tp)
}

func main() {
Expand All @@ -73,7 +63,9 @@ func main() {
bar(ctx)
<-time.After(6 * time.Millisecond)
span.End()
<-time.After(24 * time.Millisecond)

// Wait for the spans to be exported.
<-time.After(5 * time.Second)
}

func bar(ctx context.Context) {
Expand Down
29 changes: 14 additions & 15 deletions exporters/trace/jaeger/jaeger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ type options struct {

Config *sdktrace.Config

// RegisterGlobal is set to true if the trace provider of the new pipeline should be
// registered as Global Trace Provider
RegisterGlobal bool

Disabled bool
}

Expand Down Expand Up @@ -82,14 +78,8 @@ func WithSDK(config *sdktrace.Config) Option {
}
}

// RegisterAsGlobal enables the registration of the trace provider of the new pipeline
// as Global Trace Provider.
func RegisterAsGlobal() Option {
return func(o *options) {
o.RegisterGlobal = true
}
}

// WithDisabled option will cause pipeline methods to use
// a no-op provider
func WithDisabled(disabled bool) Option {
return func(o *options) {
o.Disabled = disabled
Expand Down Expand Up @@ -177,13 +167,22 @@ func NewExportPipeline(endpointOption EndpointOption, opts ...Option) (apitrace.
if exporter.o.Config != nil {
tp.ApplyConfig(*exporter.o.Config)
}
if exporter.o.RegisterGlobal {
global.SetTraceProvider(tp)
}

return tp, exporter.Flush, nil
}

// InstallNewPipeline instantiates a NewExportPipeline with the
// recommended configuration and registers it globally.
func InstallNewPipeline(endpointOption EndpointOption, opts ...Option) (func(), error) {
tp, flushFn, err := NewExportPipeline(endpointOption, opts...)
if err != nil {
return nil, err
}

global.SetTraceProvider(tp)
return flushFn, nil
}

// Process contains the information exported to jaeger about the source
// of the trace data.
type Process struct {
Expand Down
Loading

0 comments on commit f31d8ec

Please sign in to comment.