forked from mtusi2026/mtusi2026lab6
Добавить наблюдаемость и проверки отказов для ЛР 6
This commit is contained in:
@@ -14,6 +14,10 @@ import (
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/smithy-go"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"mtusi2026/internal/telemetry"
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("object not found")
|
||||
@@ -57,8 +61,10 @@ func NewS3(ctx context.Context, c S3Config) (*S3, error) {
|
||||
return &S3{client: client, bucket: c.Bucket}, nil
|
||||
}
|
||||
|
||||
func (s *S3) Put(ctx context.Context, key string, body io.ReadSeeker, size int64, contentType string) error {
|
||||
_, err := s.client.PutObject(ctx, &s3.PutObjectInput{
|
||||
func (s *S3) Put(ctx context.Context, key string, body io.ReadSeeker, size int64, contentType string) (err error) {
|
||||
ctx, span := startS3(ctx, "s3.put_object", key)
|
||||
defer func() { telemetry.Finish(span, err) }()
|
||||
_, err = s.client.PutObject(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
Body: body,
|
||||
@@ -68,7 +74,9 @@ func (s *S3) Put(ctx context.Context, key string, body io.ReadSeeker, size int64
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *S3) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
func (s *S3) Get(ctx context.Context, key string) (_ io.ReadCloser, err error) {
|
||||
ctx, span := startS3(ctx, "s3.get_object", key)
|
||||
defer func() { telemetry.Finish(span, err) }()
|
||||
out, err := s.client.GetObject(ctx, &s3.GetObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
@@ -82,14 +90,28 @@ func (s *S3) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
return out.Body, nil
|
||||
}
|
||||
|
||||
func (s *S3) Delete(ctx context.Context, key string) error {
|
||||
_, err := s.client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
||||
func (s *S3) Delete(ctx context.Context, key string) (err error) {
|
||||
ctx, span := startS3(ctx, "s3.delete_object", key)
|
||||
defer func() { telemetry.Finish(span, err) }()
|
||||
_, err = s.client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func startS3(ctx context.Context, operation, key string) (context.Context, trace.Span) {
|
||||
role := "original"
|
||||
switch {
|
||||
case strings.HasPrefix(key, "staging/"):
|
||||
role = "staging"
|
||||
case strings.Contains(key, "/v1/"):
|
||||
role = "variant"
|
||||
}
|
||||
return telemetry.Tracer.Start(ctx, operation, trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(attribute.String("rpc.system", "aws-api"), attribute.String("s3.object.role", role)))
|
||||
}
|
||||
|
||||
func (s *S3) Ping(ctx context.Context) error {
|
||||
body := []byte("ok")
|
||||
if _, err := s.client.PutObject(ctx, &s3.PutObjectInput{
|
||||
|
||||
Reference in New Issue
Block a user