-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathshutdown_metrics.go
More file actions
149 lines (116 loc) · 3.97 KB
/
Copy pathshutdown_metrics.go
File metadata and controls
149 lines (116 loc) · 3.97 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package rapid
import (
"fmt"
"regexp"
"sync"
"time"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda-managed-instances/interop"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda-managed-instances/rapid/model"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda-managed-instances/servicelogs"
)
const (
shutdownReasonTemplate = "ShutdownReason-%s"
)
type durationMetricTimer struct {
metricName string
startTime time.Time
metrics *shutdownMetrics
}
type durationMetric struct {
metricName string
duration time.Duration
}
func (t *durationMetricTimer) Done() {
t.metrics.mutex.Lock()
defer t.metrics.mutex.Unlock()
t.metrics.durationMetrics = append(t.metrics.durationMetrics, durationMetric{
metricName: t.metricName,
duration: t.metrics.getCurrentTime().Sub(t.startTime),
})
}
type shutdownMetrics struct {
logger servicelogs.Logger
reason model.AppError
error model.AppError
mutex sync.Mutex
props []servicelogs.Property
dims []servicelogs.Dimension
metrics []servicelogs.Metric
durationMetrics []durationMetric
startTime time.Time
internalExtensionCount int
externalExtensionCount int
killProcessDurationRegex *regexp.Regexp
getCurrentTime func() time.Time
}
func NewShutdownMetrics(logger servicelogs.Logger, reason model.AppError) *shutdownMetrics {
return &shutdownMetrics{
logger: logger,
reason: reason,
startTime: time.Now(),
getCurrentTime: time.Now,
killProcessDurationRegex: regexp.MustCompile(`^Kill.+Duration$`),
}
}
func (m *shutdownMetrics) CreateDurationMetric(name string) interop.DurationMetricTimer {
return &durationMetricTimer{
metricName: name,
startTime: m.getCurrentTime(),
metrics: m,
}
}
func (m *shutdownMetrics) AddMetric(metric servicelogs.Metric) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.metrics = append(m.metrics, metric)
}
func (m *shutdownMetrics) SetAgentCount(internal, external int) {
m.internalExtensionCount = internal
m.externalExtensionCount = external
}
func (m *shutdownMetrics) SendMetrics(err model.AppError) {
m.error = err
m.buildMetrics()
m.mutex.Lock()
defer m.mutex.Unlock()
m.logger.Log(servicelogs.ShutdownOp, m.startTime, m.props, m.dims, m.metrics)
}
func (m *shutdownMetrics) buildMetrics() {
m.mutex.Lock()
defer m.mutex.Unlock()
m.metrics = append(m.metrics,
servicelogs.Counter(interop.InternalExtensionsCountMetric, float64(m.internalExtensionCount)),
servicelogs.Counter(interop.ExternalExtensionsCountMetric, float64(m.externalExtensionCount)),
servicelogs.Counter(interop.TotalExtensionsCountMetric, float64(m.internalExtensionCount+m.externalExtensionCount)),
)
var totalDuration, sumCustomerDuration time.Duration
for _, metric := range m.durationMetrics {
switch key := metric.metricName; {
case key == interop.TotalDurationMetric:
totalDuration = metric.duration
case key == interop.ShutdownRuntimeDuration, key == interop.ShutdownExtensionsDuration, key == interop.ShutdownWaitAllProcessesDuration, key == interop.ShutdownAbortInvokesDurationMetric, m.killProcessDurationRegex.MatchString(key):
sumCustomerDuration += metric.duration
}
m.metrics = append(m.metrics, servicelogs.Timer(metric.metricName, metric.duration))
}
shutdodwnOverhead := totalDuration - sumCustomerDuration
m.metrics = append(m.metrics, servicelogs.Timer(interop.PlatformOverheadDurationMetric, shutdodwnOverhead))
if m.reason != nil {
m.metrics = append(
m.metrics,
servicelogs.Counter(fmt.Sprintf(shutdownReasonTemplate, m.reason), 1.0),
)
}
var platformErrCnt float64
if m.error != nil {
platformErrCnt = 1
m.metrics = append(m.metrics,
servicelogs.Counter(fmt.Sprintf(interop.PlatformErrorReasonTemplate, m.error.ErrorType()), 1.0),
)
}
m.metrics = append(m.metrics,
servicelogs.Counter(interop.PlatformErrorMetric, platformErrCnt),
)
}