forked from aws/aws-sdk-go-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric_collection.go
64 lines (49 loc) · 1.55 KB
/
metric_collection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// This package is designated as private and is intended for use only by the
// smithy client runtime. The exported API therein is not considered stable and
// is subject to breaking changes without notice.
package middleware
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws/middleware/private/metrics"
"github.com/aws/aws-sdk-go-v2/internal/sdk"
"github.com/aws/smithy-go/middleware"
)
type MetricCollection struct {
cc *metrics.SharedConnectionCounter
publisher metrics.MetricPublisher
}
func GetSetupMetricCollectionMiddleware(
counter *metrics.SharedConnectionCounter, publisher metrics.MetricPublisher,
) *MetricCollection {
return &MetricCollection{
cc: counter,
publisher: publisher,
}
}
func (m *MetricCollection) ID() string {
return "MetricCollection"
}
func (m *MetricCollection) HandleInitialize(
ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler,
) (
out middleware.InitializeOutput, metadata middleware.Metadata, err error,
) {
ctx = metrics.InitMetricContext(ctx, m.cc, m.publisher)
mctx := metrics.Context(ctx)
metricData := mctx.Data()
metricData.RequestStartTime = sdk.NowTime()
out, metadata, err = next.HandleInitialize(ctx, in)
metricData.RequestEndTime = sdk.NowTime()
if err == nil {
metricData.Success = 1
} else {
metricData.Success = 0
}
metricData.ComputeRequestMetrics()
publishErr := m.publisher.PostRequestMetrics(metrics.Context(ctx).Data())
if publishErr != nil {
fmt.Println("Failed to post request metrics")
}
return out, metadata, err
}