forked from open-telemetry/opentelemetry-go-instrumentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrumentation.go
398 lines (352 loc) · 11.7 KB
/
instrumentation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auto
import (
"context"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/go-logr/logr"
"github.com/go-logr/stdr"
"github.com/go-logr/zapr"
"go.opentelemetry.io/contrib/exporters/autoexport"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.uber.org/zap"
"go.opentelemetry.io/auto/internal/pkg/instrumentation"
"go.opentelemetry.io/auto/internal/pkg/opentelemetry"
"go.opentelemetry.io/auto/internal/pkg/process"
)
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"
// envResourceAttrKey is the key for the environment variable value containing
// OpenTelemetry Resource attributes.
envResourceAttrKey = "OTEL_RESOURCE_ATTRIBUTES"
// envTracesExportersKey is the key for the environment variable value
// containing what OpenTelemetry trace exporter to use.
envTracesExportersKey = "OTEL_TRACES_EXPORTER"
)
// Instrumentation manages and controls all OpenTelemetry Go
// auto-instrumentation.
type Instrumentation struct {
target *process.TargetDetails
analyzer *process.Analyzer
manager *instrumentation.Manager
}
// Error message returned when instrumentation is launched without a valid target
// binary or pid.
var errUndefinedTarget = fmt.Errorf("undefined target Go binary, consider setting the %s environment variable pointing to the target binary to instrument", envTargetExeKey)
func newLogger() logr.Logger {
zapLog, err := zap.NewProduction()
var logger logr.Logger
if err != nil {
// Fallback to stdr logger.
logger = stdr.New(log.New(os.Stderr, "", log.LstdFlags))
} else {
logger = zapr.NewLogger(zapLog)
}
return logger
}
// NewInstrumentation returns a new [Instrumentation] configured with the
// provided opts.
//
// If conflicting or duplicate options are provided, the last one will have
// precedence and be used.
func NewInstrumentation(ctx context.Context, opts ...InstrumentationOption) (*Instrumentation, error) {
// TODO: pass this in as an option.
//
// We likely want to use slog instead of logr in the longterm. Wait until
// that package has enough Go version support and then switch to that so we
// can expose it in an option.
logger := newLogger()
logger = logger.WithName("Instrumentation")
c, err := newInstConfig(ctx, opts)
if err != nil {
return nil, err
}
if err := c.validate(); err != nil {
return nil, err
}
pa := process.NewAnalyzer(logger)
pid, err := pa.DiscoverProcessID(&c.target)
if err != nil {
return nil, err
}
ctrl, err := opentelemetry.NewController(logger, c.tracerProvider(), Version())
if err != nil {
return nil, err
}
mngr, err := instrumentation.NewManager(logger, ctrl)
if err != nil {
return nil, err
}
td, err := pa.Analyze(pid, mngr.GetRelevantFuncs())
if err != nil {
mngr.Close()
return nil, err
}
allocDetails, err := process.Allocate(logger, pid)
if err != nil {
return nil, err
}
td.AllocationDetails = allocDetails
logger.Info(
"target process analysis completed",
"pid", td.PID,
"go_version", td.GoVersion,
"dependencies", td.Libraries,
"total_functions_found", len(td.Functions),
)
mngr.FilterUnusedProbes(td)
return &Instrumentation{
target: td,
analyzer: pa,
manager: mngr,
}, nil
}
// Run starts the instrumentation.
func (i *Instrumentation) Run(ctx context.Context) error {
return i.manager.Run(ctx, i.target)
}
// Close closes the Instrumentation, cleaning up all used resources.
func (i *Instrumentation) Close() error {
i.analyzer.Close()
i.manager.Close()
return nil
}
// InstrumentationOption applies a configuration option to [Instrumentation].
type InstrumentationOption interface {
apply(context.Context, instConfig) (instConfig, error)
}
type instConfig struct {
sampler trace.Sampler
traceExp trace.SpanExporter
target process.TargetArgs
serviceName string
}
func newInstConfig(ctx context.Context, opts []InstrumentationOption) (instConfig, error) {
var (
c instConfig
err error
)
for _, opt := range opts {
if opt != nil {
var e error
c, e = opt.apply(ctx, c)
err = errors.Join(err, e)
}
}
// Defaults.
if c.serviceName == "" {
c.serviceName = c.defualtServiceName()
}
if c.traceExp == nil {
var e error
// This is the OTel recommended default.
c.traceExp, e = otlptracehttp.New(ctx)
err = errors.Join(err, e)
}
if c.sampler == nil {
c.sampler = trace.AlwaysSample()
}
return c, err
}
func (c instConfig) defualtServiceName() string {
name := "unknown_service"
if c.target.ExePath != "" {
name = fmt.Sprintf("%s:%s", name, filepath.Base(c.target.ExePath))
}
return name
}
func (c instConfig) validate() error {
var zero process.TargetArgs
if c.target == zero {
return errUndefinedTarget
}
if c.traceExp == nil {
return errors.New("undefined trace exporter")
}
return c.target.Validate()
}
func (c instConfig) tracerProvider() *trace.TracerProvider {
return trace.NewTracerProvider(
trace.WithSampler(c.sampler),
trace.WithResource(c.res()),
trace.WithBatcher(c.traceExp),
trace.WithIDGenerator(opentelemetry.NewEBPFSourceIDGenerator()),
)
}
func (c instConfig) res() *resource.Resource {
runVer := strings.TrimPrefix(runtime.Version(), "go")
runName := runtime.Compiler
if runName == "gc" {
runName = "go"
}
runDesc := fmt.Sprintf(
"go version %s %s/%s",
runVer, runtime.GOOS, runtime.GOARCH,
)
return resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(c.serviceName),
semconv.TelemetrySDKLanguageGo,
semconv.TelemetryAutoVersionKey.String(Version()),
semconv.ProcessRuntimeName(runName),
semconv.ProcessRuntimeVersion(runVer),
semconv.ProcessRuntimeDescription(runDesc),
)
}
type fnOpt func(context.Context, instConfig) (instConfig, error)
func (o fnOpt) apply(ctx context.Context, c instConfig) (instConfig, error) { return o(ctx, c) }
// WithTarget returns an [InstrumentationOption] defining the target binary for
// [Instrumentation] that is being executed at the provided path.
//
// This option conflicts with [WithPID]. If both are used, the last one
// provided to an [Instrumentation] will 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, this option will conflict with
// [WithEnv]. If both are used, the last one provided to an [Instrumentation]
// will be used.
func WithTarget(path string) InstrumentationOption {
return fnOpt(func(_ context.Context, c instConfig) (instConfig, error) {
c.target = process.TargetArgs{ExePath: path}
return c, nil
})
}
// 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 or the service name is defined in
// OTEL_RESOURCE_ATTRIBUTES, this option will conflict with [WithEnv]. If both
// are used, the last one provided to an [Instrumentation] will be used.
func WithServiceName(serviceName string) InstrumentationOption {
return fnOpt(func(_ context.Context, c instConfig) (instConfig, error) {
c.serviceName = serviceName
return c, nil
})
}
// WithPID returns an [InstrumentationOption] defining the target binary for
// [Instrumentation] that is being run with the provided PID.
//
// This option conflicts with [WithTarget]. If both are used, the last one
// provided to an [Instrumentation] will 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, this option will conflict with
// [WithEnv]. If both are used, the last one provided to an [Instrumentation]
// will be used.
func WithPID(pid int) InstrumentationOption {
return fnOpt(func(_ context.Context, c instConfig) (instConfig, error) {
c.target = process.TargetArgs{Pid: pid}
return c, nil
})
}
var lookupEnv = os.LookupEnv
// WithEnv returns an [InstrumentationOption] that will configure
// [Instrumentation] using the values defined by the following environment
// variables:
//
// - OTEL_GO_AUTO_TARGET_EXE: sets the target binary
// - OTEL_SERVICE_NAME (or OTEL_RESOURCE_ATTRIBUTES): sets the service name
// - OTEL_TRACES_EXPORTER: sets the trace exporter
//
// This option may conflict with [WithTarget], [WithPID], [WithTraceExporter],
// and [WithServiceName] if their respective environment variable is defined.
// If more than one of these options are used, the last one provided to an
// [Instrumentation] will be used.
//
// The OTEL_TRACES_EXPORTER environment variable value is resolved using the
// [autoexport] package. See that package's documentation for information on
// supported values and registration of custom exporters.
func WithEnv() InstrumentationOption {
return fnOpt(func(ctx context.Context, c instConfig) (instConfig, error) {
var err error
if v, ok := lookupEnv(envTargetExeKey); ok {
c.target = process.TargetArgs{ExePath: v}
}
if _, ok := lookupEnv(envTracesExportersKey); ok {
// Don't track the lookup value because autoexport does not provide
// a way to just pass the environment value currently. Just use
// NewSpanExporter which will re-read this value.
var e error
// NewSpanExporter will use an OTLP (HTTP/protobuf) exporter as the
// default. This is the OTel recommended default.
c.traceExp, e = autoexport.NewSpanExporter(ctx)
err = errors.Join(err, e)
}
if v, ok := lookupServiceName(); ok {
c.serviceName = v
}
return c, err
})
}
func lookupServiceName() (string, bool) {
// Prioritize OTEL_SERVICE_NAME over OTEL_RESOURCE_ATTRIBUTES value.
if v, ok := lookupEnv(envServiceNameKey); ok {
return v, ok
}
v, ok := lookupEnv(envResourceAttrKey)
if !ok {
return "", false
}
for _, keyval := range strings.Split(strings.TrimSpace(v), ",") {
key, val, found := strings.Cut(keyval, "=")
if !found {
continue
}
key = strings.TrimSpace(key)
if key == string(semconv.ServiceNameKey) {
return strings.TrimSpace(val), true
}
}
return "", false
}
// WithTraceExporter returns an [InstrumentationOption] that will configure an
// [Instrumentation] to use the provided exp to export OpenTelemetry tracing
// telemetry.
//
// If OTEL_TRACES_EXPORTER is defined, this option will conflict with
// [WithEnv]. If both are used, the last one provided to an [Instrumentation]
// will be used.
func WithTraceExporter(exp trace.SpanExporter) InstrumentationOption {
return fnOpt(func(_ context.Context, c instConfig) (instConfig, error) {
c.traceExp = exp
return c, nil
})
}
// WithSampler returns an [InstrumentationOption] that will configure
// an [Instrumentation] to use the provided sampler to sample OpenTelemetry traces.
func WithSampler(sampler trace.Sampler) InstrumentationOption {
return fnOpt(func(_ context.Context, c instConfig) (instConfig, error) {
c.sampler = sampler
return c, nil
})
}