Skip to content

Commit

Permalink
feat: Harvest should log number of renderedBytes for each collector (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrinds authored Nov 13, 2024
1 parent e19d978 commit 668a661
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/collectors/rest/plugins/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (h *Health) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, *util

result = append(result, emsMat)
h.SLogger.Info(
"Collected",
"Health plugin",
slog.Int("numLicenseAlerts", licenseAlertCount),
slog.Int("numVolumeMoveAlerts", volumeMoveAlertCount),
slog.Int("numVolumeRansomwareAlerts", volumeRansomwareAlertCount),
Expand Down
31 changes: 22 additions & 9 deletions cmd/exporters/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ func (p *Prometheus) render(data *matrix.Matrix) ([][]byte, exporter.Stats) {
histograms map[string]*histogram
normalizedLabels map[string][]string // cache of histogram normalized labels
instancesExported uint64
renderedBytes uint64
instanceKeysOk bool
buf bytes.Buffer // shared buffer for rendering
)
Expand Down Expand Up @@ -442,11 +443,13 @@ func (p *Prometheus) render(data *matrix.Matrix) ([][]byte, exporter.Stats) {
prefixed := prefix + "_labels"
if tagged != nil && !tagged.Has(prefixed) {
tagged.Add(prefixed)
rendered = append(rendered,
[]byte("# HELP "+prefixed+" Pseudo-metric for "+data.Object+" labels"),
[]byte("# TYPE "+prefixed+" gauge"))
help := "# HELP " + prefixed + " Pseudo-metric for " + data.Object + " labels"
typeT := "# TYPE " + prefixed + " gauge"
rendered = append(rendered, []byte(help), []byte(typeT))
renderedBytes += uint64(len(help)) + uint64(len(typeT))
}
rendered = append(rendered, labelData)
renderedBytes += uint64(len(labelData))
}
}

Expand Down Expand Up @@ -503,12 +506,14 @@ func (p *Prometheus) render(data *matrix.Matrix) ([][]byte, exporter.Stats) {
prefixedName := prefix + "_" + metric.GetName()
if tagged != nil && !tagged.Has(prefixedName) {
tagged.Add(prefixedName)
rendered = append(rendered,
[]byte("# HELP "+prefixedName+" Metric for "+data.Object),
[]byte("# TYPE "+prefixedName+" histogram"))
help := "# HELP " + prefixedName + " Metric for " + data.Object
typeT := "# TYPE " + prefixedName + " histogram"
rendered = append(rendered, []byte(help), []byte(typeT))
renderedBytes += uint64(len(help)) + uint64(len(typeT))
}

rendered = append(rendered, []byte(x))
renderedBytes += uint64(len(x))
// scalar metric
} else {
buf.Reset()
Expand Down Expand Up @@ -547,6 +552,7 @@ func (p *Prometheus) render(data *matrix.Matrix) ([][]byte, exporter.Stats) {
copy(helpB, xbr)

rendered = append(rendered, helpB)
renderedBytes += uint64(len(helpB))

buf.Reset()
buf.WriteString("# TYPE ")
Expand All @@ -558,9 +564,11 @@ func (p *Prometheus) render(data *matrix.Matrix) ([][]byte, exporter.Stats) {
copy(typeB, tbr)

rendered = append(rendered, typeB)
renderedBytes += uint64(len(typeB))
}

rendered = append(rendered, scalarMetric)
renderedBytes += uint64(len(scalarMetric))
}
}
}
Expand Down Expand Up @@ -592,9 +600,11 @@ func (p *Prometheus) render(data *matrix.Matrix) ([][]byte, exporter.Stats) {
prefixedName := prefix + "_" + metric.GetName()
if tagged != nil && !tagged.Has(prefixedName) {
tagged.Add(prefix + "_" + metric.GetName())
rendered = append(rendered,
[]byte("# HELP "+prefixedName+" Metric for "+data.Object),
[]byte("# TYPE "+prefixedName+" histogram"))

help := "# HELP " + prefixedName + " Metric for " + data.Object
typeT := "# TYPE " + prefixedName + " histogram"
rendered = append(rendered, []byte(help), []byte(typeT))
renderedBytes += uint64(len(help)) + uint64(len(typeT))
}

normalizedNames, canNormalize := normalizedLabels[objectMetric]
Expand All @@ -616,15 +626,18 @@ func (p *Prometheus) render(data *matrix.Matrix) ([][]byte, exporter.Stats) {
x = prefix + "_" + metric.GetName() + "{" + joinedKeys + `,` + escape(p.replacer, "metric", bucketName) + "} " + value
}
rendered = append(rendered, []byte(x))
renderedBytes += uint64(len(x))
}
if canNormalize {
rendered = append(rendered, []byte(countMetric), []byte(sumMetric))
renderedBytes += uint64(len(countMetric)) + uint64(len(sumMetric))
}
}
}
stats := exporter.Stats{
InstancesExported: instancesExported,
MetricsExported: uint64(len(rendered)),
RenderedBytes: renderedBytes,
}

return rendered, stats
Expand Down
27 changes: 15 additions & 12 deletions cmd/poller/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,19 +529,21 @@ func (c *AbstractCollector) Start(wg *sync.WaitGroup) {

// Continue if metadata failed, since it might be specific to metadata
for _, data := range results {
if data.IsExportable() {
stats, err := e.Export(data)
if err != nil {
c.Logger.Error(
"export data",
slogx.Err(err),
slog.String("exporter", e.GetName()),
)
break
}
exporterStats.InstancesExported += stats.InstancesExported
exporterStats.MetricsExported += stats.MetricsExported
if !data.IsExportable() {
continue
}
stats, err := e.Export(data)
if err != nil {
c.Logger.Error(
"export data",
slogx.Err(err),
slog.String("exporter", e.GetName()),
)
break
}
exporterStats.InstancesExported += stats.InstancesExported
exporterStats.MetricsExported += stats.MetricsExported
exporterStats.RenderedBytes += stats.RenderedBytes
}
}

Expand Down Expand Up @@ -612,6 +614,7 @@ func (c *AbstractCollector) logMetadata(taskName string, stats exporter.Stats) {
int64Field("pluginInstances"),
timeToMilli("plugin_time"),
timeToMilli("poll_time"),
slog.Uint64("renderedBytes", stats.RenderedBytes),
int64Field("skips"),
int64Field("zBegin"),
)
Expand Down
1 change: 1 addition & 0 deletions cmd/poller/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var status = [3]string{
type Stats struct {
InstancesExported uint64
MetricsExported uint64
RenderedBytes uint64
}

// AbstractExporter implements all methods of the Exporter interface, except Export()
Expand Down

0 comments on commit 668a661

Please sign in to comment.