Skip to content

Commit

Permalink
Merge pull request #79 from w-h-a/otel-protocol-traces
Browse files Browse the repository at this point in the history
refactor: prep for otel protocol exporter
  • Loading branch information
w-h-a authored Oct 21, 2024
2 parents 36759b9 + 7d95932 commit b7ce056
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tracev2
package traceexporter

import (
"time"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ package memory
import (
"context"

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

type memoryExporter struct {
buffer *memoryutils.Buffer
options traceexporter.ExporterOptions
}

func (e *memoryExporter) Options() traceexporter.ExporterOptions {
return e.options
}

func (e *memoryExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
spanData := []*tracev2.SpanData{}
spanData := []*traceexporter.SpanData{}

for _, s := range spans {
var parentSpanId trace.SpanID
Expand All @@ -33,12 +37,12 @@ func (e *memoryExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadO
metadata[string(attr.Key)] = attr.Value.AsString()
}

status := tracev2.Status{
status := traceexporter.Status{
Code: uint32(s.Status().Code),
Description: s.Status().Description,
}

data := &tracev2.SpanData{
data := &traceexporter.SpanData{
Name: s.Name(),
Id: s.SpanContext().SpanID().String(),
Parent: parentSpanId.String(),
Expand All @@ -53,17 +57,31 @@ func (e *memoryExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadO
}

for _, d := range spanData {
e.buffer.Put(d)
e.options.Buffer.Put(d)
}

return nil
}

// TODO?
// TODO: ?
func (e *memoryExporter) Shutdown(ctx context.Context) error {
return nil
}

func NewExporter(buffer *memoryutils.Buffer) sdktrace.SpanExporter {
return &memoryExporter{buffer}
func (e *memoryExporter) String() string {
return "memory"
}

func NewExporter(opts ...traceexporter.ExporterOption) sdktrace.SpanExporter {
options := traceexporter.NewExporterOptions(opts...)

if options.Buffer == nil {
log.Fatalf("no buffer was given")
}

e := &memoryExporter{
options: options,
}

return e
}
1 change: 1 addition & 0 deletions telemetry/traceexporter/memory/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package memory
32 changes: 32 additions & 0 deletions telemetry/traceexporter/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package traceexporter

import (
"context"

"github.com/w-h-a/pkg/utils/memoryutils"
)

type ExporterOption func(o *ExporterOptions)

type ExporterOptions struct {
Buffer *memoryutils.Buffer
Context context.Context
}

func ExporterWithBuffer(b *memoryutils.Buffer) ExporterOption {
return func(o *ExporterOptions) {
o.Buffer = b
}
}

func NewExporterOptions(opts ...ExporterOption) ExporterOptions {
options := ExporterOptions{
Context: context.Background(),
}

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

return options
}
1 change: 1 addition & 0 deletions telemetry/traceexporter/otelp/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package otelp
1 change: 1 addition & 0 deletions telemetry/traceexporter/otelp/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package otelp
21 changes: 0 additions & 21 deletions telemetry/tracev2/memory/options.go

This file was deleted.

25 changes: 0 additions & 25 deletions telemetry/tracev2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,3 @@ func NewTraceOptions(opts ...TraceOption) TraceOptions {

return options
}

type ReadOption func(o *ReadOptions)

type ReadOptions struct {
Count int
Context context.Context
}

func ReadWithCount(c int) ReadOption {
return func(o *ReadOptions) {
o.Count = c
}
}

func NewReadOptions(opts ...ReadOption) ReadOptions {
options := ReadOptions{
Context: context.Background(),
}

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

return options
}
1 change: 1 addition & 0 deletions telemetry/tracev2/otel/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package otel
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
package memory
package otel

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/codes"
"go.opentelemetry.io/otel/trace"
)

type memoryTrace struct {
type otelTrace struct {
options tracev2.TraceOptions
tracer trace.Tracer
spans map[string]trace.Span
buffer *memoryutils.Buffer
mtx sync.RWMutex
}

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

func (t *memoryTrace) Start(ctx context.Context, name string) (context.Context, string) {
func (t *otelTrace) Start(ctx context.Context, name string) (context.Context, string) {
t.mtx.Lock()
defer t.mtx.Unlock()

Expand All @@ -52,7 +49,7 @@ func (t *memoryTrace) Start(ctx context.Context, name string) (context.Context,
return newCtx, key
}

func (t *memoryTrace) AddMetadata(span string, md map[string]string) {
func (t *otelTrace) AddMetadata(span string, md map[string]string) {
t.mtx.Lock()
defer t.mtx.Unlock()

Expand All @@ -77,7 +74,7 @@ func (t *memoryTrace) AddMetadata(span string, md map[string]string) {
t.spans[span].SetAttributes(attrs...)
}

func (t *memoryTrace) UpdateStatus(span string, code uint32, description string) {
func (t *otelTrace) UpdateStatus(span string, code uint32, description string) {
t.mtx.Lock()
defer t.mtx.Unlock()

Expand All @@ -90,7 +87,7 @@ func (t *memoryTrace) UpdateStatus(span string, code uint32, description string)
}
}

func (t *memoryTrace) Finish(span string) {
func (t *otelTrace) Finish(span string) {
t.mtx.Lock()
defer t.mtx.Unlock()

Expand All @@ -99,33 +96,11 @@ func (t *memoryTrace) Finish(span string) {
delete(t.spans, span)
}

func (t *memoryTrace) Read(opts ...tracev2.ReadOption) ([]*tracev2.SpanData, error) {
options := tracev2.NewReadOptions(opts...)

var entries []*memoryutils.Entry

if options.Count > 0 {
entries = t.buffer.Get(options.Count)
} else {
entries = t.buffer.Get(t.buffer.Options().Size)
}

spans := []*tracev2.SpanData{}

for _, entry := range entries {
span := entry.Value.(*tracev2.SpanData)

spans = append(spans, span)
}

return spans, nil
}

func (t *memoryTrace) String() string {
return "memory"
func (t *otelTrace) String() string {
return "otel"
}

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

ctx = trace.ContextWithRemoteSpanContext(ctx, parentSpanCtx)
Expand All @@ -136,18 +111,12 @@ func (t *memoryTrace) start(ctx context.Context, name string, parentCtxCfg trace
func NewTrace(opts ...tracev2.TraceOption) tracev2.Trace {
options := tracev2.NewTraceOptions(opts...)

t := &memoryTrace{
t := &otelTrace{
options: options,
tracer: otel.Tracer(options.Name),
spans: map[string]trace.Span{},
mtx: sync.RWMutex{},
}

if b, ok := GetBufferFromContext(options.Context); ok && b != nil {
t.buffer = b
} else {
log.Fatalf("no buffer was given")
}

return t
}
1 change: 0 additions & 1 deletion telemetry/tracev2/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ type Trace interface {
AddMetadata(span string, md map[string]string)
UpdateStatus(span string, code uint32, description string)
Finish(span string)
Read(opts ...ReadOption) ([]*SpanData, error)
String() string
}

0 comments on commit b7ce056

Please sign in to comment.