From ded195ccdcac7f99665c34137347d8404a560ebc Mon Sep 17 00:00:00 2001 From: w-h-a Date: Fri, 11 Oct 2024 15:47:52 -0700 Subject: [PATCH] refactor: reading from secret store should take ctx --- sidecar/custom/sidecar.go | 30 +++++++++++++++++++++++++++++- sidecar/sidecar.go | 3 ++- telemetry/trace/domain.go | 4 +++- telemetry/trace/trace.go | 10 +++++++++- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/sidecar/custom/sidecar.go b/sidecar/custom/sidecar.go index 4e4a291..6e822ae 100644 --- a/sidecar/custom/sidecar.go +++ b/sidecar/custom/sidecar.go @@ -13,6 +13,7 @@ import ( "github.com/w-h-a/pkg/sidecar" "github.com/w-h-a/pkg/store" "github.com/w-h-a/pkg/telemetry/log" + "github.com/w-h-a/pkg/telemetry/trace" "github.com/w-h-a/pkg/utils/datautils" "golang.org/x/text/cases" "golang.org/x/text/language" @@ -181,18 +182,45 @@ func (s *customSidecar) UnsubscribeFromBroker(brokerId string) error { return nil } -func (s *customSidecar) ReadFromSecretStore(secretStore string, name string) (*sidecar.Secret, error) { +func (s *customSidecar) ReadFromSecretStore(ctx context.Context, secretStore string, name string) (*sidecar.Secret, error) { + tracer := trace.GetTracer() + + if tracer == nil { + log.Error("failed to get tracer") + return nil, trace.ErrNotFound + } + + _, span, err := tracer.Start( + ctx, + "customSidecar.ReadFromSecretStore", + map[string]string{ + "secretStore": secretStore, + "name": name, + "error": "", + }, + ) + if err != nil { + log.Errorf("failed to start span: %v", err) + return nil, trace.ErrStart + } + sc, ok := s.options.Secrets[secretStore] if !ok { log.Warnf("secret store %s was not found", secretStore) + span.Metadata["error"] = fmt.Sprintf("secret store %s was not found", secretStore) + tracer.Finish(span) return nil, sidecar.ErrComponentNotFound } mp, err := sc.GetSecret(name) if err != nil { + span.Metadata["error"] = err.Error() + tracer.Finish(span) return nil, err } + tracer.Finish(span) + return &sidecar.Secret{ Data: mp, }, nil diff --git a/sidecar/sidecar.go b/sidecar/sidecar.go index fe6dea9..1d1bf68 100644 --- a/sidecar/sidecar.go +++ b/sidecar/sidecar.go @@ -1,6 +1,7 @@ package sidecar import ( + "context" "errors" "github.com/w-h-a/pkg/store" @@ -20,6 +21,6 @@ type Sidecar interface { WriteEventToBroker(event *Event) error ReadEventsFromBroker(broker string) UnsubscribeFromBroker(broker string) error - ReadFromSecretStore(secretStore string, name string) (*Secret, error) + ReadFromSecretStore(ctx context.Context, secretStore string, name string) (*Secret, error) String() string } diff --git a/telemetry/trace/domain.go b/telemetry/trace/domain.go index 1425747..58a0895 100644 --- a/telemetry/trace/domain.go +++ b/telemetry/trace/domain.go @@ -1,6 +1,8 @@ package trace -import "time" +import ( + "time" +) type Span struct { Name string `json:"name"` diff --git a/telemetry/trace/trace.go b/telemetry/trace/trace.go index b094da9..e84a549 100644 --- a/telemetry/trace/trace.go +++ b/telemetry/trace/trace.go @@ -1,6 +1,14 @@ package trace -import "context" +import ( + "context" + "errors" +) + +var ( + ErrNotFound = errors.New("not found") + ErrStart = errors.New("failed to start span") +) var tracer Trace