|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package cli |
| 7 | + |
| 8 | +import ( |
| 9 | + "compress/gzip" |
| 10 | + "encoding/csv" |
| 11 | + "encoding/gob" |
| 12 | + "fmt" |
| 13 | + "io" |
| 14 | + "net/http" |
| 15 | + "os" |
| 16 | + "strconv" |
| 17 | + "strings" |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/cockroachdb/cockroach/pkg/roachpb" |
| 22 | + "github.com/cockroachdb/cockroach/pkg/testutils" |
| 23 | + "github.com/cockroachdb/cockroach/pkg/ts" |
| 24 | + "github.com/cockroachdb/cockroach/pkg/ts/tspb" |
| 25 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 26 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 27 | + "github.com/cockroachdb/datadriven" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | +) |
| 30 | + |
| 31 | +// TestTSDumpUploadE2E tests the end-to-end functionality of uploading a time |
| 32 | +// series dump to Datadog from a user perspective. This runs the tsdump command |
| 33 | +// externally. The datadog API is mocked to capture the request and verify the |
| 34 | +// uploaded data. |
| 35 | +func TestTSDumpUploadE2E(t *testing.T) { |
| 36 | + defer leaktest.AfterTest(t)() |
| 37 | + defer log.Scope(t).Close(t) |
| 38 | + defer testutils.TestingHook(&getCurrentTime, func() time.Time { |
| 39 | + return time.Date(2024, 11, 14, 0, 0, 0, 0, time.UTC) |
| 40 | + })() |
| 41 | + |
| 42 | + datadriven.RunTest(t, "testdata/tsdump_upload_e2e", func(t *testing.T, d *datadriven.TestData) string { |
| 43 | + var buf strings.Builder |
| 44 | + defer mockDoDDRequest(t, &buf)() |
| 45 | + |
| 46 | + c := NewCLITest(TestCLIParams{}) |
| 47 | + defer c.Cleanup() |
| 48 | + |
| 49 | + switch d.Cmd { |
| 50 | + case "upload-datadog": |
| 51 | + debugTimeSeriesDumpOpts.clusterLabel = "test-cluster" |
| 52 | + debugTimeSeriesDumpOpts.clusterID = "test-cluster-id" |
| 53 | + debugTimeSeriesDumpOpts.zendeskTicket = "zd-test" |
| 54 | + debugTimeSeriesDumpOpts.organizationName = "test-org" |
| 55 | + debugTimeSeriesDumpOpts.userName = "test-user" |
| 56 | + dumpFilePath := generateMockTSDumpFromCSV(t, d.Input) |
| 57 | + |
| 58 | + var clusterLabel, apiKey string |
| 59 | + if d.HasArg("cluster-label") { |
| 60 | + d.ScanArgs(t, "cluster-label", &clusterLabel) |
| 61 | + } else { |
| 62 | + clusterLabel = "test-cluster" |
| 63 | + } |
| 64 | + if d.HasArg("api-key") { |
| 65 | + d.ScanArgs(t, "api-key", &apiKey) |
| 66 | + } else { |
| 67 | + apiKey = "dd-api-key" |
| 68 | + } |
| 69 | + |
| 70 | + // Run the command |
| 71 | + _, err := c.RunWithCapture(fmt.Sprintf( |
| 72 | + `debug tsdump --format=datadog --dd-api-key="%s" --cluster-label="%s" %s`, |
| 73 | + apiKey, clusterLabel, dumpFilePath, |
| 74 | + )) |
| 75 | + require.NoError(t, err) |
| 76 | + return strings.TrimSpace(buf.String()) |
| 77 | + |
| 78 | + default: |
| 79 | + t.Fatalf("unknown command: %s", d.Cmd) |
| 80 | + return "" |
| 81 | + } |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func mockDoDDRequest(t *testing.T, w io.Writer) func() { |
| 86 | + t.Helper() |
| 87 | + |
| 88 | + return testutils.TestingHook(&doDDRequest, func(req *http.Request) error { |
| 89 | + defer req.Body.Close() |
| 90 | + |
| 91 | + reader, err := gzip.NewReader(req.Body) |
| 92 | + require.NoError(t, err) |
| 93 | + |
| 94 | + raw, err := io.ReadAll(reader) |
| 95 | + require.NoError(t, err) |
| 96 | + |
| 97 | + fmt.Fprintln(w, string(raw)) |
| 98 | + return nil |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +// generateMockTSDumpFromCSV creates a mock tsdump file from CSV input string. |
| 103 | +// CSV format: metric_name,timestamp,source,value |
| 104 | +// Example: cr.node.admission.admitted.elastic-cpu,2025-05-26T08:32:00Z,1,1 |
| 105 | +// NOTE: this is the same format generated by the `cockroach tsdump` command |
| 106 | +// when --format=csv is used. |
| 107 | +func generateMockTSDumpFromCSV(t *testing.T, csvInput string) string { |
| 108 | + t.Helper() |
| 109 | + |
| 110 | + // Parse CSV data from input string |
| 111 | + reader := csv.NewReader(strings.NewReader(csvInput)) |
| 112 | + csvData, err := reader.ReadAll() |
| 113 | + require.NoError(t, err) |
| 114 | + require.Greater(t, len(csvData), 0, "CSV input must have at least one data row") |
| 115 | + |
| 116 | + // Create temporary file |
| 117 | + tmpFile, err := os.CreateTemp("", "mock_tsdump_*.gob") |
| 118 | + require.NoError(t, err) |
| 119 | + defer tmpFile.Close() |
| 120 | + |
| 121 | + // Create gob encoder |
| 122 | + encoder := gob.NewEncoder(tmpFile) |
| 123 | + |
| 124 | + // Process each row (no header expected) |
| 125 | + for i, row := range csvData { |
| 126 | + require.Len(t, row, 4, "CSV row %d must have 4 columns: metric_name,timestamp,source,value", i+1) |
| 127 | + |
| 128 | + metricName := row[0] |
| 129 | + timestampStr := row[1] |
| 130 | + source := row[2] |
| 131 | + valueStr := row[3] |
| 132 | + |
| 133 | + // Parse timestamp (RFC3339 format) |
| 134 | + timestamp, err := time.Parse(time.RFC3339, timestampStr) |
| 135 | + require.NoError(t, err, "invalid timestamp format in row %d: %s (expected RFC3339)", i+1, timestampStr) |
| 136 | + timestampNanos := timestamp.UnixNano() |
| 137 | + |
| 138 | + // Parse value |
| 139 | + value, err := strconv.ParseFloat(valueStr, 64) |
| 140 | + require.NoError(t, err, "invalid value in row %d: %s", i+1, valueStr) |
| 141 | + |
| 142 | + // Create KeyValue entry for this data point |
| 143 | + kv, err := createMockTimeSeriesKV(metricName, source, timestampNanos, value) |
| 144 | + require.NoError(t, err) |
| 145 | + |
| 146 | + // Encode to gob format |
| 147 | + err = encoder.Encode(kv) |
| 148 | + require.NoError(t, err) |
| 149 | + } |
| 150 | + |
| 151 | + t.Cleanup(func() { |
| 152 | + require.NoError(t, os.Remove(tmpFile.Name()), "failed to remove temporary file") |
| 153 | + }) |
| 154 | + return tmpFile.Name() |
| 155 | +} |
| 156 | + |
| 157 | +// createMockTimeSeriesKV creates a roachpb.KeyValue entry containing time series data |
| 158 | +func createMockTimeSeriesKV( |
| 159 | + name, source string, timestamp int64, value float64, |
| 160 | +) (roachpb.KeyValue, error) { |
| 161 | + // Create TimeSeriesData |
| 162 | + tsData := tspb.TimeSeriesData{ |
| 163 | + Name: name, |
| 164 | + Source: source, |
| 165 | + Datapoints: []tspb.TimeSeriesDatapoint{ |
| 166 | + {TimestampNanos: timestamp, Value: value}, |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + // Convert to internal format using 10s resolution |
| 171 | + resolution := ts.Resolution10s |
| 172 | + idatas, err := tsData.ToInternal( |
| 173 | + resolution.SlabDuration(), // 1 hour (3600 * 10^9 ns) |
| 174 | + resolution.SampleDuration(), // 10 seconds (10 * 10^9 ns) |
| 175 | + true, // columnar format |
| 176 | + ) |
| 177 | + if err != nil { |
| 178 | + return roachpb.KeyValue{}, err |
| 179 | + } |
| 180 | + |
| 181 | + // Should only be one internal data entry for a single datapoint |
| 182 | + if len(idatas) != 1 { |
| 183 | + return roachpb.KeyValue{}, fmt.Errorf("expected 1 internal data entry, got %d", len(idatas)) |
| 184 | + } |
| 185 | + |
| 186 | + idata := idatas[0] |
| 187 | + |
| 188 | + // Create the key |
| 189 | + key := ts.MakeDataKey(name, source, resolution, idata.StartTimestampNanos) |
| 190 | + |
| 191 | + // Create the value (protobuf-encoded internal data) |
| 192 | + var roachValue roachpb.Value |
| 193 | + if err := roachValue.SetProto(&idata); err != nil { |
| 194 | + return roachpb.KeyValue{}, err |
| 195 | + } |
| 196 | + |
| 197 | + return roachpb.KeyValue{Key: key, Value: roachValue}, nil |
| 198 | +} |
0 commit comments