Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/loki] Log the first part of the http body on failed pushes to loki #6946

Merged
merged 3 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `datadogexporter`: Add compatibility with ECS Fargate semantic conventions (#6670)
- `k8s_observer`: discover k8s.node endpoints (#6820)
- `redisreceiver`: Add missing description fields to keyspace metrics (#6940)
- `lokiexporter`: Log the first part of the http body on failed pushes to loki (#6946)

## 🛑 Breaking changes 🛑

Expand Down
19 changes: 15 additions & 4 deletions exporter/lokiexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package lokiexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/lokiexporter"

import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
Expand All @@ -39,6 +39,10 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/lokiexporter/internal/third_party/loki/logproto"
)

const (
maxErrMsgLen = 1024
)

type lokiExporter struct {
config *Config
logger *zap.Logger
Expand Down Expand Up @@ -93,11 +97,18 @@ func (l *lokiExporter) pushLogData(ctx context.Context, ld pdata.Logs) error {
return consumererror.NewLogs(err, ld)
}

_, _ = io.Copy(ioutil.Discard, resp.Body)
_ = resp.Body.Close()
defer func() {
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}()

if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
err = fmt.Errorf("HTTP %d %q", resp.StatusCode, http.StatusText(resp.StatusCode))
scanner := bufio.NewScanner(io.LimitReader(resp.Body, maxErrMsgLen))
line := ""
if scanner.Scan() {
line = scanner.Text()
}
err = fmt.Errorf("HTTP %d %q: %s", resp.StatusCode, http.StatusText(resp.StatusCode), line)
jpkrohling marked this conversation as resolved.
Show resolved Hide resolved
return consumererror.NewLogs(err, ld)
}

Expand Down