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
10 changes: 1 addition & 9 deletions examples/otlpmetric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,15 @@ Executing this command will save your application credentials to default path wh
- Linux, macOS: `$HOME/.config/gcloud/application_default_credentials.json`
- Windows: `%APPDATA%\gcloud\application_default_credentials.json`

Next, set your endpoint with the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable:

```shell
export OTEL_EXPORTER_OTLP_ENDPOINT="http://your-endpoint:port"
```

Next, update [`build.gradle`](build.grade) to set the following:

```
'-Dotel.resource.attributes=gcp.project_id=<YOUR_PROJECT_ID>,
'-Dotel.exporter.otlp.headers=X-Goog-User-Project=<YOUR_QUOTA_PROJECT>',
# Optional - if you want to export using gRPC protocol
'-Dotel.exporter.otlp.protocol=grpc',
```

Finally, to run the sample from the project root:

```
cd examples/otlpmetric && gradle run
./gradlew :examples-otlpmetric:run
```
11 changes: 7 additions & 4 deletions examples/otlpmetric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,22 @@ dependencies {
implementation(libraries.opentelemetry_sdk)
implementation(libraries.opentelemetry_otlp_exporter)
implementation(libraries.opentelemetry_sdk_autoconf)
implementation(libraries.opentelemetry_gcp_resources)
implementation(libraries.google_auth)
}

// Provide headers from env variable
// export OTEL_EXPORTER_OTLP_ENDPOINT="http://path/to/yourendpoint:port"
def autoconf_config = [
'-Dotel.resource.attributes=gcp.project_id=<YOUR_PROJECT>',
'-Dotel.exporter.otlp.headers=X-Goog-User-Project=<YOUR_QUOTA_PROJECT>',
'-Dotel.exporter.otlp.endpoint=https://telemetry.googleapis.com',
'-Dotel.resource.attributes=gcp.project_id=<YOUR_PROJECT_ID>',
'-Dotel.metrics.exporter=otlp',
'-Dotel.logs.exporter=none',
'-Dotel.traces.exporter=none',
'-Dotel.service.name=otlpmetrics-example',
'-Dotel.exporter.otlp.protocol=http/protobuf',
'-Dotel.java.global-autoconfigure.enabled=true',
]

application {
mainClassName = 'com.google.cloud.opentelemetry.example.otlpmetric.OTLPMetricExample'
applicationDefaultJvmArgs = autoconf_config
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import com.google.auth.oauth2.GoogleCredentials;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter;
import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporterBuilder;
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

Expand All @@ -49,39 +49,35 @@ private static OpenTelemetrySdk setupMetricExporter() throws IOException {
}

// Modifies the metric exporter initially auto-configured using environment variables
// Note: This adds static authorization headers which are set only at initialization time.
// This will stop working after the token expires, since the token is not refreshed.
// This will invoke the header supplier function to compute the headers, which takes care of the
// refresh.
private static MetricExporter addAuthorizationHeaders(
MetricExporter exporter, GoogleCredentials credentials) {
if (exporter instanceof OtlpHttpMetricExporter) {
try {
credentials.refreshIfExpired();
OtlpHttpMetricExporterBuilder builder =
((OtlpHttpMetricExporter) exporter)
.toBuilder()
.addHeader(
"Authorization", "Bearer " + credentials.getAccessToken().getTokenValue());

return builder.build();
} catch (IOException e) {
System.out.println("error:" + e.getMessage());
}
return ((OtlpHttpMetricExporter) exporter)
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials)).build();
} else if (exporter instanceof OtlpGrpcMetricExporter) {
try {
credentials.refreshIfExpired();
OtlpGrpcMetricExporterBuilder builder =
((OtlpGrpcMetricExporter) exporter)
.toBuilder()
.addHeader(
"Authorization", "Bearer " + credentials.getAccessToken().getTokenValue());
return builder.build();
} catch (IOException e) {
throw new RuntimeException(e);
}
return ((OtlpGrpcMetricExporter) exporter)
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials)).build();
}
return exporter;
}

private static Map<String, String> getRequiredHeaderMap(GoogleCredentials credentials) {
Map<String, String> gcpHeaders = new HashMap<>();
try {
credentials.refreshIfExpired();
} catch (IOException e) {
throw new RuntimeException(e);
}
gcpHeaders.put("Authorization", "Bearer " + credentials.getAccessToken().getTokenValue());
String configuredQuotaProjectId = credentials.getQuotaProjectId();
if (configuredQuotaProjectId != null && !configuredQuotaProjectId.isEmpty()) {
gcpHeaders.put("x-goog-user-project", configuredQuotaProjectId);
}
return gcpHeaders;
}

private static void myUseCase() {
LongCounter counter =
openTelemetrySdk
Expand Down Expand Up @@ -110,7 +106,6 @@ public static void main(String[] args) throws IOException {

// Application-specific logic
myUseCase();
myUseCase();

// Flush all buffered metrics
CompletableResultCode completableResultCode = openTelemetrySdk.getSdkMeterProvider().shutdown();
Expand Down
3 changes: 2 additions & 1 deletion examples/otlptrace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ Next, export the `GOOGLE_CLOUD_PROJECT` environment variable:
# Use your GCP project ID
export GOOGLE_CLOUD_PROJECT="your-gcp-project-id"
```
This environment variable also configures the [OpenTelemetry GCP Auth Extension](https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/gcp-auth-extension) that facilitates authentication to GCP.

Finally, to run the sample from the project root:

```
cd examples/otlptrace && gradle run
./gradlew :examples-otlptrace:run
```

Running this sample will generate and export Traces to Google Cloud.
1 change: 0 additions & 1 deletion examples/otlptrace/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ dependencies {
implementation(libraries.opentelemetry_gcp_resources)
}

// Provide headers from env variable
def autoconf_config = [
'-Dotel.exporter.otlp.endpoint=https://telemetry.googleapis.com',
'-Dotel.traces.exporter=otlp',
Expand Down
Loading