-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Add tailing metrics #6059
Merged
cyriltovena
merged 3 commits into
grafana:main
from
MasslessParticle:tpatterson/add-tail-metrics
May 2, 2022
Merged
Add tailing metrics #6059
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package querier | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
type Metrics struct { | ||
tailsActive prometheus.Gauge | ||
tailedStreamsActive prometheus.Gauge | ||
tailedBytesTotal prometheus.Counter | ||
} | ||
|
||
func NewMetrics(r prometheus.Registerer) *Metrics { | ||
return &Metrics{ | ||
tailsActive: promauto.With(r).NewGauge(prometheus.GaugeOpts{ | ||
Name: "loki_querier_tail_active", | ||
Help: "Number of active tailers", | ||
}), | ||
tailedStreamsActive: promauto.With(r).NewGauge(prometheus.GaugeOpts{ | ||
Name: "loki_querier_tail_active_streams", | ||
Help: "Number of active streams being tailed", | ||
}), | ||
tailedBytesTotal: promauto.With(r).NewCounter(prometheus.CounterOpts{ | ||
Name: "loki_querier_tail_bytes_total", | ||
Help: "total bytes tailed", | ||
}), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,10 @@ type Tailer struct { | |
currEntry logproto.Entry | ||
currLabels string | ||
|
||
// keep track of the streams for metrics about active streams | ||
seenStreams map[uint64]struct{} | ||
seenStreamsMtx sync.Mutex | ||
|
||
tailDisconnectedIngesters func([]string) (map[string]logproto.Querier_TailClient, error) | ||
|
||
querierTailClients map[string]logproto.Querier_TailClient // addr -> grpc clients for tailing logs from ingesters | ||
|
@@ -55,6 +59,7 @@ type Tailer struct { | |
// if we are not seeing any response from ingester, | ||
// how long do we want to wait by going into sleep | ||
waitEntryThrottle time.Duration | ||
metrics *Metrics | ||
} | ||
|
||
func (t *Tailer) readTailClients() { | ||
|
@@ -95,8 +100,11 @@ func (t *Tailer) loop() { | |
|
||
// Read as much entries as we can (up to the max allowed) and populate the | ||
// tail response we'll send over the response channel | ||
tailResponse := new(loghttp.TailResponse) | ||
entriesCount := 0 | ||
var ( | ||
tailResponse = new(loghttp.TailResponse) | ||
entriesCount = 0 | ||
entriesSize = 0 | ||
) | ||
|
||
for ; entriesCount < maxEntriesPerTailResponse && t.next(); entriesCount++ { | ||
// If the response channel channel is blocked, we drop the current entry directly | ||
|
@@ -106,6 +114,7 @@ func (t *Tailer) loop() { | |
continue | ||
} | ||
|
||
entriesSize += len(t.currEntry.Line) | ||
tailResponse.Streams = append(tailResponse.Streams, logproto.Stream{ | ||
Labels: t.currLabels, | ||
Entries: []logproto.Entry{t.currEntry}, | ||
|
@@ -151,6 +160,7 @@ func (t *Tailer) loop() { | |
|
||
select { | ||
case t.responseChan <- tailResponse: | ||
t.metrics.tailedBytesTotal.Add(float64(entriesSize)) | ||
if len(droppedEntries) > 0 { | ||
droppedEntries = make([]loghttp.DroppedEntry, 0) | ||
} | ||
|
@@ -239,13 +249,18 @@ func (t *Tailer) next() bool { | |
|
||
t.currEntry = t.openStreamIterator.Entry() | ||
t.currLabels = t.openStreamIterator.Labels() | ||
t.recordStream(t.openStreamIterator.StreamHash()) | ||
|
||
return true | ||
} | ||
|
||
func (t *Tailer) close() error { | ||
t.streamMtx.Lock() | ||
defer t.streamMtx.Unlock() | ||
|
||
t.metrics.tailsActive.Dec() | ||
t.metrics.tailedStreamsActive.Sub(t.activeStreamCount()) | ||
|
||
t.stopped = true | ||
return t.openStreamIterator.Close() | ||
} | ||
|
@@ -264,25 +279,48 @@ func (t *Tailer) getCloseErrorChan() <-chan error { | |
return t.closeErrChan | ||
} | ||
|
||
func (t *Tailer) recordStream(id uint64) { | ||
t.seenStreamsMtx.Lock() | ||
defer t.seenStreamsMtx.Unlock() | ||
|
||
if _, ok := t.seenStreams[id]; ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're only writing once there's a lot of chances that |
||
return | ||
} | ||
|
||
t.seenStreams[id] = struct{}{} | ||
t.metrics.tailedStreamsActive.Inc() | ||
} | ||
|
||
func (t *Tailer) activeStreamCount() float64 { | ||
t.seenStreamsMtx.Lock() | ||
defer t.seenStreamsMtx.Unlock() | ||
|
||
return float64(len(t.seenStreams)) | ||
} | ||
|
||
func newTailer( | ||
delayFor time.Duration, | ||
querierTailClients map[string]logproto.Querier_TailClient, | ||
historicEntries iter.EntryIterator, | ||
tailDisconnectedIngesters func([]string) (map[string]logproto.Querier_TailClient, error), | ||
tailMaxDuration time.Duration, | ||
waitEntryThrottle time.Duration, | ||
m *Metrics, | ||
) *Tailer { | ||
t := Tailer{ | ||
openStreamIterator: iter.NewMergeEntryIterator(context.Background(), []iter.EntryIterator{historicEntries}, logproto.FORWARD), | ||
querierTailClients: querierTailClients, | ||
delayFor: delayFor, | ||
responseChan: make(chan *loghttp.TailResponse, maxBufferedTailResponses), | ||
closeErrChan: make(chan error), | ||
seenStreams: make(map[uint64]struct{}), | ||
tailDisconnectedIngesters: tailDisconnectedIngesters, | ||
tailMaxDuration: tailMaxDuration, | ||
waitEntryThrottle: waitEntryThrottle, | ||
metrics: m, | ||
} | ||
|
||
t.metrics.tailsActive.Inc() | ||
t.readTailClients() | ||
go t.loop() | ||
return &t | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't necessarily the number of bytes, but it is consistent with how we calculate similar metrics in other components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why isn't this the number of bytes? wouldn't each character take up a byte? either way I agree keep it consistent, but maybe we want to consider an issue to fix this across the code base (as I think actually knowing bytes is helpful)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each character takes up 1+ bytes. The string 世界 has 2 characters but 6 bytes. That said it looks like Golang
len
gives back bytes rather characters. I can't believe I didn't know this.I wonder how many bugs I've caused with it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haha, go was clearly designed for dummies like me who only use English characters and still think ASCII is the only encoding out there.