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

WIP: Feature/extra template context #95

Merged
merged 6 commits into from
May 4, 2020

Conversation

Skeen
Copy link
Contributor

@Skeen Skeen commented Apr 21, 2020

This PR introduces a new pre-defined label variable, namely extra, which contains the entire JSON object parsed from the input, when using input type webhook with format being either json_single or json_bulk.

It is flexible enough to be utilized by any input, which may want to provide a map as extra context for the label templating step.

The majority of changes has been made in the initial commit 6ee14ae, and simply involves changing the type of additionalFields from map[string]string to map[string]interface{} in order to support proper recursive data structures as extra context.

An argument could have been made to go all the way to data interface{} as this is the type utilized for the Execute method's data argument on the Template type, however map[string]interface{} seemed like a good compromise.

@Skeen
Copy link
Contributor Author

Skeen commented Apr 21, 2020

I have tested the change using:

global:
  config_version: 3
input:
  type: webhook
  webhook_path: /webhook
  webhook_format: json_single
  webhook_json_selector: .message
  webhook_text_bulk_separator: "\n\n"
metrics:
- type: counter
  name: user_logins
  help: Total number of user logins, by user and ip.
  match: 'Login occured'
  labels:
    user: '{{ index .extra "user" }}'
    ip: '{{ index .extra "ip" }}'
server:
  host: "[::]"
  port: 9144

Started as:

go run grok_exporter.go -config example/config_context_json.yml

With the following log messages being posted:

curl localhost:9144/webhook --data '{"message": "Login occured", "user": "Skeen", "ip": "1.1.1.1"}'

And this as output:

# HELP user_logins Total number of user logins, by user and ip.
# TYPE user_logins counter
user_logins{ip="1.1.1.1",user="Skeen"} 1

@Skeen Skeen changed the title Feature/extra template context WIP: Feature/extra template context Apr 21, 2020
@Skeen
Copy link
Contributor Author

Skeen commented Apr 21, 2020

Assuming this is something you'll consider merging, I will fix the broken tests, get the build to succeed, and then remove the WIP: prefix on the PR.

@fstab
Copy link
Owner

fstab commented Apr 21, 2020

Thanks a lot for this! I'll look into this in the next couple of days and get back to you.

@fstab
Copy link
Owner

fstab commented Apr 29, 2020

Thanks a lot, this sounds like a useful feature for users of the webhook users. I'm happy to merge it once it's finalized.

@Skeen
Copy link
Contributor Author

Skeen commented Apr 30, 2020

Cool, I'll finish the work on it.

@Skeen
Copy link
Contributor Author

Skeen commented Apr 30, 2020

I don't seem to be able to reproduce the failing test in the appveyor/pr check.

skeen@ibuprofen ~/p/grok_exporter (feature/extra_template_context)> go clean -testcache
skeen@ibuprofen ~/p/grok_exporter (feature/extra_template_context)> go test ./...
?   	github.com/fstab/grok_exporter	[no test files]
ok  	github.com/fstab/grok_exporter/config	0.003s
ok  	github.com/fstab/grok_exporter/config/v2	0.011s
ok  	github.com/fstab/grok_exporter/config/v3	0.015s
ok  	github.com/fstab/grok_exporter/exporter	0.681s
ok  	github.com/fstab/grok_exporter/oniguruma	0.010s
ok  	github.com/fstab/grok_exporter/tailer	23.284s
?   	github.com/fstab/grok_exporter/tailer/fswatcher	[no test files]
ok  	github.com/fstab/grok_exporter/tailer/glob	0.003s
ok  	github.com/fstab/grok_exporter/template	0.004s

Can you assist me with finding out what is going afoul here?

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 67.231% when pulling e4e0ccc on magenta-aps:feature/extra_template_context into 58c93fb on fstab:master.

@fstab
Copy link
Owner

fstab commented May 4, 2020

Unfortunately, the Windows file tailor tests are flaky:

fswatcher_test.go:652: C:\Users\appveyor\AppData\Local\Temp\1\grok_exporter807922133\logdir still contains file logfile.log after mv.

The test is simulating logrotate moving the logfile away, but after the mv operation the file is still in the directory. I have no idea why this happens, because the os.Rename(...) call does not return an error. You can ignore this. Re-running the test manually in Appveyor helps, but I think you need to log in to do that. As it is test code that fails and not production code, I don't think it's a big risk to ignore this.

If you have any idea how os.Rename(...) can be successful but the file is still there after that, I'd be grateful, but this has nothing to do with your changes.

@fstab fstab merged commit 34a0eab into fstab:master May 4, 2020
@fstab
Copy link
Owner

fstab commented May 4, 2020

Thanks a lot!!!

@fstab
Copy link
Owner

fstab commented May 4, 2020

P.S.: After the merge Appveyor built without error again without any code change https://ci.appveyor.com/project/fstab/grok-exporter/builds/32644306

@Skeen Skeen deleted the feature/extra_template_context branch May 5, 2020 07:18
@Skeen
Copy link
Contributor Author

Skeen commented May 5, 2020

...

If you have any idea how os.Rename(...) can be successful but the file is still there after that, I'd be grateful, but this has nothing to do with your changes.

Are we sure that os.rename(...) is indeed atomic on Windows?

I don't believe that is the case in general, as Python calls MoveFileW() on Windows, which is atomic in specific cases, but not as a general case (such as with network mounted file-systems).
On Windows to ensure atomic renaming, you'd have to create a transaction with CreateTransaction() followed by a call to MoveFileTransacted(), and finally a commit.

So in general, I would not assume a os.rename call to be atomic on Windows.

Potential solutions:

  1. Find a library which does transactional file-moves, and accept that the tests will only pass on Windows Vista and higher.
  2. Add a sleep / busy wait loop, continuously checking whether the file has actually been moved yet, and leaving the loop after that happens.

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.

3 participants