From d7ef84a4040ff232662c7d00cc7e49befbd01834 Mon Sep 17 00:00:00 2001 From: Jozef Reisinger Date: Tue, 19 Sep 2023 11:57:18 +0200 Subject: [PATCH] add promyaml kata --- go.mod | 1 + go.sum | 4 ++++ katas.md | 1 + promyaml/promyaml.go | 40 +++++++++++++++++++++++++++++++++++++ promyaml/promyaml_test.go | 32 +++++++++++++++++++++++++++++ promyaml/testdata/prom.yaml | 9 +++++++++ 6 files changed, 87 insertions(+) create mode 100644 promyaml/promyaml.go create mode 100644 promyaml/promyaml_test.go create mode 100644 promyaml/testdata/prom.yaml diff --git a/go.mod b/go.mod index 9213828..8e71525 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/google/go-cmp v0.5.9 github.com/rogpeppe/go-internal v1.11.0 golang.org/x/net v0.13.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( diff --git a/go.sum b/go.sum index ab8870a..a711a6d 100644 --- a/go.sum +++ b/go.sum @@ -8,3 +8,7 @@ golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/katas.md b/katas.md index f663312..97252f0 100644 --- a/katas.md +++ b/katas.md @@ -1,3 +1,4 @@ +* 2023-09-19: promyaml * 2023-09-18: kv * 2023-09-13: battery * 2023-09-11: older diff --git a/promyaml/promyaml.go b/promyaml/promyaml.go new file mode 100644 index 0000000..ef6466b --- /dev/null +++ b/promyaml/promyaml.go @@ -0,0 +1,40 @@ +// Package promyaml shows how to get (Prometheus) configuration from YAML file. +// +// Level: intermediate +// Topics: yaml, tpg-tools +package promyaml + +import ( + "os" + "time" + + "gopkg.in/yaml.v3" +) + +type Config struct { + Global GlobalConfig +} + +type GlobalConfig struct { + ScrapeInterval time.Duration `yaml:"scrape_interval"` + EvaluationInterval time.Duration `yaml:"evaluation_interval"` + ScrapeTimeout time.Duration `yaml:"scrape_timeout"` + ExternalLabels map[string]string `yaml:"external_labels"` +} + +func ConfigFrom(path string) (Config, error) { + f, err := os.Open(path) + if err != nil { + return Config{}, err + } + defer f.Close() + config := Config{ + GlobalConfig{ + ScrapeTimeout: 10 * time.Second, // default + }, + } + if err := yaml.NewDecoder(f).Decode(&config); err != nil { + return Config{}, err + } + return config, err +} diff --git a/promyaml/promyaml_test.go b/promyaml/promyaml_test.go new file mode 100644 index 0000000..c7f0347 --- /dev/null +++ b/promyaml/promyaml_test.go @@ -0,0 +1,32 @@ +package promyaml_test + +import ( + "testing" + "time" + + "github.com/jreisinger/gokatas/promyaml" + + "github.com/google/go-cmp/cmp" +) + +func TestConfigFrom_CorrectlyParsesYAMLData(t *testing.T) { + t.Parallel() + want := promyaml.Config{ + Global: promyaml.GlobalConfig{ + ScrapeInterval: 15 * time.Second, + EvaluationInterval: 30 * time.Second, + ScrapeTimeout: 10 * time.Second, + ExternalLabels: map[string]string{ + "monitor": "codelab", + "foo": "bar", + }, + }, + } + got, err := promyaml.ConfigFrom("testdata/prom.yaml") + if err != nil { + t.Fatal(err) + } + if !cmp.Equal(want, got) { + t.Error(cmp.Diff(want, got)) + } +} diff --git a/promyaml/testdata/prom.yaml b/promyaml/testdata/prom.yaml new file mode 100644 index 0000000..5e9ae7a --- /dev/null +++ b/promyaml/testdata/prom.yaml @@ -0,0 +1,9 @@ +# my global config +global: + scrape_interval: 15s + evaluation_interval: 30s + # scrape_timeout is set to the global default (10s). + + external_labels: + monitor: codelab + foo: bar