Skip to content

Commit

Permalink
add promyaml kata
Browse files Browse the repository at this point in the history
  • Loading branch information
jreisinger committed Sep 19, 2023
1 parent 5f77ab5 commit d7ef84a
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
1 change: 1 addition & 0 deletions katas.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* 2023-09-19: promyaml
* 2023-09-18: kv
* 2023-09-13: battery
* 2023-09-11: older
Expand Down
40 changes: 40 additions & 0 deletions promyaml/promyaml.go
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 32 additions & 0 deletions promyaml/promyaml_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
9 changes: 9 additions & 0 deletions promyaml/testdata/prom.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d7ef84a

Please sign in to comment.