-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathnewrelic.go
More file actions
286 lines (237 loc) · 8.14 KB
/
Copy pathnewrelic.go
File metadata and controls
286 lines (237 loc) · 8.14 KB
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
package newrelic
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/cloudfoundry/php-buildpack/src/php/extensions"
)
const newrelicEnvScript = `if [[ -z "${NEWRELIC_LICENSE:-}" ]]; then
export NEWRELIC_LICENSE=$(echo $VCAP_SERVICES | jq -r '.newrelic[0].credentials.licenseKey')
fi
`
// NewRelicExtension downloads, installs and configures the NewRelic agent for PHP
type NewRelicExtension struct {
detected bool
appName string
licenseKey string
newrelicSo string
logPath string
daemonLogPath string
daemonPath string
socketPath string
pidPath string
phpIniPath string
phpExtnDir string
phpAPI string
phpZTS bool
phpArch string
buildDir string
bpDir string
}
// Name returns the extension name
func (e *NewRelicExtension) Name() string {
return "newrelic"
}
// ShouldCompile determines if NewRelic should be installed
func (e *NewRelicExtension) ShouldCompile(ctx *extensions.Context) bool {
// Only run if PHP VM is 'php'
if ctx.GetString("PHP_VM") != "php" {
return false
}
e.loadServiceInfo(ctx)
e.loadNewRelicInfo(ctx)
return e.detected
}
// loadServiceInfo searches for NewRelic service
func (e *NewRelicExtension) loadServiceInfo(ctx *extensions.Context) {
services := ctx.FindServicesByLabel("newrelic")
if len(services) == 0 {
fmt.Println("-----> NewRelic services not detected.")
return
}
if len(services) > 1 {
fmt.Println("-----> WARNING: Multiple NewRelic services found, using credentials from first one.")
}
if len(services) > 0 {
service := services[0]
if licenseKey, ok := service.Credentials["licenseKey"].(string); ok && licenseKey != "" {
e.licenseKey = licenseKey
e.detected = true
}
}
}
// loadNewRelicInfo loads application info and checks for manual configuration
func (e *NewRelicExtension) loadNewRelicInfo(ctx *extensions.Context) {
// Get app name from VCAP_APPLICATION
e.appName = ctx.VcapApplication.Name
// Check for manual license key configuration
if manualKey := ctx.GetString("NEWRELIC_LICENSE"); manualKey != "" {
if e.detected {
fmt.Println("-----> WARNING: Detected a NewRelic Service & Manual Key, using the manual key.")
}
e.licenseKey = manualKey
e.detected = true
} else if e.licenseKey != "" {
// Store license key in context for later use
ctx.Set("NEWRELIC_LICENSE", e.licenseKey)
}
}
// Configure runs early configuration
func (e *NewRelicExtension) Configure(ctx *extensions.Context) error {
e.buildDir = ctx.GetString("BUILD_DIR")
e.bpDir = ctx.GetString("BP_DIR")
// Load PHP info
e.phpIniPath = filepath.Join(e.buildDir, "php", "etc", "php.ini")
if err := e.loadPHPInfo(); err != nil {
return fmt.Errorf("failed to load PHP info: %w", err)
}
if e.detected {
// Set up paths
newrelicSoName := fmt.Sprintf("newrelic-%s%s.so", e.phpAPI, map[bool]string{true: "zts", false: ""}[e.phpZTS])
e.newrelicSo = filepath.Join("@{HOME}", "newrelic", "agent", e.phpArch, newrelicSoName)
e.logPath = filepath.Join("@{HOME}", "logs", "newrelic.log")
e.daemonLogPath = filepath.Join("@{HOME}", "logs", "newrelic-daemon.log")
e.daemonPath = filepath.Join("@{HOME}", "newrelic", "daemon", fmt.Sprintf("newrelic-daemon.%s", e.phpArch))
e.socketPath = filepath.Join("@{HOME}", "newrelic", "daemon.sock")
e.pidPath = filepath.Join("@{HOME}", "newrelic", "daemon.pid")
}
return nil
}
// loadPHPInfo extracts PHP configuration information
func (e *NewRelicExtension) loadPHPInfo() error {
// Find extension_dir from php.ini
data, err := os.ReadFile(e.phpIniPath)
if err != nil {
return fmt.Errorf("failed to read php.ini: %w", err)
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "extension_dir") {
parts := strings.Split(line, " = ")
if len(parts) == 2 {
e.phpExtnDir = strings.Trim(parts[1], "\"")
break
}
}
}
if e.phpExtnDir == "" {
return fmt.Errorf("extension_dir not found in php.ini")
}
// Parse PHP API version and ZTS status from extension directory
basename := filepath.Base(e.phpExtnDir)
parts := strings.Split(basename, "-")
if len(parts) > 0 {
e.phpAPI = parts[len(parts)-1]
}
e.phpZTS = !strings.Contains(basename, "non-zts")
// Set architecture (default to x64)
e.phpArch = "x64"
if arch := os.Getenv("NEWRELIC_ARCH"); arch != "" {
e.phpArch = arch
}
return nil
}
// Compile downloads and installs NewRelic
func (e *NewRelicExtension) Compile(ctx *extensions.Context, installer *extensions.Installer) error {
if !e.detected {
return nil
}
fmt.Println("-----> Installing NewRelic")
newrelicInstallDir := filepath.Join(e.buildDir, "newrelic")
if err := installer.InstallOnlyVersion("newrelic", newrelicInstallDir); err != nil {
return fmt.Errorf("failed to install NewRelic package: %w", err)
}
// Add environment variables script
if err := e.addingEnvironmentVariables(); err != nil {
return fmt.Errorf("failed to add environment variables: %w", err)
}
// Modify php.ini
fmt.Println("-----> Configuring NewRelic in php.ini")
if err := e.modifyPHPIni(); err != nil {
return fmt.Errorf("failed to modify php.ini: %w", err)
}
fmt.Println("-----> NewRelic Installed.")
return nil
}
// addingEnvironmentVariables creates the NewRelic environment script
func (e *NewRelicExtension) addingEnvironmentVariables() error {
destFolder := filepath.Join(e.buildDir, ".profile.d")
dest := filepath.Join(destFolder, "0_newrelic_env.sh")
// Create .profile.d folder if it doesn't exist
if err := os.MkdirAll(destFolder, 0755); err != nil {
return fmt.Errorf("failed to create .profile.d directory: %w", err)
}
// Write the environment script
if err := os.WriteFile(dest, []byte(newrelicEnvScript), 0644); err != nil {
return fmt.Errorf("failed to write newrelic_env.sh: %w", err)
}
return nil
}
// modifyPHPIni adds NewRelic configuration to php.ini
func (e *NewRelicExtension) modifyPHPIni() error {
data, err := os.ReadFile(e.phpIniPath)
if err != nil {
return fmt.Errorf("failed to read php.ini: %w", err)
}
lines := strings.Split(string(data), "\n")
// Find where to insert the extension line
// Look for the last extension= line
insertPos := -1
for i, line := range lines {
if strings.HasPrefix(strings.TrimSpace(line), "extension=") {
insertPos = i + 1
}
}
// If no extensions found, insert after @{PHP_EXTENSIONS} marker
if insertPos == -1 {
for i, line := range lines {
if strings.Contains(line, "@{PHP_EXTENSIONS}") {
insertPos = i + 1
break
}
}
}
if insertPos == -1 {
return fmt.Errorf("could not find suitable position to insert extension in php.ini")
}
// Insert the NewRelic extension line
newLines := append(lines[:insertPos], append([]string{fmt.Sprintf("extension=%s", e.newrelicSo)}, lines[insertPos:]...)...)
// Append NewRelic configuration section at the end
newRelicConfig := []string{
"",
"[newrelic]",
fmt.Sprintf("newrelic.license=%s", "@{NEWRELIC_LICENSE}"),
fmt.Sprintf("newrelic.appname=%s", e.appName),
fmt.Sprintf("newrelic.logfile=%s", e.logPath),
fmt.Sprintf("newrelic.daemon.logfile=%s", e.daemonLogPath),
fmt.Sprintf("newrelic.daemon.location=%s", e.daemonPath),
fmt.Sprintf("newrelic.daemon.port=%s", e.socketPath),
fmt.Sprintf("newrelic.daemon.pidfile=%s", e.pidPath),
}
newLines = append(newLines, newRelicConfig...)
// Write back to php.ini
output := strings.Join(newLines, "\n")
if err := os.WriteFile(e.phpIniPath, []byte(output), 0644); err != nil {
return fmt.Errorf("failed to write php.ini: %w", err)
}
return nil
}
// PreprocessCommands returns commands to run before app starts (none for NewRelic)
func (e *NewRelicExtension) PreprocessCommands(ctx *extensions.Context) ([]string, error) {
return nil, nil
}
// ServiceCommands returns long-running service commands (none for NewRelic)
func (e *NewRelicExtension) ServiceCommands(ctx *extensions.Context) (map[string]string, error) {
return nil, nil
}
// ServiceEnvironment returns environment variables for runtime
func (e *NewRelicExtension) ServiceEnvironment(ctx *extensions.Context) (map[string]string, error) {
if !e.detected {
return nil, nil
}
return map[string]string{
"NEWRELIC_LICENSE": "$NEWRELIC_LICENSE",
}, nil
}