Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package s3store
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/codes"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// The SDK owns signing, retries and key escaping. Instrument the actual HTTP
|
|
// exchange without recording the request URL, headers or S3 error body.
|
|
type tracedTransport struct {
|
|
next http.RoundTripper
|
|
tracer trace.Tracer
|
|
}
|
|
|
|
func (t tracedTransport) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
if !trace.SpanContextFromContext(r.Context()).IsValid() {
|
|
return t.next.RoundTrip(r)
|
|
}
|
|
ctx, span := t.tracer.Start(r.Context(), "s3."+r.Method, trace.WithSpanKind(trace.SpanKindClient))
|
|
res, err := t.next.RoundTrip(r.WithContext(ctx))
|
|
if err != nil {
|
|
span.SetStatus(codes.Error, "transport error")
|
|
span.End()
|
|
return nil, err
|
|
}
|
|
span.SetAttributes(attribute.Int("http.response.status_code", res.StatusCode))
|
|
if res.StatusCode >= 400 {
|
|
span.SetStatus(codes.Error, "S3 error response")
|
|
}
|
|
res.Body = &tracedBody{ReadCloser: res.Body, span: span}
|
|
return res, nil
|
|
}
|
|
|
|
type tracedBody struct {
|
|
io.ReadCloser
|
|
span trace.Span
|
|
once sync.Once
|
|
}
|
|
|
|
func (b *tracedBody) finish(err error) {
|
|
b.once.Do(func() {
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
|
b.span.SetStatus(codes.Error, "body transfer failed")
|
|
}
|
|
b.span.End()
|
|
})
|
|
}
|
|
func (b *tracedBody) Read(p []byte) (int, error) {
|
|
n, err := b.ReadCloser.Read(p)
|
|
if err != nil {
|
|
b.finish(err)
|
|
}
|
|
return n, err
|
|
}
|
|
func (b *tracedBody) Close() error {
|
|
err := b.ReadCloser.Close()
|
|
b.finish(err)
|
|
return err
|
|
}
|