|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +package com.google.api.gax.tracing; |
| 32 | + |
| 33 | +import com.google.api.core.BetaApi; |
| 34 | +import com.google.api.core.InternalApi; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +/** |
| 39 | + * An implementation of {@link ApiTracer} that uses a {@link TraceManager} to record traces. This |
| 40 | + * implementation is agnostic to the specific {@link TraceManager} in order to allow extensions that |
| 41 | + * interact with other backends. |
| 42 | + */ |
| 43 | +@BetaApi |
| 44 | +@InternalApi |
| 45 | +public class AppCentricTracer implements ApiTracer { |
| 46 | + public static final String LANGUAGE_ATTRIBUTE = "gcp.client.language"; |
| 47 | + |
| 48 | + public static final String DEFAULT_LANGUAGE = "Java"; |
| 49 | + |
| 50 | + private final TraceManager traceManager; |
| 51 | + private final Map<String, String> attemptAttributes; |
| 52 | + private final String attemptSpanName; |
| 53 | + private final ApiTracerContext apiTracerContext; |
| 54 | + private TraceManager.Span attemptHandle; |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates a new instance of {@code AppCentricTracer}. |
| 58 | + * |
| 59 | + * @param traceManager the {@link TraceManager} to use for recording spans |
| 60 | + * @param attemptSpanName the name of the individual attempt spans |
| 61 | + */ |
| 62 | + public AppCentricTracer( |
| 63 | + TraceManager traceManager, ApiTracerContext apiTracerContext, String attemptSpanName) { |
| 64 | + this.traceManager = traceManager; |
| 65 | + this.attemptSpanName = attemptSpanName; |
| 66 | + this.apiTracerContext = apiTracerContext; |
| 67 | + this.attemptAttributes = new HashMap<>(); |
| 68 | + buildAttributes(); |
| 69 | + } |
| 70 | + |
| 71 | + private void buildAttributes() { |
| 72 | + this.attemptAttributes.put(LANGUAGE_ATTRIBUTE, DEFAULT_LANGUAGE); |
| 73 | + this.attemptAttributes.putAll(this.apiTracerContext.getAttemptAttributes()); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void attemptStarted(Object request, int attemptNumber) { |
| 78 | + Map<String, String> attemptAttributes = new HashMap<>(this.attemptAttributes); |
| 79 | + // Start the specific attempt span with the operation span as parent |
| 80 | + this.attemptHandle = traceManager.createSpan(attemptSpanName, attemptAttributes); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void attemptSucceeded() { |
| 85 | + endAttempt(); |
| 86 | + } |
| 87 | + |
| 88 | + private void endAttempt() { |
| 89 | + if (attemptHandle != null) { |
| 90 | + attemptHandle.end(); |
| 91 | + attemptHandle = null; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments