Skip to content

Commit 9e60084

Browse files
committed
feat(storage): enable App-Centric Observability (ACO) support in Otel
1 parent d08691a commit 9e60084

9 files changed

Lines changed: 1002 additions & 9 deletions

File tree

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.storage;
18+
19+
import io.opentelemetry.api.common.AttributeKey;
20+
import io.opentelemetry.api.common.Attributes;
21+
import io.opentelemetry.api.trace.Span;
22+
import io.opentelemetry.api.trace.SpanContext;
23+
import io.opentelemetry.api.trace.StatusCode;
24+
import java.util.concurrent.TimeUnit;
25+
26+
final class AcoSpan implements Span {
27+
private final Span delegate;
28+
private final String bucketName;
29+
private final OtelStorageDecorator parent;
30+
31+
AcoSpan(Span delegate, String bucketName, OtelStorageDecorator parent) {
32+
this.delegate = delegate;
33+
this.bucketName = bucketName;
34+
this.parent = parent;
35+
}
36+
37+
private void applyCacheAttributes() {
38+
if (bucketName != null && parent != null) {
39+
BucketMetadataCache.BucketMetadata md = parent.bucketMetadataCache.get(bucketName);
40+
if (md != null && !md.fetchPending) {
41+
delegate.setAttribute("gcp.resource.destination.id", md.resource);
42+
delegate.setAttribute("gcp.resource.destination.location", md.location);
43+
}
44+
}
45+
}
46+
47+
@Override
48+
public void end() {
49+
applyCacheAttributes();
50+
delegate.end();
51+
}
52+
53+
@Override
54+
public void end(long timestamp, TimeUnit unit) {
55+
applyCacheAttributes();
56+
delegate.end(timestamp, unit);
57+
}
58+
59+
@Override
60+
public Span recordException(Throwable exception) {
61+
delegate.recordException(exception);
62+
handleException(exception);
63+
return this;
64+
}
65+
66+
@Override
67+
public Span recordException(Throwable exception, Attributes attributes) {
68+
delegate.recordException(exception, attributes);
69+
handleException(exception);
70+
return this;
71+
}
72+
73+
private void handleException(Throwable exception) {
74+
if (exception instanceof StorageException && parent != null) {
75+
StorageException se = (StorageException) exception;
76+
if (se.getCode() == 404 && se.getMessage() != null) {
77+
String msg = se.getMessage().toLowerCase(java.util.Locale.US);
78+
if (msg.contains("bucket not found") || msg.contains("bucket does not exist")) {
79+
parent.bucketMetadataCache.remove(bucketName);
80+
}
81+
}
82+
}
83+
}
84+
85+
@Override
86+
public Span setAttribute(String k, String v) {
87+
delegate.setAttribute(k, v);
88+
return this;
89+
}
90+
91+
@Override
92+
public Span setAttribute(String k, long v) {
93+
delegate.setAttribute(k, v);
94+
return this;
95+
}
96+
97+
@Override
98+
public Span setAttribute(String k, double v) {
99+
delegate.setAttribute(k, v);
100+
return this;
101+
}
102+
103+
@Override
104+
public Span setAttribute(String k, boolean v) {
105+
delegate.setAttribute(k, v);
106+
return this;
107+
}
108+
109+
@Override
110+
public <T> Span setAttribute(AttributeKey<T> k, T v) {
111+
delegate.setAttribute(k, v);
112+
return this;
113+
}
114+
115+
@Override
116+
public Span addEvent(String n) {
117+
delegate.addEvent(n);
118+
return this;
119+
}
120+
121+
@Override
122+
public Span addEvent(String n, Attributes a) {
123+
delegate.addEvent(n, a);
124+
return this;
125+
}
126+
127+
@Override
128+
public Span addEvent(String n, long t, TimeUnit u) {
129+
delegate.addEvent(n, t, u);
130+
return this;
131+
}
132+
133+
@Override
134+
public Span addEvent(String n, Attributes a, long t, TimeUnit u) {
135+
delegate.addEvent(n, a, t, u);
136+
return this;
137+
}
138+
139+
@Override
140+
public Span setStatus(StatusCode c) {
141+
delegate.setStatus(c);
142+
return this;
143+
}
144+
145+
@Override
146+
public Span setStatus(StatusCode c, String d) {
147+
delegate.setStatus(c, d);
148+
return this;
149+
}
150+
151+
@Override
152+
public Span updateName(String name) {
153+
delegate.updateName(name);
154+
return this;
155+
}
156+
157+
@Override
158+
public SpanContext getSpanContext() {
159+
return delegate.getSpanContext();
160+
}
161+
162+
@Override
163+
public boolean isRecording() {
164+
return delegate.isRecording();
165+
}
166+
}

0 commit comments

Comments
 (0)