-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
276 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2023 Adevinta | ||
|
||
// Package metrics collects all kind of Lava metrics. | ||
package metrics | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
// Collector represents a metrics collector. | ||
type Collector struct { | ||
Metrics map[string]interface{} | ||
Emitter emitter | ||
} | ||
|
||
// NewCollector creates a new metrics collector. | ||
func NewCollector() (*Collector, error) { | ||
return &Collector{ | ||
Metrics: make(map[string]interface{}), | ||
Emitter: NewJSONEmitter(), | ||
}, nil | ||
} | ||
|
||
// Collect stores the metric. | ||
func (c *Collector) Collect(metric string, value interface{}) { | ||
c.Metrics[metric] = value | ||
} | ||
|
||
// Write renders the metrics. | ||
func (c *Collector) Write() error { | ||
if err := c.Emitter.Emit(c.Metrics); err != nil { | ||
return fmt.Errorf("emit metrics: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// A Emitter emit metrics. | ||
type emitter interface { | ||
Emit(metrics map[string]interface{}) error | ||
} | ||
|
||
// JSONEmitter emits metrics in JSON format. | ||
type JSONEmitter struct { | ||
createWriter func() (io.WriteCloser, error) | ||
} | ||
|
||
// NewJSONEmitter create a metrics emitter. | ||
func NewJSONEmitter() *JSONEmitter { | ||
return &JSONEmitter{ | ||
// For lazy file creation. | ||
createWriter: func() (io.WriteCloser, error) { | ||
f, err := os.Create("metrics.json") | ||
if err != nil { | ||
return nil, fmt.Errorf("create file: %w", err) | ||
} | ||
return f, nil | ||
}, | ||
} | ||
} | ||
|
||
// Emit renders the metrics in JSON format. | ||
func (c *JSONEmitter) Emit(metrics map[string]interface{}) error { | ||
w, err := c.createWriter() | ||
if err != nil { | ||
return fmt.Errorf("create writer: %w", err) | ||
} | ||
defer w.Close() | ||
enc := json.NewEncoder(w) | ||
enc.SetIndent("", " ") | ||
if err := enc.Encode(metrics); err != nil { | ||
return fmt.Errorf("encode report: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2023 Adevinta | ||
|
||
package metrics | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
type mockWriteClose struct { | ||
*bytes.Buffer | ||
} | ||
|
||
func (mwc *mockWriteClose) Close() error { | ||
return nil | ||
} | ||
|
||
func TestMetrics_JSONCollector(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
metrics map[string]interface{} | ||
want map[string]interface{} | ||
}{ | ||
{ | ||
name: "Happy Path", | ||
metrics: map[string]interface{}{ | ||
"metric 1": "metric value 1", | ||
"metric 2": 12345, | ||
"metric 3": 25.5, | ||
"metric 4": map[string]int{ | ||
"key 1": 1, | ||
"key 2": 2, | ||
}, | ||
"metric 5": []string{ | ||
"one", "two", "three", | ||
}, | ||
}, | ||
want: map[string]interface{}{ | ||
"metric 1": "metric value 1", | ||
"metric 2": float64(12345), | ||
"metric 3": 25.5, | ||
"metric 4": map[string]any{ | ||
"key 1": float64(1), | ||
"key 2": float64(2), | ||
}, | ||
"metric 5": []any{ | ||
"one", "two", "three", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
je := &JSONEmitter{ | ||
createWriter: func() (io.WriteCloser, error) { | ||
return &mockWriteClose{buf}, nil | ||
}, | ||
} | ||
mc := Collector{ | ||
Metrics: make(map[string]interface{}), | ||
Emitter: je, | ||
} | ||
for key, value := range tt.metrics { | ||
mc.Collect(key, value) | ||
} | ||
|
||
if err := mc.Write(); err != nil { | ||
t.Fatalf("emit metrics %v", err) | ||
} | ||
var got map[string]interface{} | ||
if err := json.Unmarshal(buf.Bytes(), &got); err != nil { | ||
t.Errorf("unmarshal json metrics: %v", err) | ||
} | ||
if diff := cmp.Diff(tt.want, got); diff != "" { | ||
t.Errorf("metrics mismatch (-want +got):\n%v", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.