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

Restart Docker target if stopped. #5280

Merged
merged 5 commits into from
Feb 1, 2022

Conversation

jeschkies
Copy link
Contributor

What this PR does / why we need it:
The Docker target would not reconnect to a container if it restarted and
kept the same ID. This should introduces a startIfNotRunning method
that will be called to restart scraping the container's logs.

Which issue(s) this PR fixes:
Fixes #5259

Special notes for your reviewer:

Checklist

  • Documentation added
  • Tests updated
  • Add an entry in the CHANGELOG.md about the changes.

Summary:
The Docker target would not reconnect to a container if it restarted and
kept the same ID. This should introduces a `startIfNotRunning` method
that will be called to restart scraping the container's logs.

Fixes grafana#5259
@jeschkies jeschkies requested a review from a team as a code owner January 31, 2022 14:56
func (t *Target) startIfNotRunning() {
if t.running.CAS(false, true) {
ctx, cancel := context.WithCancel(context.Background())
t.cancel = cancel
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the one bit that would bother me. However, I claim it's thread safe as long as it's threadsafe to access t.cancel concurrently. It will never be nil for Stop.

Copy link
Contributor

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, two non-blocking suggestions

Comment on lines 75 to 76
t, ok := tg.targets[id]
if ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an opportunity to use the idiom

	if t, ok := tg.targets[id]; ok {
		level.Debug(tg.logger).Log("msg", "container target already exists", "container", id)
		t.startIfNotRunning()
		return nil
	}

@jeschkies jeschkies merged commit 54bc2dc into grafana:main Feb 1, 2022
@jeschkies jeschkies deleted the karsten/docker-restart branch February 1, 2022 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Docker service discovery - logging interrupted when a container is restarted
3 participants