forked from goadesign/clue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OTLP exporter constructors for convenience (goadesign#353)
- Loading branch information
Showing
6 changed files
with
407 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package clue | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" | ||
"go.opentelemetry.io/otel/sdk/metric" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
|
||
"goa.design/clue/log" | ||
) | ||
|
||
// Allow mocking | ||
var ( | ||
otlpmetricgrpcNew = otlpmetricgrpc.New | ||
otlpmetrichttpNew = otlpmetrichttp.New | ||
otlptracegrpcNew = otlptracegrpc.New | ||
otlptracehttpNew = otlptracehttp.New | ||
) | ||
|
||
// NewGRPCMetricExporter returns an OpenTelementry Protocol metric exporter that | ||
// report metrics to a gRPC collector. | ||
func NewGRPCMetricExporter(ctx context.Context, options ...otlpmetricgrpc.Option) (exporter metric.Exporter, shutdown func(), err error) { | ||
exporter, err = otlpmetricgrpcNew(ctx, options...) | ||
if err != nil { | ||
return | ||
} | ||
shutdown = func() { | ||
// Create new context in case the parent context has been canceled. | ||
ctx := log.WithContext(context.Background(), ctx) | ||
if err := exporter.Shutdown(ctx); err != nil { | ||
log.Errorf(ctx, err, "failed to shutdown metric exporter") | ||
} | ||
} | ||
return | ||
} | ||
|
||
// NewGRPCSpanExporter returns an OpenTelementry Protocol span exporter that | ||
// report spans to a gRPC collector. | ||
func NewGRPCSpanExporter(ctx context.Context, options ...otlptracegrpc.Option) (exporter trace.SpanExporter, shutdown func(), err error) { | ||
exporter, err = otlptracegrpcNew(ctx, options...) | ||
if err != nil { | ||
return | ||
} | ||
shutdown = func() { | ||
// Create new context in case the parent context has been canceled. | ||
ctx := log.WithContext(context.Background(), ctx) | ||
if err := exporter.Shutdown(ctx); err != nil { | ||
log.Errorf(ctx, err, "failed to shutdown span exporter") | ||
} | ||
} | ||
return | ||
} | ||
|
||
// NewHTTPMetricExporter returns an OpenTelementry Protocol metric exporter that | ||
// report metrics to a HTTP collector. | ||
func NewHTTPMetricExporter(ctx context.Context, options ...otlpmetrichttp.Option) (exporter metric.Exporter, shutdown func(), err error) { | ||
exporter, err = otlpmetrichttpNew(ctx, options...) | ||
if err != nil { | ||
return | ||
} | ||
shutdown = func() { | ||
// Create new context in case the parent context has been canceled. | ||
ctx := log.WithContext(context.Background(), ctx) | ||
if err := exporter.Shutdown(ctx); err != nil { | ||
log.Errorf(ctx, err, "failed to shutdown metric exporter") | ||
} | ||
} | ||
return | ||
} | ||
|
||
// NewHTTPSpanExporter returns an OpenTelementry Protocol span exporter that | ||
// report spans to a HTTP collector. | ||
func NewHTTPSpanExporter(ctx context.Context, options ...otlptracehttp.Option) (exporter trace.SpanExporter, shutdown func(), err error) { | ||
exporter, err = otlptracehttpNew(ctx, options...) | ||
if err != nil { | ||
return | ||
} | ||
shutdown = func() { | ||
// Create new context in case the parent context has been canceled. | ||
ctx := log.WithContext(context.Background(), ctx) | ||
if err := exporter.Shutdown(ctx); err != nil { | ||
log.Errorf(ctx, err, "failed to shutdown span exporter") | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
package clue | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" | ||
"goa.design/clue/log" | ||
) | ||
|
||
func TestNewGRPCMetricExporter(t *testing.T) { | ||
testErr := errors.New("test error") | ||
// Define test cases | ||
tests := []struct { | ||
name string | ||
options []otlpmetricgrpc.Option | ||
newErr error | ||
wantLog string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Success", | ||
}, | ||
{ | ||
name: "Options", | ||
options: []otlpmetricgrpc.Option{otlpmetricgrpc.WithInsecure(), otlpmetricgrpc.WithEndpoint("test")}, | ||
}, | ||
{ | ||
name: "New Error", | ||
newErr: testErr, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
otlpmetricgrpcNew = func(ctx context.Context, options ...otlpmetricgrpc.Option) (*otlpmetricgrpc.Exporter, error) { | ||
if tt.newErr != nil { | ||
return nil, tt.newErr | ||
} | ||
return otlpmetricgrpc.New(ctx) | ||
} | ||
var buf bytes.Buffer | ||
ctx := log.Context(context.Background(), log.WithOutput(&buf)) | ||
|
||
exporter, shutdown, err := NewGRPCMetricExporter(ctx, tt.options...) | ||
|
||
if tt.wantErr { | ||
assert.Error(t, err) | ||
assert.Nil(t, exporter) | ||
assert.Nil(t, shutdown) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
assert.NotNil(t, exporter) | ||
assert.NotNil(t, shutdown) | ||
shutdown() | ||
assert.Empty(t, buf.String()) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewHTTPMetricExporter(t *testing.T) { | ||
testErr := errors.New("test error") | ||
// Define test cases | ||
tests := []struct { | ||
name string | ||
options []otlpmetrichttp.Option | ||
newErr error | ||
wantLog string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Success", | ||
}, | ||
{ | ||
name: "Options", | ||
options: []otlpmetrichttp.Option{otlpmetrichttp.WithEndpoint("test")}, | ||
}, | ||
{ | ||
name: "New Error", | ||
newErr: testErr, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
otlpmetrichttpNew = func(ctx context.Context, options ...otlpmetrichttp.Option) (*otlpmetrichttp.Exporter, error) { | ||
if tt.newErr != nil { | ||
return nil, tt.newErr | ||
} | ||
return otlpmetrichttp.New(ctx) | ||
} | ||
var buf bytes.Buffer | ||
ctx := log.Context(context.Background(), log.WithOutput(&buf)) | ||
|
||
exporter, shutdown, err := NewHTTPMetricExporter(ctx, tt.options...) | ||
|
||
if tt.wantErr { | ||
assert.Error(t, err) | ||
assert.Nil(t, exporter) | ||
assert.Nil(t, shutdown) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
assert.NotNil(t, exporter) | ||
assert.NotNil(t, shutdown) | ||
shutdown() | ||
assert.Empty(t, buf.String()) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewGRPCSpanExporter(t *testing.T) { | ||
testErr := errors.New("test error") | ||
// Define test cases | ||
tests := []struct { | ||
name string | ||
options []otlptracegrpc.Option | ||
newErr error | ||
wantLog string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Success", | ||
}, | ||
{ | ||
name: "Options", | ||
options: []otlptracegrpc.Option{otlptracegrpc.WithInsecure(), otlptracegrpc.WithEndpoint("test")}, | ||
}, | ||
{ | ||
name: "New Error", | ||
newErr: testErr, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
otlptracegrpcNew = func(ctx context.Context, options ...otlptracegrpc.Option) (*otlptrace.Exporter, error) { | ||
if tt.newErr != nil { | ||
return nil, tt.newErr | ||
} | ||
return otlptracegrpc.New(ctx) | ||
} | ||
var buf bytes.Buffer | ||
ctx := log.Context(context.Background(), log.WithOutput(&buf)) | ||
|
||
exporter, shutdown, err := NewGRPCSpanExporter(ctx, tt.options...) | ||
|
||
if tt.wantErr { | ||
assert.Error(t, err) | ||
assert.Nil(t, exporter) | ||
assert.Nil(t, shutdown) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
assert.NotNil(t, exporter) | ||
assert.NotNil(t, shutdown) | ||
shutdown() | ||
assert.Empty(t, buf.String()) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewHTTPSpanExporter(t *testing.T) { | ||
testErr := errors.New("test error") | ||
// Define test cases | ||
tests := []struct { | ||
name string | ||
options []otlptracehttp.Option | ||
newErr error | ||
wantLog string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Success", | ||
}, | ||
{ | ||
name: "Options", | ||
options: []otlptracehttp.Option{otlptracehttp.WithEndpoint("test")}, | ||
}, | ||
{ | ||
name: "New Error", | ||
newErr: testErr, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
otlptracehttpNew = func(ctx context.Context, options ...otlptracehttp.Option) (*otlptrace.Exporter, error) { | ||
if tt.newErr != nil { | ||
return nil, tt.newErr | ||
} | ||
return otlptracehttp.New(ctx) | ||
} | ||
var buf bytes.Buffer | ||
ctx := log.Context(context.Background(), log.WithOutput(&buf)) | ||
|
||
exporter, shutdown, err := NewHTTPSpanExporter(ctx, tt.options...) | ||
|
||
if tt.wantErr { | ||
assert.Error(t, err) | ||
assert.Nil(t, exporter) | ||
assert.Nil(t, shutdown) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
assert.NotNil(t, exporter) | ||
assert.NotNil(t, shutdown) | ||
shutdown() | ||
assert.Empty(t, buf.String()) | ||
}) | ||
} | ||
} |
Oops, something went wrong.