-
Notifications
You must be signed in to change notification settings - Fork 72
feat(observability): introduce minimal tracing implementation #4105
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
base: main
Are you sure you want to change the base?
Changes from all commits
7082247
bfe6a6d
ea6cb18
97bec08
097f701
f547e5a
b7b9e31
556c84c
0359b7d
402cb89
420278b
6de9fb6
a61dacf
b000d52
9ce0c34
9c6c737
e77de58
8f07f61
2cfcd68
2415c6b
0f5e9b5
0c3bd22
bdeb291
5c07a3c
ff1d45d
043ce62
dbc5f41
b4bce0d
6e95cf4
e873904
68943b0
cd4d4d0
5b76e40
c783d68
3e06b39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
diegomarquezp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| package com.google.api.gax.tracing; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
| import com.google.auto.value.AutoValue; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * A context object that contains information used to infer attributes that are common for all | ||
| * {@link ApiTracer}s. | ||
| * | ||
| * <p>For internal use only. | ||
| */ | ||
| @InternalApi | ||
| @AutoValue | ||
| public abstract class ApiTracerContext { | ||
|
|
||
| @Nullable | ||
| public abstract String getServerAddress(); | ||
|
|
||
| public static Builder newBuilder() { | ||
| return new AutoValue_ApiTracerContext.Builder(); | ||
| } | ||
|
|
||
| @AutoValue.Builder | ||
| public abstract static class Builder { | ||
| public abstract Builder setServerAddress(String serverAddress); | ||
|
|
||
| public abstract ApiTracerContext build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| package com.google.api.gax.tracing; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Utility class for providing common attributes used in app-centric observability. | ||
| * | ||
| * <p>This class extracts information from {@link ApiTracerContext} and maps it to standardized | ||
| * attribute keys that are expected by {@link ApiTracerFactory} implementations that conform to | ||
| * app-centric observability | ||
| * | ||
| * <p>For internal use only. | ||
| */ | ||
| @InternalApi | ||
| public class AppCentricAttributes { | ||
| /** The address of the server being called (e.g., "pubsub.googleapis.com"). */ | ||
| public static final String SERVER_ADDRESS_ATTRIBUTE = "server.address"; | ||
|
|
||
| /** | ||
| * Extracts attempt-level attributes from the provided {@link ApiTracerContext}. | ||
| * | ||
| * @param context the context containing information about the current API call | ||
| * @return a map of attributes to be included in attempt-level spans | ||
| */ | ||
| public static Map<String, String> getAttemptAttributes(ApiTracerContext context) { | ||
| Map<String, String> attributes = new HashMap<>(); | ||
| if (context.getServerAddress() != null) { | ||
| attributes.put(SERVER_ADDRESS_ATTRIBUTE, context.getServerAddress()); | ||
| } | ||
| return attributes; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
diegomarquezp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| package com.google.api.gax.tracing; | ||
|
|
||
| import com.google.api.core.BetaApi; | ||
| import com.google.api.core.InternalApi; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * An implementation of {@link ApiTracer} that uses a {@link TraceRecorder} to record traces. This | ||
| * implementation is agnostic to the specific {@link TraceRecorder} in order to allow extensions | ||
| * that interact with other backends. | ||
| */ | ||
| @BetaApi | ||
| @InternalApi | ||
| public class AppCentricTracer implements ApiTracer { | ||
| public static final String LANGUAGE_ATTRIBUTE = "gcp.client.language"; | ||
diegomarquezp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public static final String DEFAULT_LANGUAGE = "Java"; | ||
|
|
||
| private final TraceRecorder recorder; | ||
| private final Map<String, String> attemptAttributes; | ||
| private final String attemptSpanName; | ||
| private TraceRecorder.TraceSpan attemptHandle; | ||
|
|
||
| /** | ||
| * Creates a new instance of {@code AppCentricTracer}. | ||
| * | ||
| * @param recorder the {@link TraceRecorder} to use for recording spans | ||
| * @param attemptSpanName the name of the individual attempt spans | ||
| * @param attemptAttributes attributes to be added to each attempt span | ||
| */ | ||
| public AppCentricTracer( | ||
| TraceRecorder recorder, String attemptSpanName, Map<String, String> attemptAttributes) { | ||
| this.recorder = recorder; | ||
| this.attemptSpanName = attemptSpanName; | ||
| this.attemptAttributes = new HashMap<>(attemptAttributes); | ||
| this.attemptAttributes.put(LANGUAGE_ATTRIBUTE, DEFAULT_LANGUAGE); | ||
|
|
||
| // Start the long-lived operation span. | ||
| } | ||
|
|
||
| @Override | ||
| public void attemptStarted(Object request, int attemptNumber) { | ||
| Map<String, String> attemptAttributes = new HashMap<>(this.attemptAttributes); | ||
| // Start the specific attempt span with the operation span as parent | ||
| this.attemptHandle = recorder.createSpan(attemptSpanName, attemptAttributes); | ||
| } | ||
|
|
||
| @Override | ||
| public void attemptSucceeded() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are other scenarios we want to end span as well. For example,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed these in e77de58. I'm planning to introduce them as we implement error codes (I think attempt cancelled is a cancelled status code). |
||
| endAttempt(); | ||
| } | ||
|
|
||
| private void endAttempt() { | ||
| if (attemptHandle != null) { | ||
| attemptHandle.end(); | ||
| attemptHandle = null; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.