-
Notifications
You must be signed in to change notification settings - Fork 93
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
Add WithServiceName and WithPid as config options for instrumentation… #346
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,9 +25,13 @@ import ( | |
"go.opentelemetry.io/auto/internal/pkg/process" | ||
) | ||
|
||
// envTargetExeKey is the key for the environment variable value pointing to the | ||
// target binary to instrument. | ||
const envTargetExeKey = "OTEL_GO_AUTO_TARGET_EXE" | ||
const ( | ||
// envTargetExeKey is the key for the environment variable value pointing to the | ||
// target binary to instrument. | ||
envTargetExeKey = "OTEL_GO_AUTO_TARGET_EXE" | ||
// envServiceName is the key for the envoriment variable value containing the service name. | ||
envServiceNameKey = "OTEL_SERVICE_NAME" | ||
) | ||
|
||
// Instrumentation manages and controls all OpenTelemetry Go | ||
// auto-instrumentation. | ||
|
@@ -41,6 +45,8 @@ var ( | |
// Error message returned when instrumentation is launched without a target | ||
// binary. | ||
errUndefinedTarget = fmt.Errorf("undefined target Go binary, consider setting the %s environment variable pointing to the target binary to instrument", envTargetExeKey) | ||
// Error message returned when no service name is specified. | ||
errUndefinedServiceName = fmt.Errorf("undefined service name, consider setting %s", envServiceNameKey) | ||
) | ||
|
||
// NewInstrumentation returns a new [Instrumentation] configured with the | ||
|
@@ -57,7 +63,7 @@ func NewInstrumentation(opts ...InstrumentationOption) (*Instrumentation, error) | |
return nil, err | ||
} | ||
|
||
ctrl, err := opentelemetry.NewController(Version()) | ||
ctrl, err := opentelemetry.NewController(Version(), c.serviceName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -106,7 +112,8 @@ type InstrumentationOption interface { | |
} | ||
|
||
type instConfig struct { | ||
target *process.TargetArgs | ||
target *process.TargetArgs | ||
serviceName string | ||
} | ||
|
||
func newInstConfig(opts []InstrumentationOption) instConfig { | ||
|
@@ -122,13 +129,19 @@ func (c instConfig) applyEnv() instConfig { | |
if v, ok := os.LookupEnv(envTargetExeKey); ok { | ||
c.target = &process.TargetArgs{ExePath: v} | ||
} | ||
if v, ok := os.LookupEnv(envServiceNameKey); ok { | ||
c.serviceName = v | ||
} | ||
return c | ||
} | ||
|
||
func (c instConfig) validate() error { | ||
if c.target == nil { | ||
return errUndefinedTarget | ||
} | ||
if c.serviceName == "" { | ||
return errUndefinedServiceName | ||
} | ||
return c.target.Validate() | ||
} | ||
|
||
|
@@ -150,3 +163,39 @@ func WithTarget(path string) InstrumentationOption { | |
return c | ||
}) | ||
} | ||
|
||
// WithServiceName returns an [InstrumentationOption] defining the name of the service running. | ||
// | ||
// If multiple of these options are provided to an [Instrumentation], the last | ||
// one will be used. | ||
// | ||
// If OTEL_SERVICE_NAME is defined it will take precedence over any value | ||
// passed here. | ||
func WithServiceName(serviceName string) InstrumentationOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs a changelog entry in an |
||
return fnOpt(func(c instConfig) instConfig { | ||
c.serviceName = serviceName | ||
return c | ||
}) | ||
} | ||
|
||
// WithPID returns an [InstrumentationOption] corresponding to the executable | ||
// used by the provided pid. | ||
// | ||
// This option conflicts with [WithTarget]. If both are used, the last one | ||
// passed to [Instrumentation] will take precedence and be used. | ||
// | ||
// If multiple of these options are provided to an [Instrumentation], the last | ||
// one will be used. | ||
// | ||
// If OTEL_GO_AUTO_TARGET_EXE is defined it will take precedence over any value | ||
// passed here. | ||
func WithPID(pid int) InstrumentationOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs a changelog entry in an |
||
exeLinkPath := fmt.Sprintf("/proc/%d/exe", pid) | ||
exePath, err := os.Readlink(exeLinkPath) | ||
if err != nil { | ||
log.Logger.Error(err, "Failed to read exe link for process", "pid", pid) | ||
exePath = "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This likely shouldn't return |
||
} | ||
|
||
return WithTarget(exePath) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |
"go.opentelemetry.io/auto/internal/pkg/instrumentors/bpffs" | ||
"go.opentelemetry.io/auto/internal/pkg/instrumentors/context" | ||
"go.opentelemetry.io/auto/internal/pkg/log" | ||
"go.opentelemetry.io/auto/internal/pkg/process" | ||
) | ||
|
||
// Allocator handles the allocation of the BPF file-system. | ||
|
@@ -45,3 +46,7 @@ func (a *Allocator) Load(ctx *context.InstrumentorContext) error { | |
|
||
return nil | ||
} | ||
|
||
func (a *Allocator) Clean(target *process.TargetDetails) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing comment. |
||
return bpffs.Cleanup(target) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an error, or shouldn't it just use the resource default service name if not set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, we are not allowing for not setting the service name env variable. So this code preserves that.
We can change it to a default service name if not set.