Skip to content

Commit

Permalink
refactor: reading from secret store should take ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
w-h-a committed Oct 11, 2024
1 parent 6d9006a commit ded195c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
30 changes: 29 additions & 1 deletion sidecar/custom/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion sidecar/sidecar.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sidecar

import (
"context"
"errors"

"github.com/w-h-a/pkg/store"
Expand All @@ -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
}
4 changes: 3 additions & 1 deletion telemetry/trace/domain.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package trace

import "time"
import (
"time"
)

type Span struct {
Name string `json:"name"`
Expand Down
10 changes: 9 additions & 1 deletion telemetry/trace/trace.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit ded195c

Please sign in to comment.