Skip to content

Commit

Permalink
[prometheusremotewriteexporter] Handle case with 0 metrics (#12489)
Browse files Browse the repository at this point in the history
Fixes #10364

Signed-off-by: Goutham Veeramachaneni <[email protected]>

Signed-off-by: Goutham Veeramachaneni <[email protected]>
Co-authored-by: Bogdan Drutu <[email protected]>
  • Loading branch information
gouthamve and bogdandrutu authored Aug 22, 2022
1 parent c0fe1b3 commit fd7c5f4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions exporter/prometheusremotewriteexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ func validateAndSanitizeExternalLabels(cfg *Config) (map[string]string, error) {
}

func (prwe *prwExporter) handleExport(ctx context.Context, tsMap map[string]*prompb.TimeSeries) error {
// There are no metrics to export, so return.
if len(tsMap) == 0 {
return nil
}

// Calls the helper function to convert and batch the TsMap to the desired format
requests, err := batchTimeSeries(tsMap, maxBatchByteSize)
if err != nil {
Expand Down
14 changes: 13 additions & 1 deletion exporter/prometheusremotewriteexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,22 @@ func Test_export(t *testing.T) {
}
}

func TestNoMetricsNoError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusAccepted)
}))
defer server.Close()
serverURL, uErr := url.Parse(server.URL)
assert.NoError(t, uErr)
assert.NoError(t, runExportPipeline(nil, serverURL))
}

func runExportPipeline(ts *prompb.TimeSeries, endpoint *url.URL) error {
// First we will construct a TimeSeries array from the testutils package
testmap := make(map[string]*prompb.TimeSeries)
testmap["test"] = ts
if ts != nil {
testmap["test"] = ts
}

cfg := createDefaultConfig().(*Config)
cfg.HTTPClientSettings.Endpoint = endpoint.String()
Expand Down
16 changes: 16 additions & 0 deletions unreleased/fix-0-len-prw-exports.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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: prometheusremotewriteexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Handle the case with 0 metrics gracefully.

# One or more tracking issues related to the change
issues: [10364]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

0 comments on commit fd7c5f4

Please sign in to comment.