Skip to content

Commit 068f386

Browse files
committed
cli: upload tsdump upload completion log to Datadog
Upload a log message to Datadog upon completion of a tsdump upload, with structured information for the count of uploaded timeseries, the duration, and the estimated cost of the operation. Epic: none Release note: None
1 parent 2cd255a commit 068f386

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

pkg/cli/testdata/tsdump_upload_e2e

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ cr.node.sql.query.count,2021-01-01T00:00:00Z,1,100.5
55
cr.node.sql.query.count,2021-01-01T00:00:10Z,1,102.3
66
cr.store.rocksdb.block.cache.usage,2021-01-01T00:00:00Z,2,75.2
77
----
8+
----
89
{"series":[{"interval":10,"metric":"crdb.tsdump.admission.admitted.elastic-cpu","points":[{"timestamp":1748248320,"value":1}],"tags":["node_id:1","cluster_type:SELF_HOSTED","cluster_label:\"test-cluster\"","cluster_id:test-cluster-id","zendesk_ticket:zd-test","org_name:test-org","user_name:test-user","upload_id:\"test-cluster\"-20241114000000","upload_timestamp:2024-11-14 00:00:00","upload_year:2024","upload_month:11","upload_day:14"],"type":0},{"interval":10,"metric":"crdb.tsdump.sql.query.count","points":[{"timestamp":1609459200,"value":100.5}],"tags":["node_id:1","cluster_type:SELF_HOSTED","cluster_label:\"test-cluster\"","cluster_id:test-cluster-id","zendesk_ticket:zd-test","org_name:test-org","user_name:test-user","upload_id:\"test-cluster\"-20241114000000","upload_timestamp:2024-11-14 00:00:00","upload_year:2024","upload_month:11","upload_day:14"],"type":0},{"interval":10,"metric":"crdb.tsdump.sql.query.count","points":[{"timestamp":1609459210,"value":102.3}],"tags":["node_id:1","cluster_type:SELF_HOSTED","cluster_label:\"test-cluster\"","cluster_id:test-cluster-id","zendesk_ticket:zd-test","org_name:test-org","user_name:test-user","upload_id:\"test-cluster\"-20241114000000","upload_timestamp:2024-11-14 00:00:00","upload_year:2024","upload_month:11","upload_day:14"],"type":0},{"interval":10,"metric":"crdb.tsdump.rocksdb.block.cache.usage","points":[{"timestamp":1609459200,"value":75.2}],"tags":["store:2","cluster_type:SELF_HOSTED","cluster_label:\"test-cluster\"","cluster_id:test-cluster-id","zendesk_ticket:zd-test","org_name:test-org","user_name:test-user","upload_id:\"test-cluster\"-20241114000000","upload_timestamp:2024-11-14 00:00:00","upload_year:2024","upload_month:11","upload_day:14"],"type":0}]}
10+
11+
[{"ddsource":"tsdump_upload","ddtags":"cluster_type:SELF_HOSTED,cluster_label:\"test-cluster\",cluster_id:test-cluster-id,zendesk_ticket:zd-test,org_name:test-org,user_name:test-user,upload_id:\"test-cluster\"-20241114000000,upload_timestamp:2024-11-14 00:00:00,upload_year:2024,upload_month:11,upload_day:14,series_uploaded:4","hostname":"hostname","message":"{\"message\":\"tsdump upload completed: uploaded 4 series overall\",\"series_uploaded\":4,\"estimated_cost\":0.00024931506849315067,\"duration\":0,\"dry_run\":false,\"success\":true}","service":"tsdump_upload"}]
12+
----
13+
----

pkg/cli/tsdump_upload.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ var getCurrentTime = func() time.Time {
156156
return timeutil.Now()
157157
}
158158

159+
var getHostname = func() string {
160+
hostname, err := os.Hostname()
161+
if err != nil {
162+
return "unknown"
163+
}
164+
return hostname
165+
}
166+
159167
// appendTag appends a formatted tag to the series tags.
160168
func appendTag(series *datadogV2.MetricSeries, tagKey, tagValue string) {
161169
series.Tags = append(series.Tags, fmt.Sprintf("%s:%s", tagKey, tagValue))
@@ -472,12 +480,15 @@ func (d *datadogWriter) upload(fileName string) error {
472480
estimatedCost := float64(seriesUploaded) * 4.55 / 100 / 730
473481
fmt.Printf("Estimated cost of this upload: $%.2f\n", estimatedCost)
474482

483+
tags := getUploadTags(d)
484+
success := metricsUploadState.isSingleUploadSucceeded
475485
if metricsUploadState.isSingleUploadSucceeded {
476486
var isDatadogUploadFailed = false
477487
markDatadogUploadFailedOnce := sync.OnceFunc(func() {
478488
isDatadogUploadFailed = true
479489
})
480490
if len(metricsUploadState.uploadFailedMetrics) != 0 {
491+
success = false
481492
fmt.Printf(partialFailureMessageFormat, len(metricsUploadState.uploadFailedMetrics), strings.Join(func() []string {
482493
var failedMetricsList []string
483494
index := 1
@@ -489,7 +500,7 @@ func (d *datadogWriter) upload(fileName string) error {
489500
return failedMetricsList
490501
}(), "\n"))
491502

492-
tags := strings.Join(getUploadTags(d), ",")
503+
tags := strings.Join(tags, ",")
493504
fmt.Println("\nPushing logs of metric upload failures to datadog...")
494505
for metric := range metricsUploadState.uploadFailedMetrics {
495506
wg.Add(1)
@@ -528,6 +539,40 @@ func (d *datadogWriter) upload(fileName string) error {
528539
fmt.Println("All metric upload is failed. Please re-upload the Tsdump.")
529540
}
530541

542+
eventTags := append(tags, makeDDTag("series_uploaded", strconv.Itoa(seriesUploaded)))
543+
544+
api := datadogV2.NewLogsApi(d.apiClient)
545+
hostName := getHostname()
546+
msgJson, _ := json.Marshal(struct {
547+
Message string `json:"message"`
548+
SeriesUploaded int `json:"series_uploaded"`
549+
EstimatedCost float64 `json:"estimated_cost"`
550+
Duration int `json:"duration"`
551+
DryRun bool `json:"dry_run"`
552+
Success bool `json:"success"`
553+
}{
554+
Message: fmt.Sprintf("tsdump upload completed: uploaded %d series overall", seriesUploaded),
555+
SeriesUploaded: seriesUploaded,
556+
Duration: int(getCurrentTime().Sub(d.uploadTime).Nanoseconds()),
557+
DryRun: debugTimeSeriesDumpOpts.dryRun,
558+
EstimatedCost: estimatedCost,
559+
Success: success,
560+
})
561+
_, _, err = api.SubmitLog(d.datadogContext, []datadogV2.HTTPLogItem{
562+
{
563+
Ddsource: datadog.PtrString("tsdump_upload"),
564+
Ddtags: datadog.PtrString(strings.Join(eventTags, ",")),
565+
Message: string(msgJson),
566+
Service: datadog.PtrString("tsdump_upload"),
567+
Hostname: datadog.PtrString(hostName),
568+
},
569+
}, datadogV2.SubmitLogOptionalParameters{
570+
ContentEncoding: datadogV2.CONTENTENCODING_GZIP.Ptr(),
571+
})
572+
if err != nil {
573+
fmt.Printf("error submitting log to datadog: %v\n", err)
574+
}
575+
531576
close(ch)
532577
return nil
533578
}

pkg/cli/tsdump_upload_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func TestTSDumpUploadE2E(t *testing.T) {
3939
defer testutils.TestingHook(&getCurrentTime, func() time.Time {
4040
return time.Date(2024, 11, 14, 0, 0, 0, 0, time.UTC)
4141
})()
42+
defer testutils.TestingHook(&getHostname, func() string {
43+
return "hostname"
44+
})()
4245

4346
datadriven.RunTest(t, "testdata/tsdump_upload_e2e", func(t *testing.T, d *datadriven.TestData) string {
4447
var buf strings.Builder

0 commit comments

Comments
 (0)