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

Pass telemetry directly to the Controller #1248

Merged
merged 8 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 2 additions & 9 deletions internal/pkg/instrumentation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"

"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/otel/trace"

dbSql "go.opentelemetry.io/auto/internal/pkg/instrumentation/bpf/database/sql"
Expand Down Expand Up @@ -58,7 +57,6 @@ type Manager struct {
exe *link.Executable
td *process.TargetDetails
runningProbesWG sync.WaitGroup
telemetryCh chan ptrace.ScopeSpans
currentConfig Config
probeMu sync.Mutex
state managerState
Expand All @@ -74,7 +72,6 @@ func NewManager(logger *slog.Logger, otelController *opentelemetry.Controller, g
globalImpl: globalImpl,
loadedIndicator: loadIndicator,
cp: cp,
telemetryCh: make(chan ptrace.ScopeSpans),
}

err := m.registerProbes()
Expand Down Expand Up @@ -227,7 +224,7 @@ func (m *Manager) runProbe(p probe.Probe) {
m.runningProbesWG.Add(1)
go func(ap probe.Probe) {
defer m.runningProbesWG.Done()
ap.Run(m.telemetryCh)
ap.Run(m.otelController.Trace)
}(p)
}

Expand Down Expand Up @@ -290,19 +287,15 @@ func (m *Manager) Run(ctx context.Context, target *process.TargetDetails) error
m.logger.Debug("Shutting down all probes")
err := m.cleanup(target)

// Wait for all probes to stop before closing the chan they send on.
// Wait for all probes to stop.
m.runningProbesWG.Wait()
close(m.telemetryCh)

m.state = managerStateStopped
m.probeMu.Unlock()

done <- errors.Join(err, ctx.Err())
}()

for e := range m.telemetryCh {
m.otelController.Trace(e)
}
return <-done
}

Expand Down
11 changes: 4 additions & 7 deletions internal/pkg/instrumentation/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ func TestRunStopping(t *testing.T) {
otelController: ctrl,
logger: slog.Default(),
probes: map[probe.ID]probe.Probe{{}: p},
telemetryCh: make(chan ptrace.ScopeSpans),
cp: NewNoopConfigProvider(nil),
}

Expand Down Expand Up @@ -289,7 +288,7 @@ func (p slowProbe) Load(*link.Executable, *process.TargetDetails, *sampling.Conf
return nil
}

func (p slowProbe) Run(c chan<- ptrace.ScopeSpans) {
func (p slowProbe) Run(func(ptrace.ScopeSpans)) {
}

func (p slowProbe) Close() error {
Expand All @@ -309,7 +308,7 @@ func (p *noopProbe) Load(*link.Executable, *process.TargetDetails, *sampling.Con
return nil
}

func (p *noopProbe) Run(c chan<- ptrace.ScopeSpans) {
func (p *noopProbe) Run(func(ptrace.ScopeSpans)) {
p.running.Store(true)
}

Expand Down Expand Up @@ -371,7 +370,6 @@ func TestConfigProvider(t *testing.T) {
netHTTPServerProbeID: &noopProbe{},
somePackageProducerProbeID: &noopProbe{},
},
telemetryCh: make(chan ptrace.ScopeSpans),
cp: newDummyProvider(Config{
InstrumentationLibraryConfigs: map[LibraryID]Library{
netHTTPClientLibID: {TracesEnabled: &falseVal},
Expand Down Expand Up @@ -488,10 +486,10 @@ func (p *hangingProbe) Load(*link.Executable, *process.TargetDetails, *sampling.
return nil
}

func (p *hangingProbe) Run(c chan<- ptrace.ScopeSpans) {
func (p *hangingProbe) Run(handle func(ptrace.ScopeSpans)) {
<-p.closeReturned
// Write after Close has returned.
c <- ptrace.NewScopeSpans()
handle(ptrace.NewScopeSpans())
}

func (p *hangingProbe) Close() error {
Expand All @@ -511,7 +509,6 @@ func TestRunStopDeadlock(t *testing.T) {
otelController: ctrl,
logger: slog.Default(),
probes: map[probe.ID]probe.Probe{{}: p},
telemetryCh: make(chan ptrace.ScopeSpans),
cp: NewNoopConfigProvider(nil),
}

Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/instrumentation/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Probe interface {
Load(*link.Executable, *process.TargetDetails, *sampling.Config) error

// Run runs the events processing loop.
Run(tracesChan chan<- ptrace.ScopeSpans)
Run(func(ptrace.ScopeSpans))
RonFed marked this conversation as resolved.
Show resolved Hide resolved

// Close stops the Probe.
Close() error
Expand Down Expand Up @@ -267,7 +267,7 @@ type SpanProducer[BPFObj any, BPFEvent any] struct {
}

// Run runs the events processing loop.
func (i *SpanProducer[BPFObj, BPFEvent]) Run(dest chan<- ptrace.ScopeSpans) {
func (i *SpanProducer[BPFObj, BPFEvent]) Run(handle func(ptrace.ScopeSpans)) {
for {
event, err := i.read()
if err != nil {
Expand All @@ -285,7 +285,7 @@ func (i *SpanProducer[BPFObj, BPFEvent]) Run(dest chan<- ptrace.ScopeSpans) {

i.ProcessFn(event).CopyTo(ss.Spans())

dest <- ss
handle(ss)
}
}

Expand All @@ -296,7 +296,7 @@ type TraceProducer[BPFObj any, BPFEvent any] struct {
}

// Run runs the events processing loop.
func (i *TraceProducer[BPFObj, BPFEvent]) Run(dest chan<- ptrace.ScopeSpans) {
func (i *TraceProducer[BPFObj, BPFEvent]) Run(handle func(ptrace.ScopeSpans)) {
for {
event, err := i.read()
if err != nil {
Expand All @@ -306,7 +306,7 @@ func (i *TraceProducer[BPFObj, BPFEvent]) Run(dest chan<- ptrace.ScopeSpans) {
continue
}

dest <- i.ProcessFn(event)
handle(i.ProcessFn(event))
}
}

Expand Down
37 changes: 17 additions & 20 deletions internal/pkg/opentelemetry/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,11 @@ import (
type Controller struct {
logger *slog.Logger
tracerProvider trace.TracerProvider
tracersMap map[tracerID]trace.Tracer
}

type tracerID struct{ name, version, schema string }

func (c *Controller) getTracer(name, version, schema string) trace.Tracer {
tID := tracerID{name: name, version: version, schema: schema}
t, exists := c.tracersMap[tID]
if !exists {
t = c.tracerProvider.Tracer(name, trace.WithInstrumentationVersion(version), trace.WithSchemaURL(schema))
c.tracersMap[tID] = t
}
return t
}

// Trace creates a trace span for event.
//
// This method is safe to call concurrently.
func (c *Controller) Trace(ss ptrace.ScopeSpans) {
var (
startOpts []trace.SpanStartOption
Expand All @@ -43,15 +32,20 @@ func (c *Controller) Trace(ss ptrace.ScopeSpans) {
kvs []attribute.KeyValue
)

t := c.getTracer(ss.Scope().Name(), ss.Scope().Version(), ss.SchemaUrl())
tracer := c.tracerProvider.Tracer(
ss.Scope().Name(),
trace.WithInstrumentationVersion(ss.Scope().Version()),
trace.WithInstrumentationAttributes(attrs(ss.Scope().Attributes())...),
trace.WithSchemaURL(ss.SchemaUrl()),
)
for k := 0; k < ss.Spans().Len(); k++ {
pSpan := ss.Spans().At(k)

if pSpan.TraceID().IsEmpty() || pSpan.SpanID().IsEmpty() {
c.logger.Debug("dropping invalid span", "name", pSpan.Name())
continue
}
c.logger.Debug("handling span", "tracer", t, "span", pSpan)
c.logger.Debug("handling span", "tracer", tracer, "span", pSpan)

ctx := context.Background()
if !pSpan.ParentSpanID().IsEmpty() {
Expand All @@ -71,7 +65,7 @@ func (c *Controller) Trace(ss ptrace.ScopeSpans) {
trace.WithTimestamp(pSpan.StartTimestamp().AsTime()),
trace.WithLinks(c.links(pSpan.Links())...),
)
_, span := t.Start(ctx, pSpan.Name(), startOpts...)
_, span := tracer.Start(ctx, pSpan.Name(), startOpts...)
startOpts = startOpts[:0]
kvs = kvs[:0]

Expand All @@ -96,7 +90,6 @@ func NewController(logger *slog.Logger, tracerProvider trace.TracerProvider) (*C
return &Controller{
logger: logger,
tracerProvider: tracerProvider,
tracersMap: make(map[tracerID]trace.Tracer),
}, nil
}

Expand All @@ -113,6 +106,11 @@ func (c *Controller) Shutdown(ctx context.Context) error {
return nil
}

func attrs(m pcommon.Map) []attribute.KeyValue {
out := make([]attribute.KeyValue, 0, m.Len())
return appendAttrs(out, m)
}

func appendAttrs(dest []attribute.KeyValue, m pcommon.Map) []attribute.KeyValue {
m.Range(func(k string, v pcommon.Value) bool {
dest = append(dest, attr(k, v))
Expand Down Expand Up @@ -213,8 +211,7 @@ func appendEventOpts(dest []trace.EventOption, e ptrace.SpanEvent) []trace.Event
dest = append(dest, trace.WithTimestamp(ts))
}

var kvs []attribute.KeyValue
kvs = appendAttrs(kvs, e.Attributes())
kvs := attrs(e.Attributes())
if len(kvs) > 0 {
dest = append(dest, trace.WithAttributes(kvs...))
}
Expand Down Expand Up @@ -244,8 +241,8 @@ func (c *Controller) links(links ptrace.SpanLinkSlice) []trace.Link {
TraceFlags: trace.TraceFlags(l.Flags()),
TraceState: ts,
}),
Attributes: attrs(l.Attributes()),
}
out[i].Attributes = appendAttrs(out[i].Attributes, l.Attributes())
}
return out
}
Expand Down
65 changes: 36 additions & 29 deletions internal/pkg/opentelemetry/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -314,35 +315,6 @@ func TestTrace(t *testing.T) {
}
}

func TestGetTracer(t *testing.T) {
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(instResource()),
)
defer func() {
err := tp.Shutdown(context.Background())
assert.NoError(t, err)
}()

ctrl, err := NewController(slog.Default(), tp)
assert.NoError(t, err)

t1 := ctrl.getTracer("test", "v1", "schema")
assert.Equal(t, t1, ctrl.tracersMap[tracerID{name: "test", version: "v1", schema: "schema"}])

t2 := ctrl.getTracer("net/http", "", "")
assert.Equal(t, t2, ctrl.tracersMap[tracerID{name: "net/http", version: "", schema: ""}])

t3 := ctrl.getTracer("test", "v1", "schema")
assert.Same(t, t1, t3)

t4 := ctrl.getTracer("net/http", "", "")
assert.Same(t, t2, t4)
assert.Equal(t, len(ctrl.tracersMap), 2)
}

type shutdownExporter struct {
sdktrace.SpanExporter

Expand Down Expand Up @@ -390,3 +362,38 @@ func TestShutdown(t *testing.T) {
assert.True(t, exporter.called, "Exporter not shutdown")
assert.Equal(t, uint32(nSpan), exporter.exported.Load(), "Pending spans not flushed")
}

func TestControllerTraceConcurrentSafe(t *testing.T) {
exporter := tracetest.NewInMemoryExporter()
RonFed marked this conversation as resolved.
Show resolved Hide resolved
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(instResource()),
)
defer func() {
err := tp.Shutdown(context.Background())
assert.NoError(t, err)
}()

ctrl, err := NewController(slog.Default(), tp)
assert.NoError(t, err)

const goroutines = 10

var wg sync.WaitGroup
for n := 0; n < goroutines; n++ {
wg.Add(1)
go func() {
defer wg.Done()

data := ptrace.NewScopeSpans()
data.Scope().SetName(fmt.Sprintf("tracer-%d", n%(goroutines/2)))
data.Scope().SetVersion("v1")
data.SetSchemaUrl("url")
data.Spans().AppendEmpty().SetName("test")
ctrl.Trace(data)
}()
}

wg.Wait()
}
Loading