|
16 | 16 | package frameworks |
17 | 17 |
|
18 | 18 | import ( |
19 | | - "encoding/json" |
20 | 19 | "fmt" |
21 | 20 | "github.com/cloudfoundry/java-buildpack/src/java/common" |
22 | 21 | "os" |
@@ -179,56 +178,52 @@ func (s *SplunkOtelJavaAgentFramework) getCredentials() SplunkCredentials { |
179 | 178 | } |
180 | 179 |
|
181 | 180 | // Check service binding |
182 | | - vcapServices := os.Getenv("VCAP_SERVICES") |
183 | | - if vcapServices == "" { |
| 181 | + vcapServices, err := GetVCAPServices() |
| 182 | + if err != nil { |
184 | 183 | return creds |
185 | 184 | } |
186 | 185 |
|
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 { |
189 | 201 | return creds |
190 | 202 | } |
191 | 203 |
|
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 |
232 | 227 | } |
233 | 228 |
|
234 | 229 | return creds |
|
0 commit comments