Skip to content

Commit f6116b6

Browse files
authored
Update resource detection sample (#317)
* Update autoconf example documentation * Update resource example * Update readme for resource detection sample
1 parent b9b8c09 commit f6116b6

File tree

5 files changed

+140
-13
lines changed

5 files changed

+140
-13
lines changed

examples/autoconf/README.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,56 @@
22

33
An example application that sends custom traces+metric using *only* the OpenTelemetry API. All SDK configuration is done using the autoconfiguration module.
44

5-
To spin it up on your own GKE cluster, run the following:
5+
### Prerequisites
6+
7+
##### Get Google Cloud Credentials on your machine
8+
9+
```shell
10+
gcloud auth application-default login
11+
```
12+
Executing this command will save your application credentials to default path which will depend on the type of machine -
13+
- Linux, macOS: `$HOME/.config/gcloud/application_default_credentials.json`
14+
- Windows: `%APPDATA%\gcloud\application_default_credentials.json`
15+
16+
**NOTE: This method of authentication is not recommended for production environments.**
17+
18+
Next, export the credentials to `GOOGLE_APPLICATION_CREDENTIALS` environment variable -
19+
20+
For Linux & MacOS:
21+
```shell
22+
export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/gcloud/application_default_credentials.json
23+
```
24+
25+
For Windows:
26+
```shell
27+
SET GOOGLE_APPLICATION_CREDENTIALS=%APPDATA%\gcloud\application_default_credentials.json
28+
```
29+
30+
##### Export the Google Cloud Project ID to `GOOGLE_CLOUD_PROJECT` environment variable:
31+
32+
```shell
33+
export GOOGLE_CLOUD_PROJECT="my-awesome-gcp-project-id"
634
```
7-
export GOOGLE_CLOUD_PROJECT={your-project}
835

36+
## Running in Google Kubernetes Engine
37+
38+
To spin it up on your own GKE cluster, run the following:
39+
```
940
./gradlew :examples-autoconf:jib --image="gcr.io/$GOOGLE_CLOUD_PROJECT/hello-autoconfigure-java"
1041
1142
sed s/%GOOGLE_CLOUD_PROJECT%/$GOOGLE_CLOUD_PROJECT/g \
1243
examples/autoconf/job.yaml | kubectl apply -f -
1344
```
1445

15-
This will run a batch job which synthesizes a nested trace, latency metrics and exemplars.
46+
This will run a batch job which synthesizes a nested trace, latency metrics and exemplars.
47+
48+
## Running locally on your machine
49+
50+
In case you do not want to spin up your own GKE cluster, but still want telemetry to be published to Google Cloud, you can run the example locally.
51+
52+
From the root of the repository,
53+
```shell
54+
cd examples/autoconf && gradle run
55+
```
56+
57+
This will run the sample app locally on your machine, synthesizing a nested trace, latency metrics and exemplars.

examples/autoconf/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies {
2828
runtimeOnly(libraries.opentelemetry_sdk_autoconf)
2929
runtimeOnly(libraries.opentelemetry_sdk)
3030
runtimeOnly project(':exporter-auto')
31+
// Provides resources to the autoconfiguration module
3132
runtimeOnly(libraries.opentelemetry_gcp_resources)
3233
}
3334

@@ -38,6 +39,7 @@ def autoconf_config = [
3839
'-Dotel.metrics.exporter=google_cloud_monitoring',
3940
'-Dotel.logs.exporter=none',
4041
'-Dotel.java.global-autoconfigure.enabled=true',
42+
'-Dotel.service.name=example-autoconf-service',
4143
]
4244

4345
mainClassName = 'com.google.cloud.opentelemetry.example.autoconf.AutoconfExample'

examples/resource/README.md

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,65 @@
11
# Resource detection Example
22

3-
An example application that shows what resource attributes will be detected.
3+
> [!NOTE]
4+
> Running this example does not generate any telemetry. This example merely demonstrates resource attributes detetcted by the GCP Resource Detector.
45
5-
To spin it up on your own GKE cluster, run the following:
6+
An example application that shows what resource attributes will be detected by the [GCP Resource Detector](https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/gcp-resources) and how the [Autoconfigure Resource Provider SPI](https://github.com/open-telemetry/opentelemetry-java/tree/main/sdk-extensions/autoconfigure#resource-provider-spi) automatically *attaches* the detected resource attributes to the generated telemetry.
7+
8+
### Prerequisites
9+
10+
##### Get Google Cloud Credentials on your machine
11+
12+
```shell
13+
gcloud auth application-default login
14+
```
15+
Executing this command will save your application credentials to default path which will depend on the type of machine -
16+
- Linux, macOS: `$HOME/.config/gcloud/application_default_credentials.json`
17+
- Windows: `%APPDATA%\gcloud\application_default_credentials.json`
18+
19+
**NOTE: This method of authentication is not recommended for production environments.**
20+
21+
Next, export the credentials to `GOOGLE_APPLICATION_CREDENTIALS` environment variable -
22+
23+
For Linux & MacOS:
24+
```shell
25+
export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/gcloud/application_default_credentials.json
26+
```
27+
28+
For Windows:
29+
```shell
30+
SET GOOGLE_APPLICATION_CREDENTIALS=%APPDATA%\gcloud\application_default_credentials.json
31+
```
32+
33+
##### Export the Google Cloud Project ID to `GOOGLE_CLOUD_PROJECT` environment variable:
34+
35+
```shell
36+
export GOOGLE_CLOUD_PROJECT="my-awesome-gcp-project-id"
637
```
7-
export GOOGLE_CLOUD_PROJECT={your-project}
838

39+
## Running in Google Kubernetes Engine
40+
41+
To spin it up on your own GKE cluster, run the following:
42+
```shell
943
./gradlew :examples-resource:jib --image="gcr.io/$GOOGLE_CLOUD_PROJECT/hello-resource-java"
1044

1145
sed s/%GOOGLE_CLOUD_PROJECT%/$GOOGLE_CLOUD_PROJECT/g \
1246
examples/resource/job.yaml | kubectl apply -f -
13-
```
47+
```
48+
49+
This will run the application as a GKE workload. You can view it from the `Workloads` tab under the `Resource Management` section on GKE console.
50+
51+
The generated logs can be viewed under the `Logs` tab on the `Job Details` page. These logs will show the detected resource attributes for GKE.
52+
53+
## Running the application locally
54+
55+
> [!NOTE]
56+
> Resource attributes won't be detected in unsupported environments. You can find a list of environments supported by the GCP detector [here](https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/gcp-resources).
57+
58+
You can run the application locally as well:
59+
60+
From the root of the repository,
61+
```shell
62+
cd examples/resource && gradle run
63+
```
64+
65+
The detected resource attributes would depend on the environment in which the application is run.

examples/resource/build.gradle

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,31 @@ plugins {
1919
id 'com.google.cloud.tools.jib'
2020
}
2121

22-
2322
description = 'Examples for showing resource detection in various GCP environments.'
2423

2524
dependencies {
25+
implementation(libraries.opentelemetry_api)
26+
// Detects resource attributes from various GCP environments
2627
implementation(libraries.opentelemetry_gcp_resources)
28+
// Provides resources to the autoconfiguration module
2729
implementation(libraries.opentelemetry_sdk_autoconf)
28-
implementation(libraries.opentelemetry_otlp_exporter)
2930
}
3031

3132
mainClassName = 'com.google.cloud.opentelemetry.example.resource.ResourceExample'
3233

3334
jib {
3435
from.image = 'gcr.io/distroless/java-debian10:11'
3536
containerizingMode = 'packaged'
36-
}
37+
}
38+
39+
def autoconf_config = [
40+
'-Dotel.traces.exporter=none',
41+
'-Dotel.metrics.exporter=none',
42+
'-Dotel.logs.exporter=none',
43+
'-Dotel.java.global-autoconfigure.enabled=true',
44+
'-Dotel.service.name=example-resource',
45+
]
46+
47+
application {
48+
applicationDefaultJvmArgs = autoconf_config
49+
}

examples/resource/src/main/java/com/google/cloud/opentelemetry/example/resource/ResourceExample.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,34 @@
1616
package com.google.cloud.opentelemetry.example.resource;
1717

1818
import io.opentelemetry.contrib.gcp.resource.GCPResourceProvider;
19+
import io.opentelemetry.sdk.OpenTelemetrySdk;
20+
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
1921
import io.opentelemetry.sdk.autoconfigure.ResourceConfiguration;
2022
import io.opentelemetry.sdk.resources.Resource;
23+
import io.opentelemetry.sdk.trace.SdkTracerProvider;
2124

2225
public class ResourceExample {
2326
public static void main(String[] args) {
24-
System.out.println("Detecting resource: Autoconfigure");
27+
// Get the autoconfigured OpenTelemetry SDK
28+
OpenTelemetrySdk openTelemetrySdk =
29+
AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
30+
31+
// Shows the resource attributes detected from the environment variables
32+
// and system properties.
33+
System.out.println("Detecting resource: Environment");
2534
Resource autoResource = ResourceConfiguration.createEnvironmentResource();
26-
System.out.println(autoResource.getAttributes());
35+
System.out.println(autoResource.getAttributes() + "\n");
36+
37+
// Shows the resource attributes detected by the GCP Resource Provider
2738
System.out.println("Detecting resource: hardcoded");
2839
GCPResourceProvider resourceProvider = new GCPResourceProvider();
29-
System.out.println(resourceProvider.getAttributes());
40+
System.out.println(resourceProvider.getAttributes() + "\n");
41+
42+
// Shows the attributes attached to the Resource that was set for TracerProvider
43+
// via the autoconfiguration SPI.
44+
// This works similarly for MeterProvider and LoggerProvider.
45+
System.out.println("Detecting resource: Autoconfigure");
46+
SdkTracerProvider autoConfTracerProvider = openTelemetrySdk.getSdkTracerProvider();
47+
System.out.println(autoConfTracerProvider.toString() + "\n");
3048
}
3149
}

0 commit comments

Comments
 (0)