| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package metricsaudit |
| 4 | |
| 5 | import ( |
| 6 | "sync" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | writeQueueSize = 256 |
| 14 | maxWriteErrorSamples = 10 |
| 15 | ) |
| 16 | |
| 17 | // JobID identifies a job uniquely across modules. |
| 18 | type JobID struct { |
| 19 | Module string |
| 20 | Name string |
| 21 | } |
| 22 | |
| 23 | func newJobID(moduleName, jobName string) JobID { |
| 24 | return JobID{ |
| 25 | Module: moduleName, |
| 26 | Name: jobName, |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | type writeTask struct { |
| 31 | label string |
| 32 | run func() error |
| 33 | after func() |
| 34 | flush chan struct{} |
| 35 | } |
| 36 | |
| 37 | // Auditor collects and analyzes metric structure from metrics-audit mode. |
| 38 | type Auditor struct { |
| 39 | mu sync.RWMutex |
| 40 | jobs map[JobID]*JobAnalysis |
| 41 | startTime time.Time |
| 42 | dataDir string |
| 43 | jobDirs map[JobID]string |
| 44 | jobDone map[JobID]bool |
| 45 | onComplete func() |
| 46 | completed bool |
| 47 | writeCh chan writeTask |
| 48 | writeErrorCount int |
| 49 | writeErrors []string |
| 50 | } |
| 51 | |
| 52 | // JobAnalysis holds analysis for a single job |
| 53 | type JobAnalysis struct { |
| 54 | Name string |
| 55 | Module string |
| 56 | Charts []ChartAnalysis |
| 57 | CollectionCount int |
| 58 | LastCollection time.Time |
| 59 | AllSeenMetrics map[string]bool // Track ALL metrics seen in mx map |
| 60 | } |
| 61 | |
| 62 | // ChartAnalysis holds analysis for a single chart |
| 63 | type ChartAnalysis struct { |
| 64 | Chart *collectorapi.Chart |
| 65 | CollectedValues map[string][]int64 // dimension ID -> collected values |
| 66 | SeenDimensions map[string]bool // track which dimensions received data |
| 67 | } |
| 68 | |
| 69 | // New creates a new metrics-audit analyzer. |
| 70 | func New() *Auditor { |
| 71 | return &Auditor{ |
| 72 | jobs: make(map[JobID]*JobAnalysis), |
| 73 | startTime: time.Now(), |
| 74 | jobDirs: make(map[JobID]string), |
| 75 | jobDone: make(map[JobID]bool), |
| 76 | } |
| 77 | } |