Skip to content

Commit 458d199

Browse files
committed
Refactor splunk_otel_java_agent framework to use GetVCAPServices
1 parent c05278a commit 458d199

1 file changed

Lines changed: 40 additions & 45 deletions

File tree

src/java/frameworks/splunk_otel_java_agent.go

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package frameworks
1717

1818
import (
19-
"encoding/json"
2019
"fmt"
2120
"github.com/cloudfoundry/java-buildpack/src/java/common"
2221
"os"
@@ -179,56 +178,52 @@ func (s *SplunkOtelJavaAgentFramework) getCredentials() SplunkCredentials {
179178
}
180179

181180
// Check service binding
182-
vcapServices := os.Getenv("VCAP_SERVICES")
183-
if vcapServices == "" {
181+
vcapServices, err := GetVCAPServices()
182+
if err != nil {
184183
return creds
185184
}
186185

187-
var services map[string][]map[string]interface{}
188-
if err := json.Unmarshal([]byte(vcapServices), &services); err != nil {
186+
// Find the first matching Splunk service, preferring explicit labels over name pattern matches
187+
var service *common.VCAPService
188+
for _, label := range []string{"splunk", "splunk-otel"} {
189+
if vcapServices.HasService(label) {
190+
service = vcapServices.GetService(label)
191+
break
192+
}
193+
}
194+
if service == nil {
195+
service = vcapServices.GetServiceByNamePattern("splunk")
196+
}
197+
if service == nil {
198+
service = vcapServices.GetServiceByNamePattern("otel")
199+
}
200+
if service == nil {
189201
return creds
190202
}
191203

192-
// Look for splunk service
193-
serviceNames := []string{
194-
"splunk",
195-
"splunk-otel",
196-
"user-provided",
197-
}
198-
199-
for _, serviceName := range serviceNames {
200-
if serviceList, ok := services[serviceName]; ok {
201-
for _, service := range serviceList {
202-
if credentials, ok := service["credentials"].(map[string]interface{}); ok {
203-
// Get OTLP endpoint
204-
if endpoint, ok := credentials["otlp_endpoint"].(string); ok {
205-
creds.OTLPEndpoint = endpoint
206-
} else if endpoint, ok := credentials["otlpEndpoint"].(string); ok {
207-
creds.OTLPEndpoint = endpoint
208-
} else if endpoint, ok := credentials["endpoint"].(string); ok {
209-
creds.OTLPEndpoint = endpoint
210-
}
211-
212-
// Get access token
213-
if token, ok := credentials["access_token"].(string); ok {
214-
creds.AccessToken = token
215-
} else if token, ok := credentials["accessToken"].(string); ok {
216-
creds.AccessToken = token
217-
} else if token, ok := credentials["token"].(string); ok {
218-
creds.AccessToken = token
219-
}
220-
221-
// Get realm
222-
if realm, ok := credentials["realm"].(string); ok {
223-
creds.Realm = realm
224-
}
225-
226-
if creds.OTLPEndpoint != "" {
227-
return creds
228-
}
229-
}
230-
}
231-
}
204+
credentials := service.Credentials
205+
206+
// Get OTLP endpoint
207+
if endpoint, ok := credentials["otlp_endpoint"].(string); ok {
208+
creds.OTLPEndpoint = endpoint
209+
} else if endpoint, ok := credentials["otlpEndpoint"].(string); ok {
210+
creds.OTLPEndpoint = endpoint
211+
} else if endpoint, ok := credentials["endpoint"].(string); ok {
212+
creds.OTLPEndpoint = endpoint
213+
}
214+
215+
// Get access token
216+
if token, ok := credentials["access_token"].(string); ok {
217+
creds.AccessToken = token
218+
} else if token, ok := credentials["accessToken"].(string); ok {
219+
creds.AccessToken = token
220+
} else if token, ok := credentials["token"].(string); ok {
221+
creds.AccessToken = token
222+
}
223+
224+
// Get realm
225+
if realm, ok := credentials["realm"].(string); ok {
226+
creds.Realm = realm
232227
}
233228

234229
return creds

0 commit comments

Comments
 (0)