Skip to content

Commit 9116d27

Browse files
supports opentelemetry
Co-authored-by: zhuxiaolong37 <[email protected]>
1 parent 8ac9d08 commit 9116d27

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.aliyun.oss</groupId>
66
<artifactId>aliyun-sdk-oss</artifactId>
7-
<version>3.18.0</version>
7+
<version>3.18.1-SNAPSHOT</version>
88
<packaging>jar</packaging>
99
<name>Aliyun OSS SDK for Java</name>
1010
<description>The Aliyun OSS SDK for Java used for accessing Aliyun Object Storage Service</description>
@@ -65,6 +65,11 @@
6565
<version>0.1.0</version>
6666
<scope>test</scope>
6767
</dependency>
68+
<dependency>
69+
<groupId>com.aliyun</groupId>
70+
<artifactId>java-trace-api</artifactId>
71+
<version>0.2.11-beta</version>
72+
</dependency>
6873
</dependencies>
6974

7075
<reporting>

src/main/java/com/aliyun/oss/ClientConfiguration.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public class ClientConfiguration {
125125

126126
private boolean verifyObjectStrict = true;
127127

128+
private boolean tracerEnabled = false;
129+
128130
public ClientConfiguration() {
129131
super();
130132
AppendDefaultExcludeList(this.cnameExcludeList);
@@ -971,5 +973,22 @@ public boolean isVerifyObjectStrict() {
971973
return verifyObjectStrict;
972974
}
973975

976+
/**
977+
* Gets the flag of tracer.
978+
*
979+
* @return True if it's enabled; False if it's disabled.
980+
*/
981+
public boolean isTracerEnabled() {
982+
return tracerEnabled;
983+
}
974984

985+
/**
986+
* Sets the flag of tracer.
987+
*
988+
* @param enabled
989+
* True if it's enabled; False if it's disabled.
990+
*/
991+
public void setTracerEnabled(boolean enabled) {
992+
this.tracerEnabled = enabled;
993+
}
975994
}

src/main/java/com/aliyun/oss/internal/OSSOperation.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.URI;
2828
import java.util.List;
2929

30+
import com.aliyun.core.tracing.*;
3031
import com.aliyun.oss.*;
3132
import com.aliyun.oss.common.auth.Credentials;
3233
import com.aliyun.oss.common.auth.CredentialsProvider;
@@ -40,6 +41,8 @@
4041
import com.aliyun.oss.internal.signer.OSSSignerBase;
4142
import com.aliyun.oss.internal.signer.OSSSignerParams;
4243
import com.aliyun.oss.model.WebServiceRequest;
44+
import io.opentelemetry.api.common.Attributes;
45+
import io.opentelemetry.api.trace.Span;
4346

4447
/**
4548
* Abstract base class that provides some common functionalities for OSS
@@ -157,6 +160,23 @@ protected <T> T doOperation(RequestMessage request, ResponseParser<T> parser, St
157160
boolean keepResponseOpen, List<RequestHandler> requestHandlers, List<ResponseHandler> reponseHandlers)
158161
throws OSSException, ClientException {
159162

163+
Span span = null;
164+
if (client.getClientConfiguration().isTracerEnabled()){
165+
AlibabaCloudTracer alibabaCloudTracer = AlibabaCloudTracerProvider.getInstance().getTracer();
166+
AlibabaCloudAttributesBuilder AttributesBuilder = new AlibabaCloudAttributesBuilder();
167+
Attributes attributes = AttributesBuilder.getAttributesBuilder()
168+
.put("alibaba.cloud.service", getProduct())
169+
.put("alibaba.cloud.signature_version", client.getClientConfiguration().getSignatureVersion().toString())
170+
.put("alibaba.cloud.resource.type", "OSS")
171+
.put("http.request.method", request.getMethod().name())
172+
.build();
173+
174+
AlibabaCloudSpanBuilder alibabaCloudSpanBuilder = alibabaCloudTracer.spanBuilder(bucketName);
175+
if (alibabaCloudSpanBuilder != null) {
176+
span = alibabaCloudSpanBuilder.setAllAttributes(attributes).startSpan();
177+
}
178+
}
179+
160180
final WebServiceRequest originalRequest = request.getOriginalRequest();
161181
request.getHeaders().putAll(client.getClientConfiguration().getDefaultHeaders());
162182
request.getHeaders().putAll(originalRequest.getHeaders());
@@ -189,15 +209,37 @@ protected <T> T doOperation(RequestMessage request, ResponseParser<T> parser, St
189209
}
190210
}
191211

192-
ResponseMessage response = send(request, context, keepResponseOpen);
193-
212+
ResponseMessage response = null;
194213
try {
214+
response = send(request, context, keepResponseOpen);
195215
return parser.parse(response);
196216
} catch (ResponseParseException rpe) {
197217
OSSException oe = ExceptionFactory.createInvalidResponseException(response.getRequestId(), rpe.getMessage(),
198218
rpe);
199219
logException("Unable to parse response error: ", rpe);
220+
if (span != null) {
221+
span.setAttribute("alibaba.cloud.error.code", "ParseResponseError");
222+
}
200223
throw oe;
224+
} catch (OSSException e) {
225+
if (span != null) {
226+
span.setAttribute("alibaba.cloud.error.code", e.getErrorCode());
227+
span.setAttribute("alibaba.cloud.request_id", e.getRequestId());
228+
}
229+
throw e;
230+
} catch (ClientException ce) {
231+
if (span != null) {
232+
span.setAttribute("alibaba.cloud.error.code", ce.getErrorCode());
233+
}
234+
throw ce;
235+
} finally {
236+
if (span != null) {
237+
if (response != null) {
238+
span.setAttribute("alibaba.cloud.request_id", response.getRequestId());
239+
span.setAttribute("http.response.status_code", String.valueOf(response.getStatusCode()));
240+
}
241+
span.end();
242+
}
201243
}
202244
}
203245

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=3.18.0
1+
version=3.18.1-SNAPSHOT

0 commit comments

Comments
 (0)