Skip to content

Commit

Permalink
[dynatraceexporter] truncate long unmarshalable responses (open-telem…
Browse files Browse the repository at this point in the history
…etry#10568)

* [dynatraceexporter] truncate long unmarshalable responses

* Update changelog
  • Loading branch information
dyladan authored and kentquirk committed Jun 13, 2022
1 parent 874d274 commit 8a505f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- `tailsamplingprocessor`: Add support for string invert matching to `and` policy (#9553)
- `mezemoexporter`: Add user agent string to outgoing HTTP requests (#10470)
- `transformprocessor`: Add functions for conversion of scalar metric types (`gauge_to_sum` and `sum_to_gauge`) (#10255)
- `dynatraceexporter`: Truncate unmarshalable responses to avoid long log lines (#10568)

### 🧰 Bug fixes 🧰

Expand Down
15 changes: 14 additions & 1 deletion exporter/dynatraceexporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ func (e *exporter) sendBatch(ctx context.Context, lines []string) error {
responseBody := metricsResponse{}
if err := json.Unmarshal(bodyBytes, &responseBody); err != nil {
// if the response cannot be read, do not retry the batch as it may have been successful
e.settings.Logger.Error("Failed to unmarshal response from Dynatrace", zap.Error(err), zap.ByteString("body", bodyBytes))
bodyStr := string(bodyBytes)
bodyStr = truncateString(bodyStr, 1000)
e.settings.Logger.Error("Failed to unmarshal response from Dynatrace", zap.Error(err), zap.String("body", bodyStr))
return nil
}

Expand Down Expand Up @@ -279,6 +281,17 @@ func (e *exporter) start(_ context.Context, host component.Host) (err error) {
return nil
}

func truncateString(str string, num int) string {
truncated := str
if len(str) > num {
if num > 3 {
num -= 3
}
truncated = str[0:num] + "..."
}
return truncated
}

// Response from Dynatrace is expected to be in JSON format
type metricsResponse struct {
Ok int `json:"linesOk"`
Expand Down

0 comments on commit 8a505f0

Please sign in to comment.