|
27 | 27 | import java.net.URI; |
28 | 28 | import java.util.List; |
29 | 29 |
|
| 30 | +import com.aliyun.core.tracing.*; |
30 | 31 | import com.aliyun.oss.*; |
31 | 32 | import com.aliyun.oss.common.auth.Credentials; |
32 | 33 | import com.aliyun.oss.common.auth.CredentialsProvider; |
|
40 | 41 | import com.aliyun.oss.internal.signer.OSSSignerBase; |
41 | 42 | import com.aliyun.oss.internal.signer.OSSSignerParams; |
42 | 43 | import com.aliyun.oss.model.WebServiceRequest; |
| 44 | +import io.opentelemetry.api.common.Attributes; |
| 45 | +import io.opentelemetry.api.trace.Span; |
43 | 46 |
|
44 | 47 | /** |
45 | 48 | * Abstract base class that provides some common functionalities for OSS |
@@ -157,6 +160,23 @@ protected <T> T doOperation(RequestMessage request, ResponseParser<T> parser, St |
157 | 160 | boolean keepResponseOpen, List<RequestHandler> requestHandlers, List<ResponseHandler> reponseHandlers) |
158 | 161 | throws OSSException, ClientException { |
159 | 162 |
|
| 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 | + |
160 | 180 | final WebServiceRequest originalRequest = request.getOriginalRequest(); |
161 | 181 | request.getHeaders().putAll(client.getClientConfiguration().getDefaultHeaders()); |
162 | 182 | request.getHeaders().putAll(originalRequest.getHeaders()); |
@@ -189,15 +209,37 @@ protected <T> T doOperation(RequestMessage request, ResponseParser<T> parser, St |
189 | 209 | } |
190 | 210 | } |
191 | 211 |
|
192 | | - ResponseMessage response = send(request, context, keepResponseOpen); |
193 | | - |
| 212 | + ResponseMessage response = null; |
194 | 213 | try { |
| 214 | + response = send(request, context, keepResponseOpen); |
195 | 215 | return parser.parse(response); |
196 | 216 | } catch (ResponseParseException rpe) { |
197 | 217 | OSSException oe = ExceptionFactory.createInvalidResponseException(response.getRequestId(), rpe.getMessage(), |
198 | 218 | rpe); |
199 | 219 | logException("Unable to parse response error: ", rpe); |
| 220 | + if (span != null) { |
| 221 | + span.setAttribute("alibaba.cloud.error.code", "ParseResponseError"); |
| 222 | + } |
200 | 223 | 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 | + } |
201 | 243 | } |
202 | 244 | } |
203 | 245 |
|
|
0 commit comments