Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atomically write the json file #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

trazfr
Copy link

@trazfr trazfr commented Sep 13, 2018

Hello,

I'm using your code and ended up with a lot of holes in the monitoring.
I also got many errors like this on Prometheus side (logs set to debug):

level=debug ts=2018-09-13T04:47:02.434227377Z caller=file.go:338 component="discovery manager scrape" discovery=file msg="file_sd refresh found file that should be removed" file=/opt/prometheus/etc/mdns-sd.json
level=error ts=2018-09-13T04:47:22.433789245Z caller=file.go:321 component="discovery manager scrape" discovery=file msg="Error reading file" path=/opt/prometheus/etc/mdns-sd.json err="unexpected end of JSON input"
level=debug ts=2018-09-13T04:47:22.434966794Z caller=file.go:338 component="discovery manager scrape" discovery=file msg="file_sd refresh found file that should be removed" file=/opt/prometheus/etc/mdns-sd.json

The issues seems that mdsn.go writes the file in a non atomic manner.
If prometheus reads the file when mdns.go writes it, the json may be truncated at the precise time prometheus reads it and fails to decode it.

Before:

  • truncate the file with os.Create(). The file is still here but empty
  • fill it
  • flush and close it

Here I just used a lib to atomically write the new json file:

  • write a new temporary file next to the old one
  • flush and close it
  • atomically rename it to replace the old one

System used (Raspberry Pi 3B):
prometheus-mdns compiled with:

  • go1.10.3
  • GOOS=linux
  • GOARCH=arm
  • GOARM=7

Prometheus used:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant