From 4f4c70673aa5ead6cbb5eeff07f6091d1dc5de24 Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Mon, 21 Nov 2022 13:47:28 +0100 Subject: [PATCH] [exporter/datadog] Do not read response if nil on logs error --- .chloggen/mx-psi_logs-exporter-crash.yaml | 11 +++++++++++ exporter/datadogexporter/internal/logs/sender.go | 15 +++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100755 .chloggen/mx-psi_logs-exporter-crash.yaml diff --git a/.chloggen/mx-psi_logs-exporter-crash.yaml b/.chloggen/mx-psi_logs-exporter-crash.yaml new file mode 100755 index 000000000000..ed5bd5e7b6f2 --- /dev/null +++ b/.chloggen/mx-psi_logs-exporter-crash.yaml @@ -0,0 +1,11 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: datadogexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fixes crash when logging error on logs exporter + +# One or more tracking issues related to the change +issues: [16077] diff --git a/exporter/datadogexporter/internal/logs/sender.go b/exporter/datadogexporter/internal/logs/sender.go index 0256f489f311..9aae4a8425fd 100644 --- a/exporter/datadogexporter/internal/logs/sender.go +++ b/exporter/datadogexporter/internal/logs/sender.go @@ -20,6 +20,7 @@ import ( "github.com/DataDog/datadog-api-client-go/v2/api/datadog" "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" + "go.opentelemetry.io/collector/consumer/consumererror" "go.opentelemetry.io/collector/exporter/exporterhelper" "go.uber.org/zap" @@ -73,10 +74,16 @@ func (s *Sender) SubmitLogs(ctx context.Context, payload []datadogV2.HTTPLogItem } _, r, err := s.api.SubmitLog(ctx, payload, s.opts) if err != nil { - b := make([]byte, 1024) // 1KB message max - n, _ := r.Body.Read(b) // ignore any error - s.logger.Error("Failed to send logs", zap.Error(err), zap.String("msg", string(b[:n])), zap.String("status_code", r.Status)) - return err + if r != nil { + b := make([]byte, 1024) // 1KB message max + n, _ := r.Body.Read(b) // ignore any error + s.logger.Error("Failed to send logs", zap.Error(err), zap.String("msg", string(b[:n])), zap.String("status_code", r.Status)) + return err + } + + // If response is nil assume permanent error. + // The error will be logged by the exporter helper. + return consumererror.NewPermanent(err) } return nil }