diff --git a/tools/lambda-promtail/lambda-promtail/main.go b/tools/lambda-promtail/lambda-promtail/main.go index 14cef6732737a..0e0df1e880041 100644 --- a/tools/lambda-promtail/lambda-promtail/main.go +++ b/tools/lambda-promtail/lambda-promtail/main.go @@ -137,14 +137,15 @@ func parseExtraLabels(extraLabelsRaw string, omitPrefix bool) (model.LabelSet, e func getDropLabels() ([]model.LabelName, error) { var result []model.LabelName - dropLabelsRaw = os.Getenv("DROP_LABELS") - dropLabelsRawSplit := strings.Split(dropLabelsRaw, ",") - for _, dropLabelRaw := range dropLabelsRawSplit { - dropLabel := model.LabelName(dropLabelRaw) - if !dropLabel.IsValid() { - return []model.LabelName{}, fmt.Errorf("invalid label name %s", dropLabelRaw) + if dropLabelsRaw = os.Getenv("DROP_LABELS"); dropLabelsRaw != "" { + dropLabelsRawSplit := strings.Split(dropLabelsRaw, ",") + for _, dropLabelRaw := range dropLabelsRawSplit { + dropLabel := model.LabelName(dropLabelRaw) + if !dropLabel.IsValid() { + return []model.LabelName{}, fmt.Errorf("invalid label name %s", dropLabelRaw) + } + result = append(result, dropLabel) } - result = append(result, dropLabel) } return result, nil diff --git a/tools/lambda-promtail/lambda-promtail/main_test.go b/tools/lambda-promtail/lambda-promtail/main_test.go index 21063b643c109..5f70044e2b5ea 100644 --- a/tools/lambda-promtail/lambda-promtail/main_test.go +++ b/tools/lambda-promtail/lambda-promtail/main_test.go @@ -37,7 +37,8 @@ func TestLambdaPromtail_TestParseLabelsNoneProvided(t *testing.T) { } func TestLambdaPromtail_TestDropLabels(t *testing.T) { - os.Setenv("DROP_LABELS", "A1,A2") + // Simulate default env var set in Terraform + os.Setenv("DROP_LABELS", "") // Reset the shared global variables defer func() { @@ -48,6 +49,10 @@ func TestLambdaPromtail_TestDropLabels(t *testing.T) { var err error dropLabels, err = getDropLabels() require.Nil(t, err) + + os.Setenv("DROP_LABELS", "A1,A2") + dropLabels, err = getDropLabels() + require.Nil(t, err) require.Contains(t, dropLabels, model.LabelName("A1")) defaultLabelSet := model.LabelSet{