-
Notifications
You must be signed in to change notification settings - Fork 25
/
status.go
69 lines (61 loc) · 2.02 KB
/
status.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
65
66
67
68
69
package internal
import (
"strings"
"go.opentelemetry.io/collector/pdata/pcommon"
flowcontrolv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/flowcontrol/check/v1"
"github.com/fluxninja/aperture/v2/pkg/metrics"
otelconsts "github.com/fluxninja/aperture/v2/pkg/otelcollector/consts"
)
// StatusesFromAttributes gets HTTP status code and Flow status from attributes.
func StatusesFromAttributes(attributes pcommon.Map) (statusCode string, flowStatus string) {
rawStatusCode, exists := attributes.Get(otelconsts.HTTPStatusCodeLabel)
if exists {
statusCode = rawStatusCode.Str()
}
rawFlowStatus, exists := attributes.Get(otelconsts.ApertureFlowStatusLabel)
if exists {
flowStatus = rawFlowStatus.Str()
}
return
}
// StatusLabelsForMetrics returns labels maps used which describe Histogram.
func StatusLabelsForMetrics(
decisionType flowcontrolv1.CheckResponse_DecisionType,
statusCode string,
flowStatus string,
) map[string]string {
return map[string]string{
metrics.DecisionTypeLabel: decisionType.String(),
metrics.StatusCodeLabel: statusCode,
metrics.FlowStatusLabel: flowStatusForMetrics(statusCode, flowStatus),
}
}
func flowStatusForMetrics(statusCode, flowStatusStr string) string {
return flowStatus(
statusCode,
flowStatusStr,
metrics.FlowStatusOK,
metrics.FlowStatusError)
}
// FlowStatusForTelemetry returns protocol independent Flow status for telemetry based on
// HTTP status code and Flow status.
func FlowStatusForTelemetry(statusCode, flowStatusStr string) string {
return flowStatus(
statusCode,
flowStatusStr,
otelconsts.ApertureFlowStatusOK,
otelconsts.ApertureFlowStatusError)
}
func flowStatus(statusCode, flowStatusStr, okStatus, errorStatus string) string {
// Checking status code this way instead of parsing int properly handles empty
// string as well.
if strings.HasPrefix(statusCode, "1") ||
strings.HasPrefix(statusCode, "2") ||
strings.HasPrefix(statusCode, "3") {
return okStatus
}
if flowStatusStr != "" {
return flowStatusStr
}
return errorStatus
}