Skip to content

Commit 1dd4d9d

Browse files
committed
fix lint
1 parent adb69b5 commit 1dd4d9d

7 files changed

Lines changed: 24 additions & 35 deletions

File tree

playground/playground.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func preparePipelineLogger(buf *bytes.Buffer, onFatal zapcore.CheckWriteHook) *z
270270
func formatMetricFamily(families []*dto.MetricFamily) string {
271271
b := new(bytes.Buffer)
272272
for _, f := range families {
273-
_ = expfmt.NewEncoder(b, expfmt.FmtOpenMetrics).Encode(f)
273+
_ = expfmt.NewEncoder(b, expfmt.NewFormat(expfmt.TypeOpenMetrics)).Encode(f)
274274
b.WriteString("\n")
275275
}
276276
return b.String()

plugin/action/event_to_metrics/event_to_metrics.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,23 +330,25 @@ func (p *Plugin) Stop() {
330330

331331
func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
332332
p.mu.Lock()
333-
copyMetrics := make([]Metric, 0, len(p.Metrics))
333+
metricIndices := make([]int, 0, len(p.Metrics))
334334
for i := range p.Metrics {
335-
if p.Metrics[i].DoIfChecker == nil {
336-
copyMetrics = append(copyMetrics, p.Metrics[i])
337-
} else {
338-
if !p.config.Metrics[i].DoIfChecker.Check(event.Root) {
339-
continue
340-
}
341-
copyMetrics = append(copyMetrics, p.Metrics[i])
342-
}
343-
if p.Metrics[i].Labels != nil {
344-
copyMetrics[len(copyMetrics)-1].Labels = make(map[string]string, len(p.Metrics[i].Labels))
345-
maps.Copy(copyMetrics[len(copyMetrics)-1].Labels, p.Metrics[i].Labels)
335+
if p.Metrics[i].DoIfChecker == nil || p.config.Metrics[i].DoIfChecker.Check(event.Root) {
336+
metricIndices = append(metricIndices, i)
346337
}
347338
}
348339
p.mu.Unlock()
349340

341+
copyMetrics := make([]Metric, 0, len(metricIndices))
342+
for _, idx := range metricIndices {
343+
metric := p.Metrics[idx]
344+
if metric.Labels != nil {
345+
labels := make(map[string]string, len(metric.Labels))
346+
maps.Copy(labels, metric.Labels)
347+
metric.Labels = labels
348+
}
349+
copyMetrics = append(copyMetrics, metric)
350+
}
351+
350352
var ts time.Time
351353

352354
if len(p.config.TimeField_) != 0 {
@@ -368,7 +370,8 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
368370
}
369371

370372
children := make([]*insaneJSON.Node, 0, len(copyMetrics))
371-
for _, metric := range copyMetrics {
373+
for i := range copyMetrics {
374+
metric := &copyMetrics[i]
372375
elem := new(insaneJSON.Node)
373376
object := elem.MutateToObject()
374377

plugin/action/event_to_metrics/event_to_metrics_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func TestEventToMetrics(t *testing.T) {
4848
wrongEventsCnt := 0
4949
outWg := sync.WaitGroup{}
5050
outWg.Add(len(config.Metrics))
51-
output.SetOutFn(func(e *pipeline.Event) {
5251

52+
output.SetOutFn(func(e *pipeline.Event) {
5353
message := e.Root
5454

5555
metricName := message.Dig("name").AsString()
@@ -62,17 +62,17 @@ func TestEventToMetrics(t *testing.T) {
6262
ttl := e.Root.Dig("ttl").AsInt64()
6363
value := e.Root.Dig("value").AsFloat()
6464

65-
if metricName == "status" {
65+
switch metricName {
66+
case "status":
6667
assert.Equal(t, "counter", metricType)
6768
assert.Equal(t, (1 * time.Hour).Milliseconds(), ttl)
6869
assert.Equal(t, float64(1), value)
69-
} else if metricName == "checkout_response_time" {
70+
case "checkout_response_time":
7071
assert.Equal(t, "gauge", metricType)
7172
assert.Equal(t, int64(0), ttl)
7273
assert.Equal(t, float64(0.1), value)
73-
} else {
74-
fmt.Println("unkown metric name", metricName)
75-
return
74+
default:
75+
assert.Fail(t, "unknown metric name", metricName)
7676
}
7777

7878
assert.Equal(t, now.UnixMilli(), timestamp)

plugin/output/prometheus/metric_collector.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ func newCollector(sender storageSender, flushTimeout time.Duration, logger *zap.
4848
return c
4949
}
5050

51-
var i int
52-
5351
func (p *metricCollector) handleMetric(labels []promwrite.Label, value float64, timestamp int64, metricType string, ttl int64) {
5452
key := labelsToKey(labels)
5553
now := time.Now()
@@ -149,7 +147,7 @@ func createTimeSeries(labels []promwrite.Label, metric *metricValue, roundPeriod
149147
}
150148

151149
func keyToLabels(key string) []promwrite.Label {
152-
if len(key) == 0 {
150+
if key == "" {
153151
return nil
154152
}
155153
key = key[:len(key)-1] // Remove trailing comma

plugin/output/prometheus/metric_collector_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ func (t *TestStorageSender) reset() {
3737
t.returnError = nil
3838
}
3939

40-
func (t *TestStorageSender) setError(err error) {
41-
t.mu.Lock()
42-
defer t.mu.Unlock()
43-
t.returnError = err
44-
}
45-
4640
func TestMetricCollector(t *testing.T) {
4741
t.Run("labelsToKey and keyToLabels roundtrip", func(t *testing.T) {
4842
labels := []promwrite.Label{

plugin/output/prometheus/prometheus.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ const (
2525
outPluginType = "prometheus"
2626
)
2727

28-
type data struct {
29-
outBuf []byte
30-
}
31-
3228
type Label struct {
3329
Label string `json:"label" required:"true"`
3430
Value string `json:"value" required:"true"`

plugin/output/prometheus/prometheus_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ func (m *mockPrometheus) reset() {
6666

6767
// mockController implements pipeline.OutputPluginController for testing
6868
type mockController struct {
69-
mu sync.Mutex
70-
commitCount int
7169
}
7270

7371
func (m *mockController) Commit(_ *pipeline.Event) {}

0 commit comments

Comments
 (0)