forked from mtusi2026/mtusi2026lab6
feat(storage): lab6 add PostgreSQL, Redis and S3 tracing and DB pool metrics
This commit is contained in:
@@ -3,9 +3,11 @@ package s3
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
@@ -25,10 +27,25 @@ type MinioStorage struct {
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg Config) (*MinioStorage, error) {
|
||||
base, err := minio.DefaultTransport(cfg.UseSSL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Оборачиваем базовый транспорт в otelhttp.
|
||||
// Опция WithSpanNameFormatter задаёт красивое имя спана (s3.PUT, s3.GET),
|
||||
// чтобы в Grafana не смотреть на дефолтное "HTTP PUT".
|
||||
tracedTransport := otelhttp.NewTransport(base,
|
||||
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
|
||||
return "s3." + r.Method
|
||||
}),
|
||||
)
|
||||
|
||||
client, err := minio.New(cfg.Endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
|
||||
Secure: cfg.UseSSL,
|
||||
Region: cfg.Region,
|
||||
Transport: tracedTransport,
|
||||
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
|
||||
Secure: cfg.UseSSL,
|
||||
Region: cfg.Region,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// Один span на HTTP-попытку MinIO до получения заголовков ответа.
|
||||
type tracingTransport struct {
|
||||
next http.RoundTripper
|
||||
tracer trace.Tracer
|
||||
}
|
||||
|
||||
func (t tracingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
ctx, span := t.tracer.Start(req.Context(), "s3."+req.Method, trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(attribute.String("http.request.method", req.Method)))
|
||||
defer span.End()
|
||||
|
||||
resp, err := t.next.RoundTrip(req.WithContext(ctx))
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, "ошибка соединения с S3")
|
||||
} else {
|
||||
span.SetAttributes(attribute.Int("http.response.status_code", resp.StatusCode))
|
||||
if resp.StatusCode >= 400 {
|
||||
span.SetStatus(codes.Error, http.StatusText(resp.StatusCode))
|
||||
}
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
Reference in New Issue
Block a user