Skip to content

Commit

Permalink
feat(trace): Updated otlp-grpc connection constructor to have default…
Browse files Browse the repository at this point in the history
… values (#188)
  • Loading branch information
ekkinox authored Mar 27, 2024
1 parent ccb88ee commit c964798
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
18 changes: 8 additions & 10 deletions trace/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ import (
)

// DefaultOtlpGrpcTimeout is the default timeout in seconds for the OTLP gRPC connection.
const DefaultOtlpGrpcTimeout = 5
const DefaultOtlpGrpcTimeout = 30

// NewOtlpGrpcClientConnection returns a gRPC connection, and accept a host and a list of [DialOption]
//
// [DialOption]: https://github.com/grpc/grpc-go
// NewOtlpGrpcClientConnection returns a gRPC connection, and accept a host and a list of [grpc.DialOption].
func NewOtlpGrpcClientConnection(ctx context.Context, host string, dialOptions ...grpc.DialOption) (*grpc.ClientConn, error) {
dialCtx, cancel := context.WithTimeout(ctx, DefaultOtlpGrpcTimeout*time.Second)
defer cancel()

if len(dialOptions) == 0 {
dialOptions = []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
}
dialContextOptions := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
}

return grpc.DialContext(dialCtx, host, dialOptions...)
dialContextOptions = append(dialContextOptions, dialOptions...)

return grpc.DialContext(dialCtx, host, dialContextOptions...)
}
3 changes: 0 additions & 3 deletions trace/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
)

Expand All @@ -35,8 +34,6 @@ func TestNewOtlpGrpcInsecureConnectionSuccess(t *testing.T) {
context.Background(),
"bufnet",
grpc.WithContextDialer(bufDialer),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
)
assert.NoError(t, err)

Expand Down
16 changes: 4 additions & 12 deletions trace/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,24 @@ const (
Noop = "noop" // processor to void the trace spans
)

// NewTestSpanProcessor returns a [OTEL SpanProcessor] using a sync [tracetest.TestTraceExporter].
//
// [OTEL SpanProcessor]: https://github.com/open-telemetry/opentelemetry-go
// NewTestSpanProcessor returns a [trace.SpanProcessor] using a sync [tracetest.TestTraceExporter].
func NewTestSpanProcessor(testTraceExporter tracetest.TestTraceExporter) trace.SpanProcessor {
return trace.NewSimpleSpanProcessor(testTraceExporter)
}

// NewNoopSpanProcessor returns a [OTEL SpanProcessor] that voids trace spans via an async [otelsdktracetest.NoopExporter].
//
// [OTEL SpanProcessor]: https://github.com/open-telemetry/opentelemetry-go
// NewNoopSpanProcessor returns a [trace.SpanProcessor] that voids trace spans via an async [otelsdktracetest.NoopExporter].
func NewNoopSpanProcessor() trace.SpanProcessor {
return trace.NewBatchSpanProcessor(otelsdktracetest.NewNoopExporter())
}

// NewTestSpanProcessor returns a [OTEL SpanProcessor] using an async [stdouttrace.Exporter].
//
// [OTEL SpanProcessor]: https://github.com/open-telemetry/opentelemetry-go
// NewStdoutSpanProcessor returns a [trace.SpanProcessor] using an async [stdouttrace.Exporter].
func NewStdoutSpanProcessor(options ...stdouttrace.Option) trace.SpanProcessor {
exporter, _ := stdouttrace.New(options...)

return trace.NewBatchSpanProcessor(exporter)
}

// NewOtlpGrpcSpanProcessor returns a [OTEL SpanProcessor] using an async [otlptracegrpc.Exporter].
//
// [OTEL SpanProcessor]: https://github.com/open-telemetry/opentelemetry-go
// NewOtlpGrpcSpanProcessor returns a [trace.SpanProcessor] using an async [otlptracegrpc.Exporter].
func NewOtlpGrpcSpanProcessor(ctx context.Context, conn *grpc.ClientConn) (trace.SpanProcessor, error) {
exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
if err != nil {
Expand Down

0 comments on commit c964798

Please sign in to comment.