Skip to content

Commit

Permalink
add NewExportPipeline and InstallNewPipeline for otlp (#1373)
Browse files Browse the repository at this point in the history
* add NewExportPipeline and InstallNewPipeline for otlp

Signed-off-by: binjip978 <binjip978@gmail.com>

* remove resource, AlwaysSample, Timeout, etc

* remove withSpanProcessor

Signed-off-by: binjip978 <binjip978@gmail.com>

* move changelog entry to unreleased

Signed-off-by: binjip978 <binjip978@gmail.com>

* merged with combined push/pull metrics controller

Signed-off-by: binjip978 <binjip978@gmail.com>

* remove SetMeterProvider

* Update CHANGELOG.md

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>

* Update CHANGELOG.md

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Update exporters/otlp/otlp.go

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 8, 2021
1 parent 7d8e6bd commit 5843280
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Added

- The OTLP exporter now has two new convenience functions, `NewExportPipeline` and `InstallNewPipeline`, setup and install the exporter in tracing and metrics pipelines. (#1373)
- Adds test to check BatchSpanProcessor ignores `OnEnd` and `ForceFlush` post `Shutdown`. (#1772)
- Option `ExportTimeout` was added to batch span processor. (#1755)
- Adds semantic conventions for exceptions. (#1492)
Expand Down
49 changes: 49 additions & 0 deletions exporters/otlp/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ import (
"errors"
"sync"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
metricsdk "go.opentelemetry.io/otel/sdk/export/metric"
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
sdktrace "go.opentelemetry.io/otel/sdk/trace"

"go.opentelemetry.io/otel/sdk/metric/controller/basic"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
)

Expand Down Expand Up @@ -128,3 +134,46 @@ func (e *Exporter) ExportKindFor(desc *metric.Descriptor, kind aggregation.Kind)
func (e *Exporter) ExportSpans(ctx context.Context, ss []*tracesdk.SpanSnapshot) error {
return e.driver.ExportTraces(ctx, ss)
}

// NewExportPipeline sets up a complete export pipeline
// with the recommended TracerProvider setup.
func NewExportPipeline(ctx context.Context, driver ProtocolDriver, exporterOpts ...ExporterOption) (*Exporter,
*sdktrace.TracerProvider, *basic.Controller, error) {

exp, err := NewExporter(ctx, driver, exporterOpts...)
if err != nil {
return nil, nil, nil, err
}

tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exp),
)

cntr := basic.New(
processor.New(
simple.NewWithInexpensiveDistribution(),
exp,
),
)

return exp, tracerProvider, cntr, nil
}

// InstallNewPipeline instantiates a NewExportPipeline with the
// recommended configuration and registers it globally.
func InstallNewPipeline(ctx context.Context, driver ProtocolDriver, exporterOpts ...ExporterOption) (*Exporter,
*sdktrace.TracerProvider, *basic.Controller, error) {

exp, tp, cntr, err := NewExportPipeline(ctx, driver, exporterOpts...)
if err != nil {
return nil, nil, nil, err
}

otel.SetTracerProvider(tp)
err = cntr.Start(ctx)
if err != nil {
return nil, nil, nil, err
}

return exp, tp, cntr, err
}
38 changes: 38 additions & 0 deletions exporters/otlp/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/exporters/otlp/internal/transform"
metricsdk "go.opentelemetry.io/otel/sdk/export/metric"
Expand Down Expand Up @@ -244,6 +245,43 @@ func TestExporterShutdownManyTimes(t *testing.T) {
}
}

func TestInstallNewPipeline(t *testing.T) {
ctx := context.Background()
_, _, _, err := otlp.InstallNewPipeline(ctx, &stubProtocolDriver{})
assert.NoError(t, err)
assert.IsType(t, &tracesdk.TracerProvider{}, otel.GetTracerProvider())
}

func TestNewExportPipeline(t *testing.T) {
testCases := []struct {
name string
expOpts []otlp.ExporterOption
testSpanSampling bool
}{
{
name: "simple pipeline",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, tp, _, err := otlp.NewExportPipeline(
context.Background(),
&stubProtocolDriver{},
tc.expOpts...,
)

assert.NoError(t, err)
assert.NotEqual(t, tp, otel.GetTracerProvider())

_, span := tp.Tracer("otlp test").Start(context.Background(), tc.name)
spanCtx := span.SpanContext()
assert.Equal(t, true, spanCtx.IsSampled())
span.End()
})
}
}

func TestSplitDriver(t *testing.T) {
driverTraces := &stubProtocolDriver{}
driverMetrics := &stubProtocolDriver{}
Expand Down

0 comments on commit 5843280

Please sign in to comment.