Skip to content

Commit

Permalink
internal: capture user-agent in metrics (#2644)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucix-aws authored May 16, 2024
1 parent c964dbd commit ef4a9a0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .changelog/33f766393bb344b9b26b55846650e8a0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "33f76639-3bb3-44b9-b26b-55846650e8a0",
"type": "feature",
"description": "Capture user agent in internal metrics.",
"modules": [
"."
]
}
1 change: 1 addition & 0 deletions aws/middleware/private/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type MetricData struct {
OperationName string
PartitionID string
Region string
UserAgent string
RequestContentLength int64
Stream StreamMetrics
Attempts []AttemptMetrics
Expand Down
3 changes: 3 additions & 0 deletions aws/middleware/private/metrics/middleware/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func WithMetricMiddlewares(
if err := timeSigning(stack); err != nil {
return err
}
if err := stack.Build.Add(&captureUserAgent{}, middleware.After); err != nil {
return err
}
return nil
}
}
29 changes: 29 additions & 0 deletions aws/middleware/private/metrics/middleware/user_agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package middleware

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws/middleware/private/metrics"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
)

type captureUserAgent struct{}

func (*captureUserAgent) ID() string { return "captureUserAgent" }

func (*captureUserAgent) HandleBuild(
ctx context.Context, in middleware.BuildInput, next middleware.BuildHandler,
) (
out middleware.BuildOutput, md middleware.Metadata, err error,
) {
r, ok := in.Request.(*smithyhttp.Request)
if !ok {
return out, md, fmt.Errorf("unexpected transport type %T", in.Request)
}

mctx := metrics.Context(ctx)
mctx.Data().UserAgent = r.Header.Get("User-Agent")
return next.HandleBuild(ctx, in)
}

0 comments on commit ef4a9a0

Please sign in to comment.