/
policy.go
179 lines (149 loc) · 4.69 KB
/
policy.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
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
package policy
import (
log "log/slog"
"net/http"
"net/url"
"strings"
"time"
armPolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
azcorePolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"google.golang.org/grpc/codes"
)
type LoggingPolicy struct {
logger log.Logger
}
func NewLoggingPolicy(logger log.Logger) *LoggingPolicy {
return &LoggingPolicy{logger: logger}
}
func (p *LoggingPolicy) Do(req *azcorePolicy.Request) (*http.Response, error) {
startTime := time.Now()
resp, err := req.Next()
method := req.Raw().Method
parsedURL, parseErr := url.Parse(req.Raw().URL.String())
if parseErr != nil {
p.logger.With(
"component", "client",
"method", "ERROR",
"service", "ERROR",
"url", req.Raw().URL.String(),
"error", parseErr.Error(),
).Error("Error parsing request URL: ", parseErr)
return nil, parseErr
}
trimmedURL := trimURL(*parsedURL)
// ex url: "https://management.azure.com/subscriptions/{sub_id}/resourcegroups?api-version={version}"
splitPath := strings.Split(trimmedURL, "/")
var resourceType string
if len(splitPath) > 5 {
resourceType = splitPath[5]
if strings.ContainsAny(resourceType, "?/") {
index := strings.IndexAny(resourceType, "?/")
// No resource name is present, must be LIST operation
resourceType = resourceType[:index] + " - LIST"
// validate resource name to check if read operation
// validation based on length to differentiate from polling operation:
// https://azure.github.io/PSRule.Rules.Azure/en/rules/Azure.ResourceGroup.Name/
} else if method == "GET" && len(splitPath[6]) <= 90 {
resourceType = resourceType + " - READ"
}
} else {
resourceType = trimmedURL
}
// REST VERB + Resource Type
methodInfo := method + " " + resourceType
if err != nil {
p.logger.With(
"component", "client",
"method", methodInfo,
"service", parsedURL.Host,
"url", trimmedURL,
"error", err.Error(),
).Error("Error finishing request: ", err)
return nil, err
}
// Time is in ms
latency := time.Since(startTime).Milliseconds()
logEntry := p.logger.With(
"code", resp.StatusCode,
"component", "client",
"time_ms", latency,
"method", methodInfo,
"service", parsedURL.Host,
"url", trimmedURL,
)
// separate check here b/c previous error check only checks if there was an error in the req
if 200 <= resp.StatusCode && resp.StatusCode < 300 {
logEntry.Info("finished call")
} else {
logEntry.With("error", resp.Status).Error("finished call")
}
return resp, err
}
func (p *LoggingPolicy) Clone() azcorePolicy.Policy {
return &LoggingPolicy{logger: p.logger}
}
func GetDefaultArmClientOptions(logger *log.Logger) *armPolicy.ClientOptions {
logOptions := new(policy.LogOptions)
retryOptions := new(policy.RetryOptions)
retryOptions.MaxRetries = 5
clientOptions := new(policy.ClientOptions)
clientOptions.Logging = *logOptions
clientOptions.Retry = *retryOptions
armClientOptions := new(armPolicy.ClientOptions)
armClientOptions.ClientOptions = *clientOptions
policyLogger := logger.With(
"source", "ApiAutoLog",
"method_type", "unary",
"protocol", "REST",
)
loggingPolicy := NewLoggingPolicy(*policyLogger)
armClientOptions.PerCallPolicies = append(armClientOptions.PerCallPolicies, loggingPolicy)
return armClientOptions
}
func trimURL(parsedURL url.URL) string {
query := parsedURL.Query()
apiVersion := query.Get("api-version")
// Remove all other query parameters
for key := range query {
if key != "api-version" {
query.Del(key)
}
}
// Set the api-version query parameter
query.Set("api-version", apiVersion)
// Encode the query parameters and set them in the parsed URL
parsedURL.RawQuery = query.Encode()
return parsedURL.String()
}
// Based off of gRPC standard here: https://chromium.googlesource.com/external/github.com/grpc/grpc/+/refs/tags/v1.21.4-pre1/doc/statuscodes.md
func ConvertHTTPStatusToGRPCError(httpStatusCode int) codes.Code {
var code codes.Code
switch httpStatusCode {
case http.StatusOK, http.StatusCreated, http.StatusAccepted:
code = codes.OK
case http.StatusBadRequest:
code = codes.InvalidArgument
case http.StatusGatewayTimeout:
code = codes.DeadlineExceeded
case http.StatusUnauthorized:
code = codes.Unauthenticated
case http.StatusForbidden:
code = codes.PermissionDenied
case http.StatusNotFound:
code = codes.NotFound
case http.StatusConflict:
code = codes.Aborted
case http.StatusTooManyRequests:
code = codes.ResourceExhausted
case http.StatusInternalServerError:
code = codes.Internal
case http.StatusNotImplemented:
code = codes.Unimplemented
case http.StatusServiceUnavailable:
code = codes.Unavailable
default:
code = codes.Unknown
}
return code
}