Skip to content

Commit

Permalink
Allow option to ignore http errors for loadgen (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahsivjar authored Oct 24, 2023
1 parent 109c34a commit 3f6e5c0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions cmd/apmbench/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func newEventHandler(tb testing.TB, p string, l *rate.Limiter) *eventhandler.Han
URL: loadgencfg.Config.ServerURL.String(),
Token: loadgencfg.Config.SecretToken,
APIKey: loadgencfg.Config.APIKey,
IgnoreErrors: loadgencfg.Config.IgnoreErrors,
RewriteIDs: loadgencfg.Config.RewriteIDs,
RewriteTimestamps: loadgencfg.Config.RewriteTimestamps,
Headers: loadgencfg.Config.Headers,
Expand Down
2 changes: 2 additions & 0 deletions internal/loadgen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var Config struct {
APIKey string
Secure bool
EventRate RateFlag
IgnoreErrors bool
RewriteIDs bool
RewriteTimestamps bool
RewriteServiceNames bool
Expand Down Expand Up @@ -110,6 +111,7 @@ func init() {
},
)
flag.Var(&Config.EventRate, "event-rate", "Event rate in format of {burst}/{interval}. For example, 200/5s, <= 0 values evaluate to Inf (default 0/s)")
flag.BoolVar(&Config.IgnoreErrors, "ignore-errors", false, "Ignore HTTP errors while sending events")

rewriteNames := map[string]*bool{
"service.name": &Config.RewriteServiceNames,
Expand Down
2 changes: 2 additions & 0 deletions internal/loadgen/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type EventHandlerParams struct {
APIKey string
Limiter *rate.Limiter
Rand *rand.Rand
IgnoreErrors bool
RewriteIDs bool
RewriteServiceNames bool
RewriteServiceNodeNames bool
Expand Down Expand Up @@ -56,6 +57,7 @@ func NewEventHandler(p EventHandlerParams) (*eventhandler.Handler, error) {
Storage: events,
Limiter: p.Limiter,
Rand: p.Rand,
IgnoreErrors: p.IgnoreErrors,
RewriteIDs: p.RewriteIDs,
RewriteServiceNames: p.RewriteServiceNames,
RewriteServiceNodeNames: p.RewriteServiceNodeNames,
Expand Down
6 changes: 5 additions & 1 deletion internal/loadgen/eventhandler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ type Config struct {
// it must not be invoked concurrently by any other goroutines.
Rand *rand.Rand

// IgnoreErrors when set to true ignores HTTP errors while sending
// events using the event handler.
IgnoreErrors bool

// RewriteTimestamps controls whether event timestamps are rewritten
// during replay.
//
Expand Down Expand Up @@ -342,7 +346,7 @@ func (h *Handler) sendBatch(
if err := s.w.Close(); err != nil {
return err
}
if err := h.config.Transport.SendV2Events(ctx, &s.w.buf); err != nil {
if err := h.config.Transport.SendV2Events(ctx, &s.w.buf, h.config.IgnoreErrors); err != nil {
return err
}
s.w.Reset()
Expand Down
24 changes: 11 additions & 13 deletions internal/loadgen/eventhandler/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package eventhandler

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -37,7 +36,7 @@ func NewTransport(c *http.Client, srvURL, token, apiKey string, headers map[stri
}

// SendV2Events sends the reader contents to `/intake/v2/events` as a batch.
func (t *Transport) SendV2Events(ctx context.Context, r io.Reader) error {
func (t *Transport) SendV2Events(ctx context.Context, r io.Reader, ignoreErrs bool) error {
req, err := http.NewRequestWithContext(ctx, "POST", t.intakeV2URL, r)
if err != nil {
return err
Expand All @@ -46,27 +45,26 @@ func (t *Transport) SendV2Events(ctx context.Context, r io.Reader) error {
// set it to `-1` just like the agents would.
req.ContentLength = -1
req.Header = t.intakeHeaders
return t.sendEvents(req, r)
return t.sendEvents(req, r, ignoreErrs)
}

func (t *Transport) sendEvents(req *http.Request, r io.Reader) error {
func (t *Transport) sendEvents(req *http.Request, r io.Reader, ignoreErrs bool) error {
res, err := t.client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()

switch res.StatusCode {
case http.StatusOK, http.StatusAccepted:
return nil
if !ignoreErrs {
switch res.StatusCode / 100 {
case 4:
return fmt.Errorf("unexpected client error: %d", res.StatusCode)
case 5:
return fmt.Errorf("unexpected server error: %d", res.StatusCode)
}
}

msg := fmt.Sprintf("unexpected apm server response %d", res.StatusCode)
b, err := io.ReadAll(res.Body)
if err != nil {
return errors.New(msg)
}
return fmt.Errorf(msg+": %s", string(b))
return nil
}

func getAuthHeader(token string, apiKey string) string {
Expand Down

0 comments on commit 3f6e5c0

Please sign in to comment.