-
Notifications
You must be signed in to change notification settings - Fork 1.2k
docs: add observability guide for OpenTelemetry tracing and logging #14462
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
Draft
jinseopkim0
wants to merge
9
commits into
main
Choose a base branch
from
docs-observability-guide
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.
+124
−0
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
03665bf
docs: add observability guide for OpenTelemetry tracing and logging
jinseopkim0 a7519fd
docs: fix TraceExporter method name, remove unused import, and wrap i…
jinseopkim0 dc2965a
docs: fix method name from setApiTracerFactory to setTracerFactory
jinseopkim0 16ced2d
docs: return OpenTelemetrySdk in initializeOpenTelemetry example
jinseopkim0 ff66f7f
docs: clarify stubSettings tracerFactory and remove unused import
jinseopkim0 bcd4877
docs: use MyServiceSettings and getStubSettingsBuilder in example
jinseopkim0 63b7ca1
docs: clarify MDC propagation for trace-log correlation
jinseopkim0 666a628
docs: update logback MDC dependency name to opentelemetry-logback-mdc…
jinseopkim0 1181b3b
docs: add full maven dependency snippet for opentelemetry-logback-mdc…
jinseopkim0 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
This file contains hidden or 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,123 @@ | ||
| # Observability in Google Cloud Java Client Libraries | ||
|
|
||
| This guide explains how to enable and configure observability (tracing and logging) in Google Cloud Java client libraries using OpenTelemetry and SLF4J. | ||
|
|
||
| --- | ||
|
|
||
| ## 1. Overview | ||
|
|
||
| Google Cloud Java client libraries built on [GAX (Google API Extensions)](https://github.com/googleapis/google-cloud-java/tree/main/sdk-platform-java/gax-java) provide built-in support for observability: | ||
| - **Tracing**: Powered by [OpenTelemetry](https://opentelemetry.io/). Client calls generate spans representing both high-level logical client operations and individual RPC attempts. | ||
| - **Logging**: Client libraries emit structured diagnostic logs using [SLF4J](https://www.slf4j.org/). When OpenTelemetry is configured, logging output automatically correlates with active trace and span IDs. | ||
|
|
||
| --- | ||
|
|
||
| ## 2. Distributed Tracing with OpenTelemetry | ||
|
|
||
| ### 2.1 Dependencies | ||
|
|
||
| Add the OpenTelemetry SDK and exporters to your project. | ||
|
|
||
| #### Maven | ||
| ```xml | ||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-bom</artifactId> | ||
| <version>1.44.1</version> | ||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
| <dependencies> | ||
| <!-- OpenTelemetry SDK --> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-sdk</artifactId> | ||
| </dependency> | ||
| <!-- Google Cloud Trace Exporter (optional, for Cloud Trace) --> | ||
| <dependency> | ||
| <groupId>com.google.cloud.opentelemetry</groupId> | ||
| <artifactId>exporter-trace</artifactId> | ||
| <version>0.33.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
| ``` | ||
|
|
||
| ### 2.2 Initializing OpenTelemetry | ||
|
|
||
| Initialize the OpenTelemetry SDK and register it globally, or pass it directly to the client settings: | ||
|
|
||
| ```java | ||
| import com.google.cloud.opentelemetry.trace.TraceExporter; | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
| import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; | ||
| import java.io.IOException; | ||
|
|
||
| public static OpenTelemetrySdk initializeOpenTelemetry() throws IOException { | ||
| // Initialize TracerProvider with Cloud Trace exporter | ||
| SdkTracerProvider tracerProvider = SdkTracerProvider.builder() | ||
| .addSpanProcessor(BatchSpanProcessor.builder(TraceExporter.createWithDefaultConfiguration()).build()) | ||
| .build(); | ||
|
|
||
| return OpenTelemetrySdk.builder() | ||
| .setTracerProvider(tracerProvider) | ||
| .buildAndRegisterGlobal(); | ||
| } | ||
| ``` | ||
|
|
||
| ### 2.3 Span Hierarchy | ||
|
|
||
| When tracing is enabled, GAX instruments client calls with a hierarchy of spans: | ||
| 1. **Operation Span (Logical / Client Request Span)**: Represents the entire client call from invocation until the final response or failure, spanning across any retries. | ||
| 2. **Attempt Span (RPC Attempt Span)**: Represents each individual network RPC attempt made by the client. If retries occur, each attempt produces its own attempt span parented under the operation span. | ||
|
|
||
| --- | ||
|
|
||
| ## 3. Structured Logging & Trace Correlation | ||
|
|
||
| ### 3.1 SLF4J Binding | ||
|
|
||
| GAX and Google Cloud Java libraries use SLF4J for logging. Include an SLF4J implementation such as Logback: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>ch.qos.logback</groupId> | ||
| <artifactId>logback-classic</artifactId> | ||
| <version>1.5.16</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| ### 3.2 Correlating Logs with Cloud Trace | ||
|
|
||
| To correlate logs with Cloud Trace, ensure that active trace and span IDs are propagated to your logging framework's Mapped Diagnostic Context (MDC): | ||
| 1. Use the [OpenTelemetry Javaagent](https://opentelemetry.io/docs/zero-code/java/agent/), which automatically instruments logging frameworks. | ||
| 2. Or add the Logback MDC dependency to your project: | ||
| ```xml | ||
| <dependency> | ||
| <groupId>io.opentelemetry.instrumentation</groupId> | ||
| <artifactId>opentelemetry-logback-mdc-1.0</artifactId> | ||
| <version>2.10.0-alpha</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| Once the MDC is populated with `trace_id` and `span_id`, the Google Cloud Logging Logback appender automatically maps these to the trace and span fields in Cloud Logging, allowing you to view correlated logs and traces in the Google Cloud Console. | ||
|
|
||
| --- | ||
|
|
||
| ## 4. Disabling or Customizing Tracing | ||
|
|
||
| If you need to disable tracing or provide a custom `ApiTracerFactory`, configure your client stub settings: | ||
|
|
||
| ```java | ||
| import com.google.api.gax.tracing.NoopApiTracerFactory; | ||
|
|
||
| // Example disabling tracing via ServiceSettings builder | ||
| MyServiceSettings.Builder settingsBuilder = MyServiceSettings.newBuilder(); | ||
| settingsBuilder.getStubSettingsBuilder().setTracerFactory(NoopApiTracerFactory.getInstance()); | ||
| MyServiceSettings settings = settingsBuilder.build(); | ||
|
jinseopkim0 marked this conversation as resolved.
|
||
| ``` | ||
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.