@@ -97,8 +97,8 @@ func (r *gubernatorRateLimiter) Shutdown(ctx context.Context) error {
97
97
}
98
98
99
99
func (r * gubernatorRateLimiter ) RateLimit (ctx context.Context , hits int ) error {
100
- projectID := getProjectIDFromMetadata (ctx )
101
100
uniqueKey := getUniqueKey (ctx , r .cfg .MetadataKeys )
101
+
102
102
createdAt := time .Now ().UnixMilli ()
103
103
getRateLimitsResp , err := r .client .GetRateLimits (ctx , & gubernator.GetRateLimitsReq {
104
104
Requests : []* gubernator.RateLimitReq {{
@@ -115,32 +115,35 @@ func (r *gubernatorRateLimiter) RateLimit(ctx context.Context, hits int) error {
115
115
})
116
116
if err != nil {
117
117
r .set .Logger .Error ("error executing gubernator rate limit request" , zap .Error (err ))
118
- r .telemetryBuilder .RatelimitRequests .Add (ctx , 1 , metric .WithAttributeSet (attribute .NewSet (
119
- telemetry .WithProjectID (projectID ),
120
- telemetry .WithLimitReason ("request_error" ),
118
+ attrs := []attribute.KeyValue {
119
+ telemetry .WithErrorReason (telemetry .BadRequest ),
121
120
telemetry .WithDecision ("rejected" ),
122
- )))
121
+ }
122
+ attrs = attrsFromMetadata (ctx , r .cfg .MetadataKeys , attrs )
123
+ r .telemetryBuilder .RatelimitRequests .Add (ctx , 1 , metric .WithAttributeSet (attribute .NewSet (attrs ... )))
123
124
return errRateLimitInternalError
124
125
}
125
126
126
127
// Inside the gRPC response, we should have a single-item list of responses.
127
128
responses := getRateLimitsResp .GetResponses ()
128
129
if n := len (responses ); n != 1 {
129
- r .telemetryBuilder .RatelimitRequests .Add (ctx , 1 , metric .WithAttributeSet (attribute .NewSet (
130
- telemetry .WithProjectID (projectID ),
131
- telemetry .WithLimitReason ("request_error" ),
130
+ attrs := []attribute.KeyValue {
131
+ telemetry .WithErrorReason (telemetry .Invalid ),
132
132
telemetry .WithDecision ("accepted" ),
133
- )))
133
+ }
134
+ attrs = attrsFromMetadata (ctx , r .cfg .MetadataKeys , attrs )
135
+ r .telemetryBuilder .RatelimitRequests .Add (ctx , 1 , metric .WithAttributeSet (attribute .NewSet (attrs ... )))
134
136
return fmt .Errorf ("expected 1 response from gubernator, got %d" , n )
135
137
}
136
138
resp := responses [0 ]
137
139
if resp .GetError () != "" {
138
140
r .set .Logger .Error ("failed to get response from gubernator" , zap .Error (errors .New (resp .GetError ())))
139
- r .telemetryBuilder .RatelimitRequests .Add (ctx , 1 , metric .WithAttributeSet (attribute .NewSet (
140
- telemetry .WithProjectID (projectID ),
141
- telemetry .WithLimitReason ("limit_error" ),
141
+ attrs := []attribute.KeyValue {
142
+ telemetry .WithErrorReason (telemetry .ServerError ),
142
143
telemetry .WithDecision ("rejected" ),
143
- )))
144
+ }
145
+ attrs = attrsFromMetadata (ctx , r .cfg .MetadataKeys , attrs )
146
+ r .telemetryBuilder .RatelimitRequests .Add (ctx , 1 , metric .WithAttributeSet (attribute .NewSet (attrs ... )))
144
147
return errRateLimitInternalError
145
148
}
146
149
@@ -154,28 +157,36 @@ func (r *gubernatorRateLimiter) RateLimit(ctx context.Context, hits int) error {
154
157
zap .String ("processor_id" , r .set .ID .String ()),
155
158
zap .Strings ("metadata_keys" , r .cfg .MetadataKeys ),
156
159
)
157
- r .telemetryBuilder .RatelimitRequests .Add (ctx , 1 , metric .WithAttributeSet (attribute .NewSet (
158
- telemetry .WithProjectID (projectID ),
159
- telemetry .WithLimitReason ("throttled" ),
160
- telemetry .WithDecision ("accepted" ),
161
- )))
160
+ attrs := []attribute.KeyValue {
161
+ telemetry .WithErrorReason (telemetry .StatusOverLimit ),
162
+ telemetry .WithDecision ("rejected" ),
163
+ }
164
+ attrs = attrsFromMetadata (ctx , r .cfg .MetadataKeys , attrs )
165
+ r .telemetryBuilder .RatelimitRequests .Add (ctx , 1 , metric .WithAttributeSet (attribute .NewSet (attrs ... )))
162
166
return errTooManyRequests
163
167
case ThrottleBehaviorDelay :
164
168
delay := time .Duration (resp .GetResetTime ()- createdAt ) * time .Millisecond
165
169
timer := time .NewTimer (delay )
166
170
defer timer .Stop ()
167
171
select {
168
172
case <- ctx .Done ():
173
+ attrs := []attribute.KeyValue {
174
+ telemetry .WithErrorReason (telemetry .StatusOverLimit ),
175
+ telemetry .WithDecision ("accepted" ),
176
+ }
177
+ attrs = attrsFromMetadata (ctx , r .cfg .MetadataKeys , attrs )
178
+ r .telemetryBuilder .RatelimitRequests .Add (ctx , 1 , metric .WithAttributeSet (attribute .NewSet (attrs ... )))
169
179
return ctx .Err ()
170
180
case <- timer .C :
171
181
}
172
182
}
173
183
}
174
184
175
- r .telemetryBuilder .RatelimitRequests .Add (ctx , 1 , metric .WithAttributeSet (attribute .NewSet (
176
- telemetry .WithProjectID (projectID ),
177
- telemetry .WithLimitReason ("under_limit" ),
185
+ attrs := []attribute.KeyValue {
186
+ telemetry .WithErrorReason (telemetry .StatusUnderLimit ),
178
187
telemetry .WithDecision ("accepted" ),
179
- )))
188
+ }
189
+ attrs = attrsFromMetadata (ctx , r .cfg .MetadataKeys , attrs )
190
+ r .telemetryBuilder .RatelimitRequests .Add (ctx , 1 , metric .WithAttributeSet (attribute .NewSet (attrs ... )))
180
191
return nil
181
192
}
0 commit comments