-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
apm_inject.go
250 lines (220 loc) · 8.1 KB
/
apm_inject.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build !windows
// Package service provides a way to interact with os services
package service
import (
"bytes"
"context"
"fmt"
"os"
"path"
"strings"
"github.com/DataDog/datadog-agent/pkg/util/log"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
var (
injectorConfigPrefix = []byte("# BEGIN LD PRELOAD CONFIG")
injectorConfigSuffix = []byte("# END LD PRELOAD CONFIG")
)
const (
injectorConfigTemplate = `
apm_config:
receiver_socket: %s
use_dogstatsd: true
dogstatsd_socket: %s
`
datadogConfigPath = "/etc/datadog-agent/datadog.yaml"
ldSoPreloadPath = "/etc/ld.so.preload"
injectorPath = "/opt/datadog-packages/datadog-apm-inject/stable"
oldLDPath = "/opt/datadog/apm/inject/launcher.preload.so"
)
// SetupAPMInjector sets up the injector at bootstrap
func SetupAPMInjector(ctx context.Context) error {
var err error
span, ctx := tracer.StartSpanFromContext(ctx, "setup_injector")
defer span.Finish(tracer.WithError(err))
installer := newAPMInjectorInstaller(injectorPath)
return installer.Setup(ctx)
}
// RemoveAPMInjector removes the APM injector
func RemoveAPMInjector(ctx context.Context) {
span, ctx := tracer.StartSpanFromContext(ctx, "remove_injector")
defer span.Finish()
installer := newAPMInjectorInstaller(injectorPath)
installer.Remove(ctx)
}
func newAPMInjectorInstaller(path string) *apmInjectorInstaller {
a := &apmInjectorInstaller{
installPath: path,
}
a.ldPreloadFileInstrument = newFileMutator(ldSoPreloadPath, a.setLDPreloadConfigContent, nil, nil)
a.ldPreloadFileUninstrument = newFileMutator(ldSoPreloadPath, a.deleteLDPreloadConfigContent, nil, nil)
a.agentConfigSockets = newFileMutator(datadogConfigPath, a.setAgentConfigContent, nil, nil)
a.dockerConfigInstrument = newFileMutator(dockerDaemonPath, a.setDockerConfigContent, nil, nil)
a.dockerConfigUninstrument = newFileMutator(dockerDaemonPath, a.deleteDockerConfigContent, nil, nil)
return a
}
type apmInjectorInstaller struct {
installPath string
ldPreloadFileInstrument *fileMutator
ldPreloadFileUninstrument *fileMutator
agentConfigSockets *fileMutator
dockerConfigInstrument *fileMutator
dockerConfigUninstrument *fileMutator
}
// Setup sets up the APM injector
func (a *apmInjectorInstaller) Setup(ctx context.Context) (err error) {
var rollbackAgentConfig, rollbackLDPreload, rollbackDockerConfig func() error
defer func() {
if err != nil {
// todo propagate rollbacks until success of package installation
if rollbackLDPreload != nil {
if err := rollbackLDPreload(); err != nil {
log.Warnf("Failed to rollback ld preload: %v", err)
}
}
if rollbackAgentConfig != nil {
if err := rollbackAgentConfig(); err != nil {
log.Warnf("Failed to rollback agent config: %v", err)
}
}
if rollbackDockerConfig != nil {
if err := rollbackDockerConfig(); err != nil {
log.Warnf("Failed to rollback docker config: %v", err)
}
}
}
}()
rollbackAgentConfig, err = a.setupSockets(ctx)
if err != nil {
return err
}
rollbackLDPreload, err = a.ldPreloadFileInstrument.mutate()
if err != nil {
return err
}
// TODO only instrument docker if DD_APM_INSTRUMENTATION_ENABLED=docker
// is set
rollbackDockerConfig, err = a.setupDocker(ctx)
return err
}
func (a *apmInjectorInstaller) Remove(ctx context.Context) {
if _, err := a.ldPreloadFileUninstrument.mutate(); err != nil {
log.Warnf("Failed to remove ld preload config: %v", err)
}
// TODO docker only on DD_APM_INSTRUMENTATION_ENABLED=docker
if err := a.uninstallDocker(ctx); err != nil {
log.Warnf("Failed to remove docker config: %v", err)
}
}
// setupSockets sets up the sockets for the APM injector
// TODO rework entirely for safe transition
func (a *apmInjectorInstaller) setupSockets(ctx context.Context) (func() error, error) {
// don't install sockets if already in env variable
if os.Getenv("DD_APM_RECEIVER_SOCKET") != "" {
return nil, nil
}
// TODO: remove sockets from run
if err := a.setRunPermissions(); err != nil {
return nil, err
}
rollbackAgentConfig, err := a.agentConfigSockets.mutate()
if err != nil {
return nil, err
}
if err := restartTraceAgent(ctx); err != nil {
return nil, err
}
rollback := func() error {
if err := rollbackAgentConfig(); err != nil {
return err
}
if err := restartTraceAgent(ctx); err != nil {
return err
}
return nil
}
return rollback, nil
}
func (a *apmInjectorInstaller) setRunPermissions() error {
return os.Chmod(path.Join(a.installPath, "inject", "run"), 0777)
}
// setLDPreloadConfigContent sets the content of the LD preload configuration
func (a *apmInjectorInstaller) setLDPreloadConfigContent(ldSoPreload []byte) ([]byte, error) {
launcherPreloadPath := path.Join(a.installPath, "inject", "launcher.preload.so")
if strings.Contains(string(ldSoPreload), launcherPreloadPath) {
// If the line of interest is already in /etc/ld.so.preload, return fast
return ldSoPreload, nil
}
if bytes.Contains(ldSoPreload, []byte(oldLDPath)) {
return bytes.ReplaceAll(ldSoPreload, []byte(oldLDPath), []byte(launcherPreloadPath)), nil
}
var buf bytes.Buffer
buf.Write(ldSoPreload)
// Append the launcher preload path to the file
if len(ldSoPreload) > 0 && ldSoPreload[len(ldSoPreload)-1] != '\n' {
buf.WriteByte('\n')
}
buf.WriteString(launcherPreloadPath)
buf.WriteByte('\n')
return buf.Bytes(), nil
}
// deleteLDPreloadConfigContent deletes the content of the LD preload configuration
func (a *apmInjectorInstaller) deleteLDPreloadConfigContent(ldSoPreload []byte) ([]byte, error) {
launcherPreloadPath := path.Join(a.installPath, "inject", "launcher.preload.so")
if !strings.Contains(string(ldSoPreload), launcherPreloadPath) {
// If the line of interest isn't there, return fast
return ldSoPreload, nil
}
// Possible configurations of the preload path, order matters
replacementsToTest := [][]byte{
[]byte(launcherPreloadPath + "\n"),
[]byte("\n" + launcherPreloadPath),
[]byte(launcherPreloadPath + " "),
[]byte(" " + launcherPreloadPath),
}
for _, replacement := range replacementsToTest {
ldSoPreloadNew := bytes.Replace(ldSoPreload, replacement, []byte{}, 1)
if !bytes.Equal(ldSoPreloadNew, ldSoPreload) {
return ldSoPreloadNew, nil
}
}
if bytes.Equal(ldSoPreload, []byte(launcherPreloadPath)) {
// If the line is the only one in the file without newlines, return an empty file
return []byte{}, nil
}
return nil, fmt.Errorf("failed to remove %s from %s", launcherPreloadPath, ldSoPreloadPath)
}
// setAgentConfigContent adds the agent configuration for the APM injector if it is not there already
// We assume that the agent file has been created by the installer's postinst script
//
// Note: This is not safe, as it assumes there were no changes to the agent configuration made without
// restart by the user. This means that the agent can crash on restart. This is a limitation of the current
// installer system and this will be replaced by a proper experiment when available. This is a temporary
// solution to allow the APM injector to be installed, and if the agent crashes, we try to detect it and
// restore the previous configuration
func (a *apmInjectorInstaller) setAgentConfigContent(content []byte) ([]byte, error) {
runPath := path.Join(a.installPath, "inject", "run")
apmSocketPath := path.Join(runPath, "apm.socket")
dsdSocketPath := path.Join(runPath, "dsd.socket")
if !bytes.Contains(content, injectorConfigPrefix) {
content = append(content, []byte("\n")...)
content = append(content, injectorConfigPrefix...)
content = append(content, []byte(
fmt.Sprintf(injectorConfigTemplate, apmSocketPath, dsdSocketPath),
)...)
content = append(content, injectorConfigSuffix...)
content = append(content, []byte("\n")...)
}
return content, nil
}
// restartTraceAgent restarts the stable trace agent
func restartTraceAgent(ctx context.Context) error {
if err := restartUnit(ctx, "datadog-agent-trace.service"); err != nil {
return err
}
return nil
}