From 9fbd1dbc04ab4f867620189472149bda0d1ec271 Mon Sep 17 00:00:00 2001 From: Marc Semrau Date: Mon, 26 Feb 2024 14:57:48 -0700 Subject: [PATCH] Add support for additional stats in DataDog upload --- datadog.go | 14 +++++++++++++- http-canary.go | 2 ++ stats.go | 28 +++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/datadog.go b/datadog.go index bec4bae..c557f7e 100644 --- a/datadog.go +++ b/datadog.go @@ -25,7 +25,7 @@ func (list statOccurrence) Less(i, j int) bool { } // Write new stats to DataDog -func datadogUploadStats(hostname string, bucketSecs int64, addedStats map[string][]StatsStat) (err error) { +func datadogUploadStats(hostname string, bucketSecs int64, addedStats map[string][]StatsStat, additionalStats map[string]AdditionalStat) (err error) { // Generate the list of aggregated stats aggregatedStats := statsAggregate(addedStats, bucketSecs) @@ -160,6 +160,18 @@ func datadogUploadStats(hostname string, bucketSecs int64, addedStats map[string } seriesArray = append(seriesArray, series) + for _, stat := range additionalStats { + series = datadog.Series{Metric: "notehub." + hostname + "." + stat.Name, Type: datadog.PtrString("gauge")} + for _, value := range stat.Values { + point := []*float64{ + datadog.PtrFloat64(float64(value.Time)), + datadog.PtrFloat64(float64(value.Value)), + } + series.Points = append(series.Points, point) + } + seriesArray = append(seriesArray, series) + } + // Submit the metrics ctx := context.Background() ctx = context.WithValue(ctx, datadog.ContextServerVariables, map[string]string{"site": Config.DatadogSite}) diff --git a/http-canary.go b/http-canary.go index 09366a2..52fbca6 100644 --- a/http-canary.go +++ b/http-canary.go @@ -38,6 +38,8 @@ var device map[string]deviceContext // Canary handler func inboundWebCanaryHandler(httpRsp http.ResponseWriter, httpReq *http.Request) { + AddAdditionalStat("canary_called", 1) + // Exit if Config.CanaryDisabled { return diff --git a/stats.go b/stats.go index 79d7d79..e0ac3b8 100644 --- a/stats.go +++ b/stats.go @@ -80,6 +80,30 @@ var statsLock sync.Mutex var stats map[string]HostStats var statsServiceVersions map[string]string +type AdditionalStat struct { + Name string + Values []StatValue +} + +type StatValue struct { + Time int64 + Value int64 +} + +var AdditionalStats map[string]AdditionalStat + +func AddAdditionalStat(name string, value int64) { + stat, ok := AdditionalStats[name] + if !ok { + // If the stat does not exist, create it + stat = AdditionalStat{Name: name} + } + // Add the new value + stat.Values = append(stat.Values, StatValue{Time: todayTime(), Value: value}) + // Put the stat back into the map + AdditionalStats[name] = stat +} + // Trace const addStatsTrace = true @@ -709,7 +733,9 @@ func statsUpdateHost(hostname string, hostaddr string, reload bool) (ss serviceS // If this is just the initial set of stats that were being loaded from the file system, ignore it, // else write the stats to datadog if len(addedStats) > 0 && time.Now().UTC().Unix() > statsInitCompleted+60 { - datadogUploadStats(hostname, ss.BucketSecs, addedStats) + datadogUploadStats(hostname, ss.BucketSecs, addedStats, AdditionalStats) + // Clean up the additional stats so it doesn't keep growing. + AdditionalStats = map[string]AdditionalStat{} } // Done