Skip to content

Commit

Permalink
fix: bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
w-h-a committed Oct 13, 2024
1 parent 5f36a7d commit 6248be4
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 202 deletions.
15 changes: 5 additions & 10 deletions sidecar/custom/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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/tracev2"
"github.com/w-h-a/pkg/utils/datautils"
"golang.org/x/text/cases"
"golang.org/x/text/language"
Expand Down Expand Up @@ -185,13 +184,9 @@ func (s *customSidecar) UnsubscribeFromBroker(brokerId string) error {
func (s *customSidecar) ReadFromSecretStore(ctx context.Context, secretStore string, name string) (*sidecar.Secret, error) {
// TODO: make sure we have a tracer

_, span, err := s.options.Tracer.Start(ctx, "customSidecar.ReadFromSecretStore")
if err != nil {
log.Errorf("failed to start span: %v", err)
return nil, tracev2.ErrStart
}
_ = s.options.Tracer.Start(ctx, "customSidecar.ReadFromSecretStore")

s.options.Tracer.AddMetadata(span, map[string]string{
s.options.Tracer.AddMetadata(map[string]string{
"secretStore": secretStore,
"name": name,
})
Expand All @@ -200,20 +195,20 @@ func (s *customSidecar) ReadFromSecretStore(ctx context.Context, secretStore str
if !ok {
log.Warnf("secret store %s was not found", secretStore)
// TODO: update status of span
s.options.Tracer.Finish(span)
s.options.Tracer.Finish()
return nil, sidecar.ErrComponentNotFound
}

mp, err := sc.GetSecret(name)
if err != nil {
// TODO: update status of span
s.options.Tracer.Finish(span)
s.options.Tracer.Finish()
return nil, err
}

// TODO: update status of span

s.options.Tracer.Finish(span)
s.options.Tracer.Finish()

return &sidecar.Secret{
Data: mp,
Expand Down
16 changes: 1 addition & 15 deletions telemetry/tracev2/memory/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,17 @@ import (

"github.com/w-h-a/pkg/telemetry/tracev2"
"github.com/w-h-a/pkg/utils/memoryutils"
"go.opentelemetry.io/otel/trace"
)

type bufferKey struct{}

func TraceWithBuffer(b *memoryutils.Buffer) tracev2.TraceOption {
return func(o *tracev2.TraceOptions) {
o.Context = context.WithValue(o.Context, b, bufferKey{})
o.Context = context.WithValue(o.Context, bufferKey{}, b)
}
}

func GetBufferFromContext(ctx context.Context) (*memoryutils.Buffer, bool) {
b, ok := ctx.Value(bufferKey{}).(*memoryutils.Buffer)
return b, ok
}

type spanKey struct{}

func SpanWithSpan(s trace.Span) tracev2.SpanOption {
return func(o *tracev2.SpanOptions) {
o.Context = context.WithValue(o.Context, s, spanKey{})
}
}

func GetSpanFromContext(ctx context.Context) (trace.Span, bool) {
s, ok := ctx.Value(spanKey{}).(trace.Span)
return s, ok
}
58 changes: 0 additions & 58 deletions telemetry/tracev2/memory/span.go

This file was deleted.

87 changes: 46 additions & 41 deletions telemetry/tracev2/memory/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,83 @@ package memory

import (
"context"
"sync"

"github.com/w-h-a/pkg/telemetry/log"
"github.com/w-h-a/pkg/telemetry/tracev2"
"github.com/w-h-a/pkg/utils/memoryutils"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

type memoryTrace struct {
options tracev2.TraceOptions
tracer trace.Tracer
span trace.Span
buffer *memoryutils.Buffer
mtx sync.RWMutex
}

func (t *memoryTrace) Options() tracev2.TraceOptions {
return t.options
}

func (t *memoryTrace) Start(ctx context.Context, name string) (context.Context, tracev2.Span, error) {
func (t *memoryTrace) Start(ctx context.Context, name string) context.Context {
// TODO: make sure we're enabled

t.mtx.Lock()
defer t.mtx.Unlock()

parentCtxCfg := trace.SpanContextConfig{}

parentData, err := tracev2.SpanDataFromContext(ctx)
if err != nil {
newCtx, memorySpan := t.start(ctx, name, parentCtxCfg)
return newCtx, memorySpan, err
if t.span == nil {
newCtx, span := t.start(ctx, name, parentCtxCfg)
t.span = span
return newCtx
}

parentCtxCfg.TraceID, err = trace.TraceIDFromHex(parentData.Trace)
if err != nil {
newCtx, memorySpan := t.start(ctx, name, parentCtxCfg)
return newCtx, memorySpan, err
}
parentCtxCfg.TraceID = t.span.SpanContext().TraceID()

parentCtxCfg.SpanID, err = trace.SpanIDFromHex(parentData.Id)
if err != nil {
newCtx, memorySpan := t.start(ctx, name, parentCtxCfg)
return newCtx, memorySpan, err
}
parentCtxCfg.SpanID = t.span.SpanContext().SpanID()

newCtx, memorySpan := t.start(ctx, name, parentCtxCfg)
newCtx, span := t.start(ctx, name, parentCtxCfg)

return newCtx, memorySpan, nil
t.span = span

return newCtx
}

func (t *memoryTrace) AddMetadata(span tracev2.Span, md map[string]string) {
if span == nil {
func (t *memoryTrace) AddMetadata(md map[string]string) {
t.mtx.Lock()
defer t.mtx.Unlock()

if t.span == nil {
return
}

if len(md) > 0 {
span.AddMetadata(md)
if len(md) == 0 {
return
}

attrs := []attribute.KeyValue{}

for k, v := range md {
attrs = append(attrs, attribute.String(k, v))
}

if len(attrs) == 0 {
return
}

t.span.SetAttributes(attrs...)
}

func (t *memoryTrace) Finish(span tracev2.Span) {
span.Finish()
func (t *memoryTrace) Finish() {
t.mtx.Lock()
defer t.mtx.Unlock()

t.span.End()
}

func (t *memoryTrace) Read(opts ...tracev2.ReadOption) ([]*tracev2.SpanData, error) {
Expand Down Expand Up @@ -88,21 +107,12 @@ func (t *memoryTrace) String() string {
return "memory"
}

func (t *memoryTrace) start(ctx context.Context, name string, parentCtxCfg trace.SpanContextConfig) (context.Context, tracev2.Span) {
func (t *memoryTrace) start(ctx context.Context, name string, parentCtxCfg trace.SpanContextConfig) (context.Context, trace.Span) {
parentSpanCtx := trace.NewSpanContext(parentCtxCfg)

ctx = trace.ContextWithRemoteSpanContext(ctx, parentSpanCtx)

newCtx, span := t.tracer.Start(ctx, name)

opts := []tracev2.SpanOption{
tracev2.SpanWithName(name),
SpanWithSpan(span),
}

memorySpan := NewSpan(opts...)

return newCtx, memorySpan
return t.tracer.Start(ctx, name)
}

func NewTrace(opts ...tracev2.TraceOption) tracev2.Trace {
Expand All @@ -111,15 +121,10 @@ func NewTrace(opts ...tracev2.TraceOption) tracev2.Trace {
t := &memoryTrace{
options: options,
tracer: otel.Tracer(options.Name),
mtx: sync.RWMutex{},
}

b, ok := GetBufferFromContext(options.Context)

log.Infof("MY CTX %+#v", options.Context)

log.Infof("MY buffer %+#v", b)

if ok && b != nil {
if b, ok := GetBufferFromContext(options.Context); ok && b != nil {
t.buffer = b
} else {
log.Fatalf("no buffer was given")
Expand Down
25 changes: 0 additions & 25 deletions telemetry/tracev2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,6 @@ func NewTraceOptions(opts ...TraceOption) TraceOptions {
return options
}

type SpanOption func(o *SpanOptions)

type SpanOptions struct {
Name string
Context context.Context
}

func SpanWithName(name string) SpanOption {
return func(o *SpanOptions) {
o.Name = name
}
}

func NewSpanOptions(opts ...SpanOption) SpanOptions {
options := SpanOptions{
Context: context.Background(),
}

for _, fn := range opts {
fn(&options)
}

return options
}

type ReadOption func(o *ReadOptions)

type ReadOptions struct {
Expand Down
9 changes: 0 additions & 9 deletions telemetry/tracev2/span.go

This file was deleted.

11 changes: 3 additions & 8 deletions telemetry/tracev2/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ package tracev2

import (
"context"
"errors"
)

var (
ErrStart = errors.New("failed to start span")
)

// TODO: status
type Trace interface {
Options() TraceOptions
// TODO: add cfg argument to check for whether tracing is enabled
Start(ctx context.Context, name string) (context.Context, Span, error)
AddMetadata(span Span, md map[string]string)
Finish(span Span)
Start(ctx context.Context, name string) context.Context
AddMetadata(md map[string]string)
Finish()
Read(opts ...ReadOption) ([]*SpanData, error)
String() string
}
36 changes: 0 additions & 36 deletions telemetry/tracev2/utils.go

This file was deleted.

0 comments on commit 6248be4

Please sign in to comment.