Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(flagd): custom error handling for OTel errors #769

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/pkg/runtime/from_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func FromConfig(logger *logger.Logger, version string, config Config) (*Runtime,
CollectorTarget: config.OtelCollectorURI,
}

// register error handling for OpenTelemetry
telemetry.RegisterErrorHandling(logger)

// register trace provider for the runtime
err := telemetry.BuildTraceProvider(context.Background(), logger, svcName, version, telCfg)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions core/pkg/telemetry/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
Expand All @@ -33,6 +34,12 @@ type Config struct {
CollectorTarget string
}

func RegisterErrorHandling(log *logger.Logger) {
otel.SetErrorHandler(otelErrorsHandler{
logger: log,
})
}

// BuildMetricsRecorder is a helper to build telemetry.MetricsRecorder based on configurations
func BuildMetricsRecorder(
ctx context.Context, svcName string, svcVersion string, config Config,
Expand Down Expand Up @@ -172,3 +179,13 @@ func buildResourceFor(ctx context.Context, serviceName string, serviceVersion st
}
return r, nil
}

// OTelErrorsHandler is a custom error interceptor for OpenTelemetry
type otelErrorsHandler struct {
logger *logger.Logger
}

func (h otelErrorsHandler) Handle(err error) {
msg := fmt.Sprintf("OpenTelemetry Error: %s", err.Error())
h.logger.WithFields(zap.String("component", "otel")).Debug(msg)
}
50 changes: 50 additions & 0 deletions core/pkg/telemetry/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ package telemetry

import (
"context"
"fmt"
"testing"
"time"

"github.com/open-feature/flagd/core/pkg/logger"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
)

func TestBuildMetricsRecorder(t *testing.T) {
Expand Down Expand Up @@ -151,3 +159,45 @@ func TestBuildResourceFor(t *testing.T) {
Value: attribute.StringValue(svcVersion),
}, "expected resource to contain service version")
}

func TestErrorIntercepted(t *testing.T) {
// register the OTel error handling
observedZapCore, observedLogs := observer.New(zap.DebugLevel)
observedLogger := zap.New(observedZapCore)
log := logger.NewLogger(observedLogger, true)
RegisterErrorHandling(log)

// configure a metric reader with an exporter that only returns error
reader := metric.NewPeriodicReader(&errorExp{}, metric.WithInterval(1*time.Millisecond))
rs := resource.NewWithAttributes("testSchema")
NewOTelRecorder(reader, rs, "testSvc")
var data metricdata.ResourceMetrics
err := reader.Collect(context.TODO(), &data)
require.Nil(t, err)

// we should have some logs that were intercepted
require.True(t, observedLogs.FilterField(zap.String("component", "otel")).Len() > 0)
}

// errorExp is an exporter that always fails
type errorExp struct{}

func (e *errorExp) Temporality(k metric.InstrumentKind) metricdata.Temporality {
return metric.DefaultTemporalitySelector(k)
}

func (e *errorExp) Aggregation(_ metric.InstrumentKind) aggregation.Aggregation {
return nil
}

func (e *errorExp) Export(_ context.Context, _ *metricdata.ResourceMetrics) error {
return fmt.Errorf("I am an error")
}

func (e *errorExp) ForceFlush(_ context.Context) error {
return fmt.Errorf("I am an error")
}

func (e *errorExp) Shutdown(_ context.Context) error {
return fmt.Errorf("I am an error")
}