File: metrics.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.24.1-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 554,032 kB
  • sloc: java: 15,941; makefile: 419; sh: 175
file content (319 lines) | stat: -rw-r--r-- 11,385 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Package metrics implements metrics gathering for SDK development purposes.
//
// This package is designated as private and is intended for use only by the
// AWS client runtime. The exported API therein is not considered stable and
// is subject to breaking changes without notice.
package metrics

import (
	"context"
	"encoding/json"
	"fmt"
	"sync"
	"time"

	"github.com/aws/smithy-go/middleware"
)

const (
	// ServiceIDKey is the key for the service ID metric.
	ServiceIDKey = "ServiceId"
	// OperationNameKey is the key for the operation name metric.
	OperationNameKey = "OperationName"
	// ClientRequestIDKey is the key for the client request ID metric.
	ClientRequestIDKey = "ClientRequestId"
	// APICallDurationKey is the key for the API call duration metric.
	APICallDurationKey = "ApiCallDuration"
	// APICallSuccessfulKey is the key for the API call successful metric.
	APICallSuccessfulKey = "ApiCallSuccessful"
	// MarshallingDurationKey is the key for the marshalling duration metric.
	MarshallingDurationKey = "MarshallingDuration"
	// InThroughputKey is the key for the input throughput metric.
	InThroughputKey = "InThroughput"
	// OutThroughputKey is the key for the output throughput metric.
	OutThroughputKey = "OutThroughput"
	// RetryCountKey is the key for the retry count metric.
	RetryCountKey = "RetryCount"
	// HTTPStatusCodeKey is the key for the HTTP status code metric.
	HTTPStatusCodeKey = "HttpStatusCode"
	// AWSExtendedRequestIDKey is the key for the AWS extended request ID metric.
	AWSExtendedRequestIDKey = "AwsExtendedRequestId"
	// AWSRequestIDKey is the key for the AWS request ID metric.
	AWSRequestIDKey = "AwsRequestId"
	// BackoffDelayDurationKey is the key for the backoff delay duration metric.
	BackoffDelayDurationKey = "BackoffDelayDuration"
	// StreamThroughputKey is the key for the stream throughput metric.
	StreamThroughputKey = "Throughput"
	// ConcurrencyAcquireDurationKey is the key for the concurrency acquire duration metric.
	ConcurrencyAcquireDurationKey = "ConcurrencyAcquireDuration"
	// PendingConcurrencyAcquiresKey is the key for the pending concurrency acquires metric.
	PendingConcurrencyAcquiresKey = "PendingConcurrencyAcquires"
	// SigningDurationKey is the key for the signing duration metric.
	SigningDurationKey = "SigningDuration"
	// UnmarshallingDurationKey is the key for the unmarshalling duration metric.
	UnmarshallingDurationKey = "UnmarshallingDuration"
	// TimeToFirstByteKey is the key for the time to first byte metric.
	TimeToFirstByteKey = "TimeToFirstByte"
	// ServiceCallDurationKey is the key for the service call duration metric.
	ServiceCallDurationKey = "ServiceCallDuration"
	// EndpointResolutionDurationKey is the key for the endpoint resolution duration metric.
	EndpointResolutionDurationKey = "EndpointResolutionDuration"
	// AttemptNumberKey is the key for the attempt number metric.
	AttemptNumberKey = "AttemptNumber"
	// MaxConcurrencyKey is the key for the max concurrency metric.
	MaxConcurrencyKey = "MaxConcurrency"
	// AvailableConcurrencyKey is the key for the available concurrency metric.
	AvailableConcurrencyKey = "AvailableConcurrency"
)

// MetricPublisher provides the interface to provide custom MetricPublishers.
// PostRequestMetrics will be invoked by the MetricCollection middleware to post request.
// PostStreamMetrics will be invoked by ReadCloserWithMetrics to post stream metrics.
type MetricPublisher interface {
	PostRequestMetrics(*MetricData) error
	PostStreamMetrics(*MetricData) error
}

// Serializer provides the interface to provide custom Serializers.
// Serialize will transform any input object in its corresponding string representation.
type Serializer interface {
	Serialize(obj interface{}) (string, error)
}

// DefaultSerializer is an implementation of the Serializer interface.
type DefaultSerializer struct{}

// Serialize uses the default JSON serializer to obtain the string representation of an object.
func (DefaultSerializer) Serialize(obj interface{}) (string, error) {
	bytes, err := json.Marshal(obj)
	if err != nil {
		return "", err
	}
	return string(bytes), nil
}

type metricContextKey struct{}

// MetricContext contains fields to store metric-related information.
type MetricContext struct {
	connectionCounter *SharedConnectionCounter
	publisher         MetricPublisher
	data              *MetricData
}

// MetricData stores the collected metric data.
type MetricData struct {
	RequestStartTime           time.Time
	RequestEndTime             time.Time
	APICallDuration            time.Duration
	SerializeStartTime         time.Time
	SerializeEndTime           time.Time
	MarshallingDuration        time.Duration
	ResolveEndpointStartTime   time.Time
	ResolveEndpointEndTime     time.Time
	EndpointResolutionDuration time.Duration
	InThroughput               float64
	OutThroughput              float64
	RetryCount                 int
	Success                    uint8
	StatusCode                 int
	ClientRequestID            string
	ServiceID                  string
	OperationName              string
	PartitionID                string
	Region                     string
	RequestContentLength       int64
	Stream                     StreamMetrics
	Attempts                   []AttemptMetrics
}

// StreamMetrics stores metrics related to streaming data.
type StreamMetrics struct {
	ReadDuration time.Duration
	ReadBytes    int64
	Throughput   float64
}

// AttemptMetrics stores metrics related to individual attempts.
type AttemptMetrics struct {
	ServiceCallStart           time.Time
	ServiceCallEnd             time.Time
	ServiceCallDuration        time.Duration
	FirstByteTime              time.Time
	TimeToFirstByte            time.Duration
	ConnRequestedTime          time.Time
	ConnObtainedTime           time.Time
	ConcurrencyAcquireDuration time.Duration
	CredentialFetchStartTime   time.Time
	CredentialFetchEndTime     time.Time
	SignStartTime              time.Time
	SignEndTime                time.Time
	SigningDuration            time.Duration
	DeserializeStartTime       time.Time
	DeserializeEndTime         time.Time
	UnMarshallingDuration      time.Duration
	RetryDelay                 time.Duration
	ResponseContentLength      int64
	StatusCode                 int
	RequestID                  string
	ExtendedRequestID          string
	HTTPClient                 string
	MaxConcurrency             int
	PendingConnectionAcquires  int
	AvailableConcurrency       int
	ActiveRequests             int
	ReusedConnection           bool
}

// Data returns the MetricData associated with the MetricContext.
func (mc *MetricContext) Data() *MetricData {
	return mc.data
}

// ConnectionCounter returns the SharedConnectionCounter associated with the MetricContext.
func (mc *MetricContext) ConnectionCounter() *SharedConnectionCounter {
	return mc.connectionCounter
}

// Publisher returns the MetricPublisher associated with the MetricContext.
func (mc *MetricContext) Publisher() MetricPublisher {
	return mc.publisher
}

// ComputeRequestMetrics calculates and populates derived metrics based on the collected data.
func (md *MetricData) ComputeRequestMetrics() {

	for idx := range md.Attempts {
		attempt := &md.Attempts[idx]
		attempt.ConcurrencyAcquireDuration = attempt.ConnObtainedTime.Sub(attempt.ConnRequestedTime)
		attempt.SigningDuration = attempt.SignEndTime.Sub(attempt.SignStartTime)
		attempt.UnMarshallingDuration = attempt.DeserializeEndTime.Sub(attempt.DeserializeStartTime)
		attempt.TimeToFirstByte = attempt.FirstByteTime.Sub(attempt.ServiceCallStart)
		attempt.ServiceCallDuration = attempt.ServiceCallEnd.Sub(attempt.ServiceCallStart)
	}

	md.APICallDuration = md.RequestEndTime.Sub(md.RequestStartTime)
	md.MarshallingDuration = md.SerializeEndTime.Sub(md.SerializeStartTime)
	md.EndpointResolutionDuration = md.ResolveEndpointEndTime.Sub(md.ResolveEndpointStartTime)

	md.RetryCount = len(md.Attempts) - 1

	latestAttempt, err := md.LatestAttempt()

	if err != nil {
		fmt.Printf("error retrieving attempts data due to: %s. Skipping Throughput metrics", err.Error())
	} else {

		md.StatusCode = latestAttempt.StatusCode

		if md.Success == 1 {
			if latestAttempt.ResponseContentLength > 0 && latestAttempt.ServiceCallDuration > 0 {
				md.InThroughput = float64(latestAttempt.ResponseContentLength) / latestAttempt.ServiceCallDuration.Seconds()
			}
			if md.RequestContentLength > 0 && latestAttempt.ServiceCallDuration > 0 {
				md.OutThroughput = float64(md.RequestContentLength) / latestAttempt.ServiceCallDuration.Seconds()
			}
		}
	}
}

// LatestAttempt returns the latest attempt metrics.
// It returns an error if no attempts are initialized.
func (md *MetricData) LatestAttempt() (*AttemptMetrics, error) {
	if md.Attempts == nil || len(md.Attempts) == 0 {
		return nil, fmt.Errorf("no attempts initialized. NewAttempt() should be called first")
	}
	return &md.Attempts[len(md.Attempts)-1], nil
}

// NewAttempt initializes new attempt metrics.
func (md *MetricData) NewAttempt() {
	if md.Attempts == nil {
		md.Attempts = []AttemptMetrics{}
	}
	md.Attempts = append(md.Attempts, AttemptMetrics{})
}

// SharedConnectionCounter is a counter shared across API calls.
type SharedConnectionCounter struct {
	mu sync.Mutex

	activeRequests           int
	pendingConnectionAcquire int
}

// ActiveRequests returns the count of active requests.
func (cc *SharedConnectionCounter) ActiveRequests() int {
	cc.mu.Lock()
	defer cc.mu.Unlock()

	return cc.activeRequests
}

// PendingConnectionAcquire returns the count of pending connection acquires.
func (cc *SharedConnectionCounter) PendingConnectionAcquire() int {
	cc.mu.Lock()
	defer cc.mu.Unlock()

	return cc.pendingConnectionAcquire
}

// AddActiveRequest increments the count of active requests.
func (cc *SharedConnectionCounter) AddActiveRequest() {
	cc.mu.Lock()
	defer cc.mu.Unlock()

	cc.activeRequests++
}

// RemoveActiveRequest decrements the count of active requests.
func (cc *SharedConnectionCounter) RemoveActiveRequest() {
	cc.mu.Lock()
	defer cc.mu.Unlock()

	cc.activeRequests--
}

// AddPendingConnectionAcquire increments the count of pending connection acquires.
func (cc *SharedConnectionCounter) AddPendingConnectionAcquire() {
	cc.mu.Lock()
	defer cc.mu.Unlock()

	cc.pendingConnectionAcquire++
}

// RemovePendingConnectionAcquire decrements the count of pending connection acquires.
func (cc *SharedConnectionCounter) RemovePendingConnectionAcquire() {
	cc.mu.Lock()
	defer cc.mu.Unlock()

	cc.pendingConnectionAcquire--
}

// InitMetricContext initializes the metric context with the provided counter and publisher.
// It returns the updated context.
func InitMetricContext(
	ctx context.Context, counter *SharedConnectionCounter, publisher MetricPublisher,
) context.Context {
	if middleware.GetStackValue(ctx, metricContextKey{}) == nil {
		ctx = middleware.WithStackValue(ctx, metricContextKey{}, &MetricContext{
			connectionCounter: counter,
			publisher:         publisher,
			data: &MetricData{
				Attempts: []AttemptMetrics{},
				Stream:   StreamMetrics{},
			},
		})
	}
	return ctx
}

// Context returns the metric context from the given context.
// It returns nil if the metric context is not found.
func Context(ctx context.Context) *MetricContext {
	mctx := middleware.GetStackValue(ctx, metricContextKey{})
	if mctx == nil {
		return nil
	}
	return mctx.(*MetricContext)
}