Skip to content

Commit c75248c

Browse files
authored
Use empty string as the fault metric method label for HTTP requests. (#693)
* Comment out labels with high cardinality. The remaining labels have maximums of 3, 2, 2 and 2 values respectively. * Updated to keep clientName since it's local and not upstream. * Remove rather than comment out. No need to keep these around. * Update to use an empty string as the method label for http requests. * Add comment.
1 parent e78dc85 commit c75248c

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

httpbp/client_middlewares.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/avast/retry-go"
1616
"github.com/prometheus/client_golang/prometheus"
1717
"github.com/reddit/baseplate.go/headerbp"
18+
"github.com/reddit/baseplate.go/internal/faults"
1819
"github.com/reddit/baseplate.go/secrets"
1920

2021
"github.com/reddit/baseplate.go/breakerbp"
@@ -467,7 +468,15 @@ func (c clientFaultMiddleware) Middleware() ClientMiddleware {
467468
address := req.URL.Hostname()
468469
method := strings.TrimPrefix(req.URL.Path, "/")
469470
header := httpHeader(req.Header)
470-
return c.injector.InjectWithAbortOverride(req.Context(), address, method, &header, resume, abort)
471+
return c.injector.Inject(req.Context(),
472+
faults.InjectParameters[*http.Response]{
473+
Address: address,
474+
Method: method,
475+
MethodLabel: "",
476+
Headers: &header,
477+
Resume: resume,
478+
Abort: abort,
479+
})
471480
})
472481
}
473482
}

internal/faults/faults.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,30 @@ func NewInjector[T any](clientName, callerName string, abortCodeMin, abortCodeMa
139139
return i
140140
}
141141

142-
// InjectWithAbortOverride injects a fault using the provided fault function
143-
// on the outgoing request if it matches the header configuration.
144-
func (i *Injector[T]) InjectWithAbortOverride(ctx context.Context, address, method string, headers Headers, resume Resume[T], abort Abort[T]) (T, error) {
142+
// InjectParameters contains the parameters needed to match and inject a fault
143+
// into the outgoing request.
144+
type InjectParameters[T any] struct {
145+
Address string
146+
Method string
147+
MethodLabel string
148+
Headers Headers
149+
Resume Resume[T]
150+
Abort Abort[T]
151+
}
152+
153+
// Inject injects a fault using the Injector default fault function on the
154+
// outgoing request if it matches the header configuration.
155+
func (i *Injector[T]) Inject(ctx context.Context, params InjectParameters[T]) (T, error) {
156+
if params.Abort == nil {
157+
params.Abort = i.defaultAbort
158+
}
159+
145160
delayed := false
146161
totalReqsCounter := func(success, aborted bool) prometheus.Counter {
147162
return totalRequests.WithLabelValues(
148163
i.clientName,
149-
address,
150-
method,
164+
params.Address,
165+
params.MethodLabel,
151166
i.callerName,
152167
strconv.FormatBool(success),
153168
strconv.FormatBool(delayed),
@@ -166,47 +181,41 @@ func (i *Injector[T]) InjectWithAbortOverride(ctx context.Context, address, meth
166181
}
167182
}
168183

169-
faultHeaderValues, err := headers.LookupValues(ctx, FaultHeader)
184+
faultHeaderValues, err := params.Headers.LookupValues(ctx, FaultHeader)
170185
if err != nil {
171186
infof("error looking up the values of header %q: %v", FaultHeader, err)
172187
totalReqsCounter(true, false).Inc()
173-
return resume()
188+
return params.Resume()
174189
}
175190

176-
faultConfiguration, err := parseMatchingFaultConfiguration(faultHeaderValues, getCanonicalAddress(address), method, i.abortCodeMin, i.abortCodeMax)
191+
faultConfiguration, err := parseMatchingFaultConfiguration(faultHeaderValues, getCanonicalAddress(params.Address), params.Method, i.abortCodeMin, i.abortCodeMax)
177192
if err != nil {
178193
warnf("error parsing fault header %q: %v", FaultHeader, err)
179194

180195
if faultConfiguration == nil {
181196
totalReqsCounter(false, false).Inc()
182-
return resume()
197+
return params.Resume()
183198
}
184199
}
185200
if faultConfiguration == nil {
186201
totalReqsCounter(true, false).Inc()
187-
return resume()
202+
return params.Resume()
188203
}
189204

190205
if faultConfiguration.Delay > 0 && i.selected(faultConfiguration.DelayPercentage) {
191206
if err := i.sleep(ctx, faultConfiguration.Delay); err != nil {
192207
warnf("error when delaying request: %v", err)
193208
totalReqsCounter(false, false).Inc()
194-
return resume()
209+
return params.Resume()
195210
}
196211
delayed = true
197212
}
198213

199214
if faultConfiguration.AbortCode != -1 && i.selected(faultConfiguration.AbortPercentage) {
200215
totalReqsCounter(true, true).Inc()
201-
return abort(faultConfiguration.AbortCode, faultConfiguration.AbortMessage)
216+
return params.Abort(faultConfiguration.AbortCode, faultConfiguration.AbortMessage)
202217
}
203218

204219
totalReqsCounter(true, false).Inc()
205-
return resume()
206-
}
207-
208-
// Inject injects a fault using the Injector default fault function on the
209-
// outgoing request if it matches the header configuration.
210-
func (i *Injector[T]) Inject(ctx context.Context, address, method string, headers Headers, resume Resume[T]) (T, error) {
211-
return i.InjectWithAbortOverride(ctx, address, method, headers, resume, i.defaultAbort)
220+
return params.Resume()
212221
}

internal/faults/faults_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,12 @@ func TestInject(t *testing.T) {
220220
return nil
221221
}
222222

223-
resp, err := injector.Inject(context.Background(), address, method, &headers, resume)
223+
resp, err := injector.Inject(context.Background(), InjectParameters[*response]{
224+
Address: address,
225+
Method: method,
226+
Headers: &headers,
227+
Resume: resume,
228+
})
224229

225230
if err != nil {
226231
t.Fatalf("expected no error, got %v", err)

thriftbp/client_middlewares.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/reddit/baseplate.go/ecinterface"
1717
"github.com/reddit/baseplate.go/errorsbp"
1818
"github.com/reddit/baseplate.go/headerbp"
19+
"github.com/reddit/baseplate.go/internal/faults"
1920
"github.com/reddit/baseplate.go/internal/gen-go/reddit/baseplate"
2021
"github.com/reddit/baseplate.go/internal/thriftint"
2122

@@ -469,7 +470,14 @@ func (c clientFaultMiddleware) Middleware() thrift.ClientMiddleware {
469470
return next.Call(ctx, method, args, result)
470471
}
471472

472-
return c.injector.Inject(ctx, c.address, method, &thriftHeaders{}, resume)
473+
return c.injector.Inject(ctx,
474+
faults.InjectParameters[thrift.ResponseMeta]{
475+
Address: c.address,
476+
Method: method,
477+
MethodLabel: method,
478+
Headers: &thriftHeaders{},
479+
Resume: resume,
480+
})
473481
},
474482
}
475483
}

0 commit comments

Comments
 (0)