Skip to content

Commit cb886e0

Browse files
authored
metrics: report platform family to console (#3897)
1 parent 22f4ee7 commit cb886e0

File tree

6 files changed

+72
-30
lines changed

6 files changed

+72
-30
lines changed

cmd/crowdsec/lpmetrics.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type MetricsProvider struct {
4848

4949
type staticMetrics struct {
5050
osName string
51+
osFamily string
5152
osVersion string
5253
startupTS int64
5354
featureFlags []string
@@ -71,9 +72,11 @@ func getHubState(hub *cwhub.Hub) models.HubItems {
7172
if item.State.IsLocal() {
7273
status = "custom"
7374
}
75+
7476
if item.State.Tainted {
7577
status = "tainted"
7678
}
79+
7780
ret[itemType] = append(ret[itemType], models.HubItem{
7881
Name: item.Name,
7982
Status: status,
@@ -93,10 +96,11 @@ func newStaticMetrics(consoleOptions []string, datasources []acquisition.DataSou
9396
datasourceMap[ds.GetName()] += 1
9497
}
9598

96-
osName, osVersion := version.DetectOS()
99+
osName, osFamily, osVersion := version.DetectOS()
97100

98101
return staticMetrics{
99102
osName: osName,
103+
osFamily: osFamily,
100104
osVersion: osVersion,
101105
startupTS: time.Now().UTC().Unix(),
102106
featureFlags: fflag.Crowdsec.GetEnabledFeatures(),
@@ -106,14 +110,23 @@ func newStaticMetrics(consoleOptions []string, datasources []acquisition.DataSou
106110
}
107111
}
108112

109-
func NewMetricsProvider(apic *apiclient.ApiClient, interval time.Duration, logger *logrus.Entry,
110-
consoleOptions []string, datasources []acquisition.DataSource, hub *cwhub.Hub,
113+
func NewMetricsProvider(
114+
apic *apiclient.ApiClient,
115+
interval time.Duration,
116+
logger *logrus.Entry,
117+
consoleOptions []string,
118+
datasources []acquisition.DataSource,
119+
hub *cwhub.Hub,
111120
) *MetricsProvider {
121+
static := newStaticMetrics(consoleOptions, datasources, hub)
122+
123+
logger.Debugf("Detected %s %s (family: %s)", static.osName, static.osVersion, static.osFamily)
124+
112125
return &MetricsProvider{
113126
apic: apic,
114127
interval: interval,
115128
logger: logger,
116-
static: newStaticMetrics(consoleOptions, datasources, hub),
129+
static: static,
117130
}
118131
}
119132

@@ -123,6 +136,7 @@ func getLabelValue(labels []*io_prometheus_client.LabelPair, key string) string
123136
return label.GetValue()
124137
}
125138
}
139+
126140
return ""
127141
}
128142

@@ -135,9 +149,11 @@ func getDeltaKey(metricName string, labels []*io_prometheus_client.LabelPair) st
135149
slices.SortFunc(sortedLabels, func(a, b *io_prometheus_client.LabelPair) int {
136150
return strings.Compare(a.GetName(), b.GetName())
137151
})
152+
138153
for _, label := range sortedLabels {
139154
parts = append(parts, label.GetName()+label.GetValue())
140155
}
156+
141157
return strings.Join(parts, "")
142158
}
143159

@@ -147,10 +163,12 @@ func shouldIgnoreMetric(exclude map[string]*regexp.Regexp, promLabels []*io_prom
147163
if labelValue == "" {
148164
continue
149165
}
166+
150167
if regex.MatchString(labelValue) {
151168
return true
152169
}
153170
}
171+
154172
return false
155173
}
156174

@@ -167,6 +185,7 @@ func (m *MetricsProvider) gatherPromMetrics(metricsName []string, labelsMap labe
167185
if !slices.Contains(metricsName, metricFamily.GetName()) {
168186
continue
169187
}
188+
170189
for _, metric := range metricFamily.GetMetric() {
171190
promLabels := metric.GetLabel()
172191

@@ -191,6 +210,7 @@ func (m *MetricsProvider) gatherPromMetrics(metricsName []string, labelsMap labe
191210
value = 0
192211
}
193212
}
213+
194214
metricsLastValues[deltaKey] = currentValue
195215

196216
if value == 0 {
@@ -312,12 +332,12 @@ func (m *MetricsProvider) metricsPayload() *models.AllMetrics {
312332
Items: make([]*models.MetricsDetailItem, 0),
313333
})
314334

315-
/* Acquisition metrics */
316-
/*{"name": "read", "value": 10, "unit": "line", labels: {"datasource_type": "file", "source":"/var/log/auth.log"}}*/
317-
/* Parser metrics */
318-
/*{"name": "parsed", labels: {"datasource_type": "file", "source":"/var/log/auth.log"}}*/
319-
/*{"name": "unparsed", labels: {"datasource_type": "file", "source":"/var/log/auth.log"}}*/
320-
/*{"name": "whitelisted", labels: {"datasource_type": "file", "source":"/var/log/auth.log"}}*/
335+
// Acquisition metrics
336+
// {"name": "read", "value": 10, "unit": "line", labels: {"datasource_type": "file", "source":"/var/log/auth.log"}}
337+
// Parser metrics
338+
// {"name": "parsed", labels: {"datasource_type": "file", "source":"/var/log/auth.log"}}
339+
// {"name": "unparsed", labels: {"datasource_type": "file", "source":"/var/log/auth.log"}}
340+
// {"name": "whitelisted", labels: {"datasource_type": "file", "source":"/var/log/auth.log"}}
321341

322342
acquisitionMetrics := m.getAcquisitionMetrics()
323343
if len(acquisitionMetrics) > 0 {

go.mod

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ require (
1818
github.com/aws/aws-sdk-go-v2/service/s3 v1.87.3
1919
github.com/aws/aws-sdk-go-v2/service/sqs v1.42.3
2020
github.com/beevik/etree v1.4.1
21-
github.com/blackfireio/osinfo v1.1.0 // indirect
2221
github.com/bluele/gcache v0.0.2
2322
github.com/buger/jsonparser v1.1.1
2423
github.com/cenkalti/backoff/v5 v5.0.2
2524
github.com/cespare/xxhash/v2 v2.3.0
2625
github.com/corazawaf/coraza/v3 v3.3.3
2726
github.com/corazawaf/libinjection-go v0.2.2
2827
github.com/crowdsecurity/dlog v0.0.2
29-
github.com/crowdsecurity/go-cs-lib v0.0.21
28+
github.com/crowdsecurity/go-cs-lib v0.0.23
3029
github.com/crowdsecurity/grokky v0.2.2
3130
github.com/crowdsecurity/machineid v1.0.3
3231
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
@@ -74,7 +73,7 @@ require (
7473
github.com/r3labs/diff/v2 v2.15.1
7574
github.com/sanity-io/litter v1.5.8
7675
github.com/segmentio/kafka-go v0.4.48
77-
github.com/shirou/gopsutil/v4 v4.25.7
76+
github.com/shirou/gopsutil/v4 v4.25.8
7877
github.com/sirupsen/logrus v1.9.3
7978
github.com/slack-go/slack v0.16.0
8079
github.com/spf13/cobra v1.10.1
@@ -195,7 +194,7 @@ require (
195194
github.com/petar-dambovaliev/aho-corasick v0.0.0-20250424160509-463d218d4745 // indirect
196195
github.com/pierrec/lz4/v4 v4.1.18 // indirect
197196
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
198-
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
197+
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
199198
github.com/prometheus/common v0.44.0 // indirect
200199
github.com/prometheus/procfs v0.15.1 // indirect
201200
github.com/rivo/uniseg v0.4.7 // indirect

go.sum

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
9494
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
9595
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
9696
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
97-
github.com/blackfireio/osinfo v1.1.0 h1:1LMkMiFL42+Brx7r3MKuf7UTlXBRgebFLJQAfoFafj8=
98-
github.com/blackfireio/osinfo v1.1.0/go.mod h1:Pd987poVNmd5Wsx6PRPw4+w7kLlf9iJxoRKPtPAjOrA=
9997
github.com/bluele/gcache v0.0.2 h1:WcbfdXICg7G/DGBh1PFfcirkWOQV+v077yF1pSy3DGw=
10098
github.com/bluele/gcache v0.0.2/go.mod h1:m15KV+ECjptwSPxKhOhQoAFQVtUFjTVkc3H8o0t/fp0=
10199
github.com/bmatcuk/doublestar v1.3.4 h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQrS0=
@@ -142,8 +140,8 @@ github.com/crowdsecurity/coraza/v3 v3.3.3-crowdsec.20250609 h1:t0fgUIJ7FnDKQSigu
142140
github.com/crowdsecurity/coraza/v3 v3.3.3-crowdsec.20250609/go.mod h1:q/LGNBRelJdzJZK08U1Rm5cNHv9DKp98p0esMDhJ5tE=
143141
github.com/crowdsecurity/dlog v0.0.2 h1:nj/7jLKO0o8tYn79O+g51ASeGLr5oOVahSoJ6Umq51g=
144142
github.com/crowdsecurity/dlog v0.0.2/go.mod h1:zpv7r+7KXwgVUZnUNjyP22zc/D7LKjyoY02weH2RBbk=
145-
github.com/crowdsecurity/go-cs-lib v0.0.21 h1:LUq3QIwICRaxsSJjV5KADP5HbY4GLcnkqS0IX8QzLxI=
146-
github.com/crowdsecurity/go-cs-lib v0.0.21/go.mod h1:HLUGOlXGjkXTpONmQACOE+YayoiudaGjRsE89dSa8So=
143+
github.com/crowdsecurity/go-cs-lib v0.0.23 h1:9YPJG97uXZh95uwvPWeFeAZPXjZ7HaUdtcyFCLSYxu8=
144+
github.com/crowdsecurity/go-cs-lib v0.0.23/go.mod h1:X0GMJY2CxdA1S09SpuqIKaWQsvRGxXmecUp9cP599dE=
147145
github.com/crowdsecurity/grokky v0.2.2 h1:yALsI9zqpDArYzmSSxfBq2dhYuGUTKMJq8KOEIAsuo4=
148146
github.com/crowdsecurity/grokky v0.2.2/go.mod h1:33usDIYzGDsgX1kHAThCbseso6JuWNJXOzRQDGXHtWM=
149147
github.com/crowdsecurity/machineid v1.0.3 h1:mgd//PhMJqyA1EdRTgwRvafwbzNjoktdJyEgZGVCD2Q=
@@ -495,8 +493,8 @@ github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77
495493
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
496494
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
497495
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
498-
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
499-
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
496+
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
497+
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
500498
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
501499
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
502500
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
@@ -540,8 +538,8 @@ github.com/segmentio/kafka-go v0.4.48 h1:9jyu9CWK4W5W+SroCe8EffbrRZVqAOkuaLd/ApI
540538
github.com/segmentio/kafka-go v0.4.48/go.mod h1:HjF6XbOKh0Pjlkr5GVZxt6CsjjwnmhVOfURM5KMd8qg=
541539
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
542540
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
543-
github.com/shirou/gopsutil/v4 v4.25.7 h1:bNb2JuqKuAu3tRlPv5piSmBZyMfecwQ+t/ILq+1JqVM=
544-
github.com/shirou/gopsutil/v4 v4.25.7/go.mod h1:XV/egmwJtd3ZQjBpJVY5kndsiOO4IRqy9TQnmm6VP7U=
541+
github.com/shirou/gopsutil/v4 v4.25.8 h1:NnAsw9lN7587WHxjJA9ryDnqhJpFH6A+wagYWTOH970=
542+
github.com/shirou/gopsutil/v4 v4.25.8/go.mod h1:q9QdMmfAOVIw7a+eF86P7ISEU6ka+NLgkUxlopV4RwI=
545543
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
546544
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
547545
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=

pkg/apiserver/apic_metrics.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type dbPayload struct {
2525

2626
func (a *apic) GetUsageMetrics(ctx context.Context) (*models.AllMetrics, []int, error) {
2727
allMetrics := &models.AllMetrics{}
28-
metricsIds := make([]int, 0)
28+
metricsIDs := make([]int, 0)
2929

3030
lps, err := a.dbClient.ListMachines(ctx)
3131
if err != nil {
@@ -66,7 +66,7 @@ func (a *apic) GetUsageMetrics(ctx context.Context) (*models.AllMetrics, []int,
6666
for _, dbMetric := range dbMetrics {
6767
dbPayload := &dbPayload{}
6868
// Append no matter what, if we cannot unmarshal, there's no way we'll be able to fix it automatically
69-
metricsIds = append(metricsIds, dbMetric.ID)
69+
metricsIDs = append(metricsIDs, dbMetric.ID)
7070

7171
err := json.Unmarshal([]byte(dbMetric.Payload), dbPayload)
7272
if err != nil {
@@ -128,7 +128,7 @@ func (a *apic) GetUsageMetrics(ctx context.Context) (*models.AllMetrics, []int,
128128
for _, dbMetric := range dbMetrics {
129129
dbPayload := &dbPayload{}
130130
// Append no matter what, if we cannot unmarshal, there's no way we'll be able to fix it automatically
131-
metricsIds = append(metricsIds, dbMetric.ID)
131+
metricsIDs = append(metricsIDs, dbMetric.ID)
132132

133133
err := json.Unmarshal([]byte(dbMetric.Payload), dbPayload)
134134
if err != nil {
@@ -150,10 +150,11 @@ func (a *apic) GetUsageMetrics(ctx context.Context) (*models.AllMetrics, []int,
150150
},
151151
}
152152

153-
osName, osVersion := version.DetectOS()
153+
osName, osFamily, osVersion := version.DetectOS()
154154

155155
allMetrics.Lapi.Os = &models.OSversion{
156156
Name: ptr.Of(osName),
157+
Family: osFamily,
157158
Version: ptr.Of(osVersion),
158159
}
159160
allMetrics.Lapi.Version = ptr.Of(version.String())
@@ -178,7 +179,7 @@ func (a *apic) GetUsageMetrics(ctx context.Context) (*models.AllMetrics, []int,
178179
allMetrics.LogProcessors = make([]*models.LogProcessorsMetrics, 0)
179180
}
180181

181-
return allMetrics, metricsIds, nil
182+
return allMetrics, metricsIDs, nil
182183
}
183184

184185
func (a *apic) MarkUsageMetricsAsSent(ctx context.Context, ids []int) error {
@@ -358,7 +359,7 @@ func (a *apic) SendUsageMetrics(ctx context.Context) {
358359
ticker.Reset(a.usageMetricsInterval)
359360
}
360361

361-
metrics, metricsId, err := a.GetUsageMetrics(ctx)
362+
metrics, metricsID, err := a.GetUsageMetrics(ctx)
362363
if err != nil {
363364
log.Errorf("unable to get usage metrics: %s", err)
364365
continue
@@ -380,13 +381,13 @@ func (a *apic) SendUsageMetrics(ctx context.Context) {
380381
}
381382
}
382383

383-
err = a.MarkUsageMetricsAsSent(ctx, metricsId)
384+
err = a.MarkUsageMetricsAsSent(ctx, metricsID)
384385
if err != nil {
385386
log.Errorf("unable to mark usage metrics as sent: %s", err)
386387
continue
387388
}
388389

389-
log.Infof("Sent %d usage metrics", len(metricsId))
390+
log.Infof("Sent %d usage metrics", len(metricsID))
390391
}
391392
}
392393
}

pkg/models/localapi_swagger.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,10 @@ definitions:
12901290
type: string
12911291
description: name of the OS
12921292
maxLength: 255
1293+
family:
1294+
type: string
1295+
description: family of the OS
1296+
maxLength: 255
12931297
version:
12941298
type: string
12951299
description: version of the OS

pkg/models/o_sversion.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)