-
Notifications
You must be signed in to change notification settings - Fork 872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add resource provider for CloudFoundry #12862
Open
KarstenSchnitter
wants to merge
2
commits into
open-telemetry:main
Choose a base branch
from
KarstenSchnitter:cf-resource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
instrumentation/cloudfoundry/cloudfoundry-resources/library/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
plugins { | ||
id("otel.sdk-extension") | ||
} | ||
|
||
dependencies { | ||
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi") | ||
|
||
annotationProcessor("com.google.auto.service:auto-service") | ||
compileOnly("com.google.auto.service:auto-service-annotations") | ||
testCompileOnly("com.google.auto.service:auto-service-annotations") | ||
|
||
implementation("org.snakeyaml:snakeyaml-engine") | ||
|
||
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi") | ||
} |
107 changes: 107 additions & 0 deletions
107
.../opentelemetry/javaagent/instrumentation/cloudfoundry/resources/CloudFoundryResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.cloudfoundry.resources; | ||
|
||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.semconv.SchemaUrls; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import org.snakeyaml.engine.v2.api.Load; | ||
import org.snakeyaml.engine.v2.api.LoadSettings; | ||
|
||
public final class CloudFoundryResource { | ||
|
||
private static final String ENV_VCAP_APPLICATION = "VCAP_APPLICATION"; | ||
|
||
// copied from CloudfoundryIncubatingAttributes | ||
private static final AttributeKey<String> CLOUDFOUNDRY_APP_ID = | ||
AttributeKey.stringKey("cloudfoundry.app.id"); | ||
private static final AttributeKey<String> CLOUDFOUNDRY_APP_INSTANCE_ID = | ||
AttributeKey.stringKey("cloudfoundry.app.instance.id"); | ||
private static final AttributeKey<String> CLOUDFOUNDRY_APP_NAME = | ||
AttributeKey.stringKey("cloudfoundry.app.name"); | ||
private static final AttributeKey<String> CLOUDFOUNDRY_ORG_ID = | ||
AttributeKey.stringKey("cloudfoundry.org.id"); | ||
private static final AttributeKey<String> CLOUDFOUNDRY_ORG_NAME = | ||
AttributeKey.stringKey("cloudfoundry.org.name"); | ||
private static final AttributeKey<String> CLOUDFOUNDRY_PROCESS_ID = | ||
AttributeKey.stringKey("cloudfoundry.process.id"); | ||
private static final AttributeKey<String> CLOUDFOUNDRY_PROCESS_TYPE = | ||
AttributeKey.stringKey("cloudfoundry.process.type"); | ||
private static final AttributeKey<String> CLOUDFOUNDRY_SPACE_ID = | ||
AttributeKey.stringKey("cloudfoundry.space.id"); | ||
private static final AttributeKey<String> CLOUDFOUNDRY_SPACE_NAME = | ||
AttributeKey.stringKey("cloudfoundry.space.name"); | ||
private static final Resource INSTANCE = buildResource(System::getenv); | ||
|
||
private CloudFoundryResource() {} | ||
|
||
public static Resource get() { | ||
return INSTANCE; | ||
} | ||
|
||
static Resource buildResource(Function<String, String> getenv) { | ||
String vcapAppRaw = getenv.apply(ENV_VCAP_APPLICATION); | ||
// If there is no VCAP_APPLICATION in the environment, we are likely not running in CloudFoundry | ||
if (vcapAppRaw == null || vcapAppRaw.isEmpty()) { | ||
return Resource.empty(); | ||
} | ||
CloudFoundryAttributesBuilder attributes = new CloudFoundryAttributesBuilder(vcapAppRaw); | ||
attributes | ||
.putString(CLOUDFOUNDRY_APP_ID, "application_id") | ||
.putString(CLOUDFOUNDRY_APP_NAME, "application_name") | ||
.putNumberAsString(CLOUDFOUNDRY_APP_INSTANCE_ID, "instance_index") | ||
.putString(CLOUDFOUNDRY_ORG_ID, "organization_id") | ||
.putString(CLOUDFOUNDRY_ORG_NAME, "organization_name") | ||
.putString(CLOUDFOUNDRY_PROCESS_ID, "process_id") | ||
.putString(CLOUDFOUNDRY_PROCESS_TYPE, "process_type") | ||
.putString(CLOUDFOUNDRY_SPACE_ID, "space_id") | ||
.putString(CLOUDFOUNDRY_SPACE_NAME, "space_name"); | ||
return Resource.create(attributes.build(), SchemaUrls.V1_24_0); | ||
} | ||
|
||
private static class CloudFoundryAttributesBuilder { | ||
private final AttributesBuilder builder = Attributes.builder(); | ||
private Map<String, Object> parsedData; | ||
KarstenSchnitter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@SuppressWarnings("unchecked") | ||
private CloudFoundryAttributesBuilder(String rawData) { | ||
Load load = new Load(LoadSettings.builder().build()); | ||
try { | ||
this.parsedData = (Map<String, Object>) load.loadFromString(rawData); | ||
} catch (ClassCastException ex) { | ||
this.parsedData = Collections.emptyMap(); | ||
} | ||
} | ||
|
||
@CanIgnoreReturnValue | ||
private CloudFoundryAttributesBuilder putString(AttributeKey<String> key, String name) { | ||
Object value = parsedData.get(name); | ||
if (value instanceof String) { | ||
builder.put(key, (String) value); | ||
} | ||
return this; | ||
} | ||
|
||
@CanIgnoreReturnValue | ||
private CloudFoundryAttributesBuilder putNumberAsString(AttributeKey<String> key, String name) { | ||
Object value = parsedData.get(name); | ||
if (value instanceof Number) { | ||
builder.put(key, value.toString()); | ||
} | ||
return this; | ||
} | ||
breedx-splk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private Attributes build() { | ||
return builder.build(); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...emetry/javaagent/instrumentation/cloudfoundry/resources/CloudFoundryResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.cloudfoundry.resources; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
|
||
@AutoService(ResourceProvider.class) | ||
public class CloudFoundryResourceProvider implements ResourceProvider { | ||
|
||
@Override | ||
public Resource createResource(ConfigProperties configProperties) { | ||
return CloudFoundryResource.get(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...ntelemetry/javaagent/instrumentation/cloudfoundry/resources/CloudFoundryResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.cloudfoundry.resources; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.semconv.SchemaUrls; | ||
import io.opentelemetry.semconv.incubating.CloudfoundryIncubatingAttributes; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CloudFoundryResourceTest { | ||
|
||
private static final String FULL_VCAP_APPLICATION = | ||
"{" | ||
+ "\"application_id\":\"0193a038-e615-7e5e-92ca-f4bcd7ba0a25\"," | ||
+ "\"application_name\":\"cf-app-name\"," | ||
+ "\"instance_index\":1," | ||
+ "\"organization_id\":\"0193a375-8d8e-7e0c-a832-01ce9ded40dc\"," | ||
+ "\"organization_name\":\"cf-org-name\"," | ||
+ "\"process_id\":\"0193a4e3-8fd3-71b9-9fe3-5640c53bf1e2\"," | ||
+ "\"process_type\":\"web\"," | ||
+ "\"space_id\":\"0193a7e7-da17-7ea4-8940-b1e07b401b16\"," | ||
+ "\"space_name\":\"cf-space-name\"}"; | ||
|
||
@Test | ||
void noVcapApplication() { | ||
Map<String, String> env = Collections.emptyMap(); | ||
Resource resource = CloudFoundryResource.buildResource(env::get); | ||
assertThat(resource).isEqualTo(Resource.empty()); | ||
} | ||
|
||
@Test | ||
void emptyVcapApplication() { | ||
Map<String, String> env = ImmutableMap.of("VCAP_APPLICATION", ""); | ||
Resource resource = CloudFoundryResource.buildResource(env::get); | ||
assertThat(resource).isEqualTo(Resource.empty()); | ||
} | ||
|
||
@Test | ||
void fullVcapApplication() { | ||
Map<String, String> env = ImmutableMap.of("VCAP_APPLICATION", FULL_VCAP_APPLICATION); | ||
|
||
Resource resource = CloudFoundryResource.buildResource(env::get); | ||
|
||
assertThat(resource.getSchemaUrl()).isEqualTo(SchemaUrls.V1_24_0); | ||
assertThat(resource.getAttribute(CloudfoundryIncubatingAttributes.CLOUDFOUNDRY_APP_ID)) | ||
.isEqualTo("0193a038-e615-7e5e-92ca-f4bcd7ba0a25"); | ||
assertThat(resource.getAttribute(CloudfoundryIncubatingAttributes.CLOUDFOUNDRY_APP_INSTANCE_ID)) | ||
.isEqualTo("1"); | ||
assertThat(resource.getAttribute(CloudfoundryIncubatingAttributes.CLOUDFOUNDRY_APP_NAME)) | ||
.isEqualTo("cf-app-name"); | ||
assertThat(resource.getAttribute(CloudfoundryIncubatingAttributes.CLOUDFOUNDRY_ORG_ID)) | ||
.isEqualTo("0193a375-8d8e-7e0c-a832-01ce9ded40dc"); | ||
assertThat(resource.getAttribute(CloudfoundryIncubatingAttributes.CLOUDFOUNDRY_ORG_NAME)) | ||
.isEqualTo("cf-org-name"); | ||
assertThat(resource.getAttribute(CloudfoundryIncubatingAttributes.CLOUDFOUNDRY_PROCESS_ID)) | ||
.isEqualTo("0193a4e3-8fd3-71b9-9fe3-5640c53bf1e2"); | ||
assertThat(resource.getAttribute(CloudfoundryIncubatingAttributes.CLOUDFOUNDRY_PROCESS_TYPE)) | ||
.isEqualTo("web"); | ||
assertThat(resource.getAttribute(CloudfoundryIncubatingAttributes.CLOUDFOUNDRY_SPACE_ID)) | ||
.isEqualTo("0193a7e7-da17-7ea4-8940-b1e07b401b16"); | ||
assertThat(resource.getAttribute(CloudfoundryIncubatingAttributes.CLOUDFOUNDRY_SPACE_NAME)) | ||
.isEqualTo("cf-space-name"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I would name the variable
builder
instead ofattributes
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to strong feelings about that. My thoughts were to make it clear, that only the attributes are being built, not the entire resource.