From 8ff1dd45169908e09d4c2a2ef9636ac25836dcc3 Mon Sep 17 00:00:00 2001 From: Neil Ramsay Date: Wed, 9 Feb 2022 12:34:40 +1300 Subject: [PATCH 1/3] Minor autoindents --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 9688232..cd8dde1 100644 --- a/main.go +++ b/main.go @@ -60,7 +60,7 @@ var times = flag.Int("config.scrape-times", 0, "how many times to scrape before var roleArn = flag.String("config.role-arn", "", "ARN of the role to assume when scraping the AWS API (optional)") var prometheusPortLabel = flag.String("config.port-label", "PROMETHEUS_EXPORTER_PORT", "Docker label to define the scrape port of the application (if missing an application won't be scraped)") var prometheusPathLabel = flag.String("config.path-label", "PROMETHEUS_EXPORTER_PATH", "Docker label to define the scrape path of the application") -var prometheusSchemeLabel= flag.String("config.scheme-label", "PROMETHEUS_EXPORTER_SCHEME", "Docker label to define the scheme of the target application") +var prometheusSchemeLabel = flag.String("config.scheme-label", "PROMETHEUS_EXPORTER_SCHEME", "Docker label to define the scheme of the target application") var prometheusFilterLabel = flag.String("config.filter-label", "", "Docker label (and optionally value) to require to scrape the application") var prometheusServerNameLabel = flag.String("config.server-name-label", "PROMETHEUS_EXPORTER_SERVER_NAME", "Docker label to define the server name") var prometheusJobNameLabel = flag.String("config.job-name-label", "PROMETHEUS_EXPORTER_JOB_NAME", "Docker label to define the job name") @@ -297,7 +297,7 @@ func (t *AugmentedTask) ExporterInformation() []*PrometheusTaskInfo { scheme, ok = d.DockerLabels[*prometheusSchemeLabel] if ok { - labels.Scheme = scheme + labels.Scheme = scheme } ret = append(ret, &PrometheusTaskInfo{ From 15e4e98b757ef3c14fa7526a69990c55544f8c0c Mon Sep 17 00:00:00 2001 From: Neil Ramsay Date: Wed, 9 Feb 2022 12:34:57 +1300 Subject: [PATCH 2/3] Perform atomic writes for resulting service discovery YAML file We were seeing parsing error messages in Prometheus, as below: ``` ts=2022-02-08T17:59:59.920Z caller=file.go:337 level=error component="discovery manager scrape" discovery=file msg="Error reading file" path=/etc/prometheus/prometheus_ecs_container_endpoints.123456789.yml err="yaml: line 186: could not find expected ':'" ``` On manual inspection of the files, they appeared complete and could be parsed. This indicates that the file is not complete at the time Prometheus is reading it. The https://github.com/natefinch/atomic library allows writing the service discovery file, and then atomically moving it in place. --- go.mod | 1 + go.sum | 2 ++ main.go | 6 ++++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 448b80c..190573b 100644 --- a/go.mod +++ b/go.mod @@ -11,4 +11,5 @@ require ( github.com/aws/smithy-go v1.3.0 github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-yaml/yaml v2.1.0+incompatible + github.com/natefinch/atomic v1.0.1 ) diff --git a/go.sum b/go.sum index 33e816b..e0846c1 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0A= +github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/main.go b/main.go index cd8dde1..a33164d 100644 --- a/main.go +++ b/main.go @@ -15,11 +15,11 @@ package main import ( + "bytes" "context" "errors" "flag" "fmt" - "io/ioutil" "log" "strconv" "strings" @@ -34,6 +34,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/sts" "github.com/aws/smithy-go" "github.com/go-yaml/yaml" + "github.com/natefinch/atomic" ) type labels struct { @@ -661,12 +662,13 @@ func main() { infos = append(infos, info...) } m, err := yaml.Marshal(infos) + b := bytes.NewBuffer(m) if err != nil { logError(err) return } log.Printf("Writing %d discovered exporters to %s", len(infos), *outFile) - err = ioutil.WriteFile(*outFile, m, 0644) + err = atomic.WriteFile(*outFile, b) // need to set mode 0644 first time if err != nil { logError(err) return From a5e9d721bbd03f01c6924e293cfed0be6a87d91a Mon Sep 17 00:00:00 2001 From: Neil Ramsay Date: Wed, 9 Feb 2022 13:03:53 +1300 Subject: [PATCH 3/3] Ensure outputted service discovery YAML file mode is 0644 --- main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index a33164d..1debc53 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ import ( "flag" "fmt" "log" + "os" "strconv" "strings" "time" @@ -668,11 +669,12 @@ func main() { return } log.Printf("Writing %d discovered exporters to %s", len(infos), *outFile) - err = atomic.WriteFile(*outFile, b) // need to set mode 0644 first time + err = atomic.WriteFile(*outFile, b) if err != nil { logError(err) return } + os.Chmod(*outFile, 0644) } s := time.NewTimer(1 * time.Millisecond) t := time.NewTicker(*interval)