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

promtail: Fix issue with dropping logs when a file based SD target's labels are updated #7016

Merged
merged 9 commits into from
Sep 14, 2022

Conversation

chodges15
Copy link
Contributor

What this PR does / why we need it:
Allows for modifying labels for a target and continuing to scrape logs afterwards.

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

Special notes for your reviewer:

Checklist

  • Documentation added
  • Tests updated
  • Is this an important fix or new feature? Add an entry in the CHANGELOG.md.
  • Changes that require user attention or interaction to upgrade are documented in docs/sources/upgrading/_index.md

@chodges15 chodges15 requested a review from a team as a code owner August 31, 2022 15:54
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@chodges15
Copy link
Contributor Author

this is ready for review now

@chodges15 chodges15 changed the title promtail: Fix issue with dropping logs when a target's labels are updated promtail: Fix issue with dropping logs when a file based SD target's labels are updated Sep 6, 2022
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@pull-request-size pull-request-size bot added size/L and removed size/M labels Sep 7, 2022
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@MasslessParticle MasslessParticle self-assigned this Sep 8, 2022
@MasslessParticle
Copy link
Contributor

MasslessParticle commented Sep 8, 2022

Thanks for submitting a PR!

I've made some suggestions but this looks reasonable.

Out of curiosity, when would a target's labels be changed while reading? Nvm, I see the linked issue.

@afayngelerindbx
Copy link
Contributor

I've made some suggestions but this looks reasonable.

Hi @MasslessParticle I don't see the suggestions you mentioned above. Is there something I need to do to see them?

// keep track of how many targets are using a fileEventWatcher
watcherUseCount := make(map[string]int, len(s.fileEventWatchers))
for _, target := range s.targets {
if _, ok := watcherUseCount[target.path]; !ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

we can always say watcherUserCount[target.path]++ here because 0 is the default value: https://go.dev/play/p/qotVnYhrlKK

k := target.path
_, ok := watcherUseCount[k]
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be written as

if _, ok := watcherUseCount[k]; !ok {
    level.Warn(s.log).Log("msg", "failed to find file event watcher", "path", k)
    continue
}

to avoid the following else clause

@MasslessParticle
Copy link
Contributor

Sorry about that, @afayngelerindbx! I didn't click the submit button 🤦

@afayngelerindbx
Copy link
Contributor

Sorry about that, @afayngelerindbx! I didn't click the submit button 🤦

No worries. Happens to me all the time

Copy link
Contributor

@cstyan cstyan left a comment

Choose a reason for hiding this comment

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

Minor comments; along with Travis' comments there's another opportunity to simplify things.

Reviewing this PR also makes me think it would be worth it to break the logic within sync into multiple functions.

@@ -373,7 +395,6 @@ func (s *targetSyncer) sync(groups []*targetgroup.Group, targetEventHandler chan
}
}
}
s.droppedTargets = dropped
Copy link
Contributor

Choose a reason for hiding this comment

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

moving this line doesn't seem necessary to change the behaviour of keeping or removing a file watcher, so lets not move it in this PR

Comment on lines +361 to +362
watcherUseCount := make(map[string]int, len(s.fileEventWatchers))
for _, target := range s.targets {
Copy link
Contributor

Choose a reason for hiding this comment

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

we could simplify/remove the need for this extra loop on every sync call by storing the watcherUseCount as a field in the targetSyncer struct

Copy link
Member

@owen-d owen-d left a comment

Choose a reason for hiding this comment

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

I'm going to approve this. There's some feedback around style/code construction, but the idea LGTM. Feel free to address those, but I'd like to merge this soon.

Thanks and sorry for the late review.

@owen-d owen-d merged commit d1f42ff into grafana:main Sep 14, 2022
lxwzy pushed a commit to lxwzy/loki that referenced this pull request Nov 7, 2022
…labels are updated (grafana#7016)

<!--  Thanks for sending a pull request!  Before submitting:

1. Read our CONTRIBUTING.md guide
2. Name your PR as `<Feature Area>: Describe your change`.
a. Do not end the title with punctuation. It will be added in the
changelog.
b. Start with an imperative verb. Example: Fix the latency between
System A and System B.
  c. Use sentence case, not title case.
d. Use a complete phrase or sentence. The PR title will appear in a
changelog, so help other people understand what your change will be.
3. Rebase your PR if it gets out of sync with main
-->

**What this PR does / why we need it**:
Allows for modifying labels for a target and continuing to scrape logs
afterwards.

**Which issue(s) this PR fixes**:
Fixes grafana#7001 

**Special notes for your reviewer**:

<!--
Note about CHANGELOG entries, if a change adds:
* an important feature
* fixes an issue present in a previous release, 
* causes a change in operation that would be useful for an operator of
Loki to know
then please add a CHANGELOG entry.

For documentation changes, build changes, simple fixes etc please skip
this step. We are attempting to curate a changelog of the most relevant
and important changes to be easier to ingest by end users of Loki.

Note about the upgrade guide, if this changes:
* default configuration values
* metric names or label names
* changes existing log lines such as the metrics.go query output line
* configuration parameters 
* anything to do with any API
* any other change that would require special attention or extra steps
to upgrade
Please document clearly what changed AND what needs to be done in the
upgrade guide.
-->
**Checklist**
- [ ] Documentation added
- [x] Tests updated
- [x] Is this an important fix or new feature? Add an entry in the
`CHANGELOG.md`.
- [ ] Changes that require user attention or interaction to upgrade are
documented in `docs/sources/upgrading/_index.md`
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.

Promtail: Log scraping stops when changing label value for existing target in file_sd_config
6 participants