Repository: prometheus/prometheus
File: cmd/prometheus/scrape_failure_log_test.go (Go)

Code before:
	"os/exec"
	"path/filepath"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
	"go.uber.org/atomic"

	"github.com/prometheus/prometheus/util/testutil"
)

func TestScrapeFailureLogFile(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	// Tracks the number of requests made to the mock server.
	var requestCount atomic.Int32

	// Starts a server that always returns HTTP 500 errors.
	mockServerAddress := startGarbageServer(t, &requestCount)

	// Create a temporary directory for Prometheus configuration and logs.
	tempDir, err := os.MkdirTemp("", "prometheus_test")
	require.NoError(t, err, "Failed to create temp directory")
	defer os.RemoveAll(tempDir)

	// Define file paths for the scrape failure log and Prometheus configuration.
	// Like other files, the scrape failure log file should be relative to the
	// config file. Therefore, we split the name we put in the file and the full
	// path used to check the content of the file.
	scrapeFailureLogFileName := "scrape_failure.log"
	scrapeFailureLogFile := filepath.Join(tempDir, "scrape_failure.log")
	promConfigFile := filepath.Join(tempDir, "prometheus.yml")

	// Step 1: Set up an initial Prometheus configuration that globally
	// specifies a scrape failure log file.
	promConfig := fmt.Sprintf(`
global:
  scrape_interval: 500ms
  scrape_failure_log_file: %s

scrape_configs:
  - job_name: 'test_job'
    static_configs:
      - targets: ['%s']
`, scrapeFailureLogFileName, mockServerAddress)

	err = os.WriteFile(promConfigFile, []byte(promConfig), 0o644)
	require.NoError(t, err, "Failed to write Prometheus configuration file")


Review comment (suggestion):
[Nit] Simpler/more idiomatic:
```suggestion
	tempDir := t.CleanUp()
```

Code after:
	"os/exec"
	"path/filepath"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
	"go.uber.org/atomic"

	"github.com/prometheus/prometheus/util/testutil"
)

func TestScrapeFailureLogFile(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	// Tracks the number of requests made to the mock server.
	var requestCount atomic.Int32

	// Starts a server that always returns HTTP 500 errors.
	mockServerAddress := startGarbageServer(t, &requestCount)

	// Create a temporary directory for Prometheus configuration and logs.
	tempDir := t.TempDir()

	// Define file paths for the scrape failure log and Prometheus configuration.
	// Like other files, the scrape failure log file should be relative to the
	// config file. Therefore, we split the name we put in the file and the full
	// path used to check the content of the file.
	scrapeFailureLogFileName := "scrape_failure.log"
	scrapeFailureLogFile := filepath.Join(tempDir, scrapeFailureLogFileName)
	promConfigFile := filepath.Join(tempDir, "prometheus.yml")

	// Step 1: Set up an initial Prometheus configuration that globally
	// specifies a scrape failure log file.
	promConfig := fmt.Sprintf(`
global:
  scrape_interval: 500ms
  scrape_failure_log_file: %s

scrape_configs:
  - job_name: 'test_job'
    static_configs:
      - targets: ['%s']
`, scrapeFailureLogFileName, mockServerAddress)

	err := os.WriteFile(promConfigFile, []byte(promConfig), 0o644)
	require.NoError(t, err, "Failed to write Prometheus configuration file")

	// Start Prometheus with the generated configuration and a random port, enabling the lifecycle API.
	port := testutil.RandomUnprivilegedPort(t)