Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
com.sap.hcf.cf.logging.opentelemetry.agent.ext.exporter.CloudLoggingLogsExporterProvider
com.sap.hcf.cf.logging.opentelemetry.agent.ext.exporter.VcapServiceLogsExporterProvider
131 changes: 131 additions & 0 deletions cf-java-logging-support-opentelemetry-agent-extension/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.binding;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.TreeMap;

Expand All @@ -12,7 +13,12 @@ private CloudFoundryCredentials(Builder builder) {
}

public String getString(String key) {
return properties.get(key);
return key == null ? null : properties.get(key);
}

public byte[] getPEMBytes(String key) {
String raw = getString(key);
return raw == null ? null : raw.trim().replace("\\n", "\n").getBytes(StandardCharsets.UTF_8);
}

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public CloudFoundryServicesAdapter() {

/**
* Stream CfServices, that match the provided properties. Empty or null values are interpreted as not applicable. No
* check will be performed during search. User-provided service instances will be preferred unless the
* {@code userProvidedLabel is null or empty. Provided only null values will return all service instances.
* check will be performed during search. Provided only null values will return all service instances.
*
* @param serviceLabels
* the labels of services
Expand All @@ -50,6 +49,23 @@ public CloudFoundryServicesAdapter() {
* @return a stream of service instances present in the CloudFoundry environment variable VCAP_SERVICES
*/
Stream<CloudFoundryServiceInstance> stream(List<String> serviceLabels, List<String> serviceTags) {
return stream(serviceLabels, serviceTags, null);
}

/**
* Stream CfServices, that match the provided properties. Empty or null values are interpreted as not applicable. No
* check will be performed during search. Provided only null values will return all service instances.
*
* @param serviceLabels
* the labels of services
* @param serviceTags
* the tags of services
* @param serviceName
* the name of the service
* @return a stream of service instances present in the CloudFoundry environment variable VCAP_SERVICES
*/
Stream<CloudFoundryServiceInstance> stream(List<String> serviceLabels, List<String> serviceTags,
String serviceName) {
if (vcapServicesJson == null) {
LOG.info("No environment variable " + VCAP_SERVICES + " found. Skipping service binding detection.");
return Stream.empty();
Expand All @@ -62,7 +78,8 @@ Stream<CloudFoundryServiceInstance> stream(List<String> serviceLabels, List<Stri
if (isNullOrEmpty(serviceLabels) || serviceLabels.contains(label)) {
parseServiceInstances(parser, label, serviceInstance -> {
if (serviceInstance.getName() != null) {
if (hasServiceTag(serviceTags, serviceInstance.getTags())) {
if (hasServiceName(serviceName, serviceInstance.getName()) && hasServiceTag(serviceTags,
serviceInstance.getTags())) {
services.add(serviceInstance);
}
}
Expand Down Expand Up @@ -168,6 +185,10 @@ private boolean hasServiceTag(List<String> requiredTags, List<String> instanceTa
return instanceTags.containsAll(requiredTags);
}

private boolean hasServiceName(String requiredName, String actualName) {
return requiredName == null || requiredName.isEmpty() || requiredName.equals(actualName);
}

private Comparator<CloudFoundryServiceInstance> byLabels(List<String> serviceLabels) {
return (l, r) -> getIndex(serviceLabels, l.getLabel()) - getIndex(serviceLabels, r.getLabel());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.binding;

import com.sap.hcf.cf.logging.opentelemetry.agent.ext.config.ExtensionConfigurations.RUNTIME;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;

import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

public class VcapServiceProvider implements Supplier<CloudFoundryServiceInstance> {

private final CloudFoundryServiceInstance service;

public VcapServiceProvider(ConfigProperties config) {
this(config, CloudFoundryServicesAdapter.builder().build());
}

VcapServiceProvider(ConfigProperties config, CloudFoundryServicesAdapter adapter) {
String label = RUNTIME.CLOUD_FOUNDRY.SERVICE.VCAP_SERVICE.LABEL.getValue(config);
String tag = RUNTIME.CLOUD_FOUNDRY.SERVICE.VCAP_SERVICE.TAG.getValue(config);
String name = RUNTIME.CLOUD_FOUNDRY.SERVICE.VCAP_SERVICE.NAME.getValue(config);

List<String> serviceLabels = label != null ? List.of(label) : Collections.emptyList();
List<String> serviceTags = tag != null ? List.of(tag) : Collections.emptyList();

this.service = adapter.stream(serviceLabels, serviceTags, name).findFirst().orElse(null);
}

@Override
public CloudFoundryServiceInstance get() {
return service;
}
}
Loading
Loading