Skip to content

Commit

Permalink
Expose OpenTelemetryController as interface option to Instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
damemi committed Aug 16, 2024
1 parent 8854dcc commit 5691e3f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
20 changes: 17 additions & 3 deletions instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"go.opentelemetry.io/auto/internal/pkg/instrumentation"
"go.opentelemetry.io/auto/internal/pkg/opentelemetry"
"go.opentelemetry.io/auto/internal/pkg/process"
otelauto "go.opentelemetry.io/auto/pkg/opentelemetry"
)

const (
Expand Down Expand Up @@ -125,9 +126,12 @@ func NewInstrumentation(ctx context.Context, opts ...InstrumentationOption) (*In
return nil, err
}

ctrl, err := opentelemetry.NewController(logger, c.tracerProvider(pa.BuildInfo), Version())
if err != nil {
return nil, err
ctrl := c.otelController
if ctrl == nil {
ctrl, err = opentelemetry.NewController(logger, c.tracerProvider(pa.BuildInfo), Version())
if err != nil {
return nil, err
}
}

mngr, err := instrumentation.NewManager(logger, ctrl, c.globalImpl, c.loadIndicator)
Expand Down Expand Up @@ -221,6 +225,7 @@ type instConfig struct {
globalImpl bool
loadIndicator chan struct{}
logLevel LogLevel
otelController otelauto.OpenTelemetryController
}

func newInstConfig(ctx context.Context, opts []InstrumentationOption) (instConfig, error) {
Expand Down Expand Up @@ -331,6 +336,15 @@ type fnOpt func(context.Context, instConfig) (instConfig, error)

func (o fnOpt) apply(ctx context.Context, c instConfig) (instConfig, error) { return o(ctx, c) }

// WithOpenTelemetryController returns an [InstrumentationOption] defining the
// OpenTelemetryController used by [Instrumentation].
func WithOpenTelemetryController(ctrl otelauto.OpenTelemetryController) InstrumentationOption {
return fnOpt(func(_ context.Context, c instConfig) (instConfig, error) {
c.otelController = ctrl
return c, nil
})
}

// WithTarget returns an [InstrumentationOption] defining the target binary for
// [Instrumentation] that is being executed at the provided path.
//
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/instrumentation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
httpServer "go.opentelemetry.io/auto/internal/pkg/instrumentation/bpf/net/http/server"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/bpffs"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/opentelemetry"
"go.opentelemetry.io/auto/internal/pkg/process"
otelauto "go.opentelemetry.io/auto/pkg/opentelemetry"
)

// Function variables overridden in testing.
Expand All @@ -39,13 +39,13 @@ var (
type Manager struct {
logger logr.Logger
probes map[probe.ID]probe.Probe
otelController *opentelemetry.Controller
otelController otelauto.OpenTelemetryController
globalImpl bool
loadedIndicator chan struct{}
}

// NewManager returns a new [Manager].
func NewManager(logger logr.Logger, otelController *opentelemetry.Controller, globalImpl bool, loadIndicator chan struct{}) (*Manager, error) {
func NewManager(logger logr.Logger, otelController otelauto.OpenTelemetryController, globalImpl bool, loadIndicator chan struct{}) (*Manager, error) {
logger = logger.WithName("Manager")
m := &Manager{
logger: logger,
Expand Down
16 changes: 8 additions & 8 deletions internal/pkg/opentelemetry/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
)

// Controller handles OpenTelemetry telemetry generation for events.
type Controller struct {
// ControllerImpl handles OpenTelemetry telemetry generation for events.
type ControllerImpl struct {
logger logr.Logger
version string
tracerProvider trace.TracerProvider
Expand All @@ -25,7 +25,7 @@ type Controller struct {

type tracerID struct{ name, version, schema string }

func (c *Controller) getTracer(pkg, tracerName, version, schema string) trace.Tracer {
func (c *ControllerImpl) getTracer(pkg, tracerName, version, schema string) trace.Tracer {
// Default Tracer ID, if the user does not provide one.
tID := tracerID{name: pkg, version: c.version}
if tracerName != "" {
Expand Down Expand Up @@ -54,7 +54,7 @@ func (c *Controller) getTracer(pkg, tracerName, version, schema string) trace.Tr
}

// Trace creates a trace span for event.
func (c *Controller) Trace(event *probe.Event) {
func (c *ControllerImpl) Trace(event *probe.Event) {
for _, se := range event.SpanEvents {
c.logger.V(1).Info("got event", "kind", event.Kind.String(), "pkg", event.Package, "attrs", se.Attributes, "traceID", se.SpanContext.TraceID().String(), "spanID", se.SpanContext.SpanID().String())
ctx := context.Background()
Expand All @@ -81,20 +81,20 @@ func (c *Controller) Trace(event *probe.Event) {
}
}

func (c *Controller) convertTime(t int64) time.Time {
func (c *ControllerImpl) convertTime(t int64) time.Time {
return time.Unix(0, c.bootTime+t)
}

// NewController returns a new initialized [Controller].
func NewController(logger logr.Logger, tracerProvider trace.TracerProvider, ver string) (*Controller, error) {
// NewController returns a new initialized [ControllerImpl].
func NewController(logger logr.Logger, tracerProvider trace.TracerProvider, ver string) (*ControllerImpl, error) {
logger = logger.WithName("Controller")

bt, err := utils.EstimateBootTimeOffset()
if err != nil {
return nil, err
}

return &Controller{
return &ControllerImpl{
logger: logger,
version: ver,
tracerProvider: tracerProvider,
Expand Down
10 changes: 10 additions & 0 deletions pkg/opentelemetry/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package opentelemetry

import "go.opentelemetry.io/auto/pkg/probe"

type OpenTelemetryController interface {

Check failure on line 8 in pkg/opentelemetry/controller.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type OpenTelemetryController should have comment or be unexported (revive)
Trace(event *probe.Event)
}
8 changes: 8 additions & 0 deletions pkg/probe/probe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package probe

import "go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"

type Event = probe.Event

0 comments on commit 5691e3f

Please sign in to comment.