-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathnew_relic.go
More file actions
173 lines (141 loc) · 6.03 KB
/
new_relic.go
File metadata and controls
173 lines (141 loc) · 6.03 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
package frameworks
import (
"fmt"
"github.com/cloudfoundry/java-buildpack/src/java/common"
"github.com/cloudfoundry/java-buildpack/src/java/resources"
"os"
"path/filepath"
"strings"
"github.com/cloudfoundry/libbuildpack"
)
// NewRelicFramework implements New Relic APM agent support
type NewRelicFramework struct {
context *common.Context
}
// NewNewRelicFramework creates a new New Relic framework instance
func NewNewRelicFramework(ctx *common.Context) *NewRelicFramework {
return &NewRelicFramework{context: ctx}
}
// Detect checks if New Relic should be included
func (n *NewRelicFramework) Detect() (string, error) {
// Check for New Relic service binding
vcapServices, err := GetVCAPServices()
if err != nil {
n.context.Log.Warning("Failed to parse VCAP_SERVICES: %s", err.Error())
return "", nil
}
// New Relic can be bound as:
// - "newrelic" service (marketplace or label)
// - Services with "newrelic" tag
// - User-provided services with "newrelic" in the name (Docker platform)
if vcapServices.HasService("newrelic") || vcapServices.HasTag("newrelic") || vcapServices.HasServiceByNamePattern("newrelic") {
n.context.Log.Info("New Relic service detected!")
return "New Relic Agent", nil
}
// Also check for NEW_RELIC_LICENSE_KEY environment variable
if n.context.Stager.LinkDirectoryInDepDir(filepath.Join(n.context.Stager.BuildDir(), ".new-relic-credentials"), "new-relic-credentials") == nil {
return "New Relic Agent", nil
}
n.context.Log.Debug("New Relic not detected")
return "", nil
}
// findNewRelicAgent locates the newrelic.jar in the agent directory
func (n *NewRelicFramework) findNewRelicAgent(agentDir string) (string, error) {
return FindFileInDirectory(agentDir, "newrelic.jar", []string{"", "newrelic"})
}
// Supply installs the New Relic agent
func (n *NewRelicFramework) Supply() error {
n.context.Log.BeginStep("Installing New Relic Agent")
// Get New Relic agent dependency from manifest
dep, err := n.context.Manifest.DefaultVersion("newrelic")
if err != nil {
n.context.Log.Warning("Unable to determine New Relic version, using default")
dep = libbuildpack.Dependency{
Name: "newrelic",
Version: "8.14.0", // Fallback version
}
}
// Install New Relic agent JAR
agentDir := filepath.Join(n.context.Stager.DepDir(), "new_relic_agent")
if err := n.context.Installer.InstallDependency(dep, agentDir); err != nil {
return fmt.Errorf("failed to install New Relic agent: %w", err)
}
// Install default newrelic.yml configuration from embedded resources
if err := n.installDefaultConfiguration(agentDir); err != nil {
n.context.Log.Warning("Could not install default New Relic configuration: %s", err.Error())
}
n.context.Log.Info("Installed New Relic Agent version %s", dep.Version)
return nil
}
// installDefaultConfiguration installs the default newrelic.yml from embedded resources
func (n *NewRelicFramework) installDefaultConfiguration(agentDir string) error {
configPath := filepath.Join(agentDir, "newrelic.yml")
// Check if configuration already exists (user-provided or from external config)
if _, err := os.Stat(configPath); err == nil {
n.context.Log.Debug("newrelic.yml already exists, skipping default configuration")
return nil
}
// Read embedded newrelic.yml template
embeddedPath := "new_relic_agent/newrelic.yml"
configData, err := resources.GetResource(embeddedPath)
if err != nil {
return fmt.Errorf("failed to read embedded newrelic.yml: %w", err)
}
// Process ERB-style template placeholders
configStr := string(configData)
// Replace <%= generated_for_user %> with buildpack info
configStr = strings.ReplaceAll(configStr, "<%= generated_for_user %>",
"This configuration file was generated by the Cloud Foundry Java Buildpack")
// Replace <%= license_key %> with placeholder (will be set via JAVA_OPTS at runtime)
// The license key will be provided through service bindings in the Finalize phase
configStr = strings.ReplaceAll(configStr, "<%= license_key %>", "YOUR_LICENSE_KEY_HERE")
// Write configuration file
if err := os.WriteFile(configPath, []byte(configStr), 0644); err != nil {
return fmt.Errorf("failed to write newrelic.yml: %w", err)
}
n.context.Log.Info("Installed default New Relic configuration")
n.context.Log.Debug(" - newrelic.yml (license key and app name will be configured via JAVA_OPTS)")
return nil
}
// Finalize performs final New Relic configuration
func (n *NewRelicFramework) Finalize() error {
// Get buildpack index for multi-buildpack support
depsIdx := n.context.Stager.DepsIdx()
// Find the actual New Relic agent jar at staging time
agentDir := filepath.Join(n.context.Stager.DepDir(), "new_relic_agent")
agentJarPath, err := n.findNewRelicAgent(agentDir)
if err != nil {
return fmt.Errorf("failed to locate newrelic.jar: %w", err)
}
// Build runtime path using $DEPS_DIR
relPath, err := filepath.Rel(n.context.Stager.DepDir(), agentJarPath)
if err != nil {
return fmt.Errorf("failed to compute relative path: %w", err)
}
runtimeAgentPath := filepath.Join(fmt.Sprintf("$DEPS_DIR/%s", depsIdx), relPath)
// Add javaagent to JAVA_OPTS
javaOpts := fmt.Sprintf("-javaagent:%s", runtimeAgentPath)
// Get New Relic configuration from service binding
vcapServices, _ := GetVCAPServices()
service := vcapServices.GetService("newrelic")
// If not found by label, try user-provided services (Docker platform)
if service == nil {
service = vcapServices.GetServiceByNamePattern("newrelic")
}
if service != nil {
// Add license key from service credentials
if licenseKey, ok := service.Credentials["licenseKey"].(string); ok && licenseKey != "" {
javaOpts += fmt.Sprintf(" -Dnewrelic.config.license_key=%s", licenseKey)
}
// Add app name from service name
if service.Name != "" {
javaOpts += fmt.Sprintf(" -Dnewrelic.config.app_name='%s'", service.Name)
}
}
// Write to .opts file using priority 35
if err := writeJavaOptsFile(n.context, 35, "new_relic", javaOpts); err != nil {
return fmt.Errorf("failed to write java_opts file: %w", err)
}
n.context.Log.Info("New Relic Agent configured (priority 35)")
return nil
}