-
Notifications
You must be signed in to change notification settings - Fork 84
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
1 parent
5f77ab5
commit d7ef84a
Showing
6 changed files
with
87 additions
and
0 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
* 2023-09-19: promyaml | ||
* 2023-09-18: kv | ||
* 2023-09-13: battery | ||
* 2023-09-11: older | ||
|
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,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 | ||
} |
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,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)) | ||
} | ||
} |
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,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 |