Skip to content

Commit

Permalink
Performance optimization for live debugging feature (#2294)
Browse files Browse the repository at this point in the history
* Performance optimization for live debugging feature

* Moving the config to the livedebugging block

Remove config from docs

* Fix test cases

* Remove the config changes
  • Loading branch information
ravishankar15 authored Jan 3, 2025
1 parent eaf4bc6 commit df1d04c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Main (unreleased)
- Add three new stdlib functions to_base64, from_URLbase64 and to_URLbase64 (@ravishankar15)
- Add `ignore_older_than` option for local.file_match (@ravishankar15)
- Add livedebugging support for `discover.relabel` (@ravishankar15)
- Performance optimization for live debugging feature (@ravishankar15)

- Upgrade `github.com/goccy/go-json` to v0.10.4, which reduces the memory consumption of an Alloy instance by 20MB.
If Alloy is running certain otelcol components, this reduction will not apply. (@ptodev)
Expand Down
6 changes: 3 additions & 3 deletions docs/sources/reference/config-blocks/livedebugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ livedebugging {

The following arguments are supported:

| Name | Type | Description | Default | Required |
| --------- | ------ | ----------------------------------- | ------- | -------- |
| `enabled` | `bool` | Enables the live debugging feature. | `false` | no |
| Name | Type | Description | Default | Required |
| -------------------- | ----- | --------------------------------------------------------------- | ------- | -------- |
| `enabled` | `bool`| Enables the live debugging feature. | `false` | no |

[debug]: ../../../troubleshoot/debug/
12 changes: 6 additions & 6 deletions internal/component/loki/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestJSONLabelsStage(t *testing.T) {
// The third stage will set some labels from the extracted values above.
// Again, if the value is empty, it is inferred that we want to use the
// populate the label with extracted value of the same name.
stg := `stage.json {
stg := `stage.json {
expressions = {"output" = "log", stream = "stream", timestamp = "time", "extra" = "" }
drop_malformed = true
}
Expand All @@ -62,7 +62,7 @@ func TestJSONLabelsStage(t *testing.T) {
source = "extra"
}
stage.labels {
values = {
values = {
stream = "",
user = "",
ts = "timestamp",
Expand Down Expand Up @@ -679,7 +679,7 @@ func TestLeakyUpdate(t *testing.T) {
numLogsToSend := 1

cfg1 := `
stage.metrics {
stage.metrics {
metric.counter {
name = "paulin_test1"
action = "inc"
Expand All @@ -688,7 +688,7 @@ func TestLeakyUpdate(t *testing.T) {
}` + forwardArgs

cfg2 := `
stage.metrics {
stage.metrics {
metric.counter {
name = "paulin_test2"
action = "inc"
Expand Down Expand Up @@ -731,7 +731,7 @@ func TestMetricsStageRefresh(t *testing.T) {
numLogsToSend := 3

cfgWithMetric := `
stage.metrics {
stage.metrics {
metric.counter {
name = "paulin_test"
action = "inc"
Expand Down Expand Up @@ -776,7 +776,7 @@ func TestMetricsStageRefresh(t *testing.T) {
// We try having a metric with the same name as before so that we can see if there
// is some sort of double registration error for that metric.
cfgWithTwoMetrics := `
stage.metrics {
stage.metrics {
metric.counter {
name = "paulin_test_3"
action = "inc"
Expand Down
10 changes: 6 additions & 4 deletions internal/web/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path"
"strconv"
"strings"
"time"

"github.com/google/uuid"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -165,13 +166,11 @@ func getClusteringPeersHandler(host service.Host) http.HandlerFunc {
}
}

func liveDebugging(host service.Host, callbackManager livedebugging.CallbackManager) http.HandlerFunc {
func liveDebugging(_ service.Host, callbackManager livedebugging.CallbackManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
componentID := livedebugging.ComponentID(vars["id"])

// Buffer of 1000 entries to handle load spikes and prevent this functionality from eating up too much memory.
// TODO: in the future we may want to make this value configurable to handle heavy load
dataCh := make(chan string, 1000)
ctx := r.Context()

Expand Down Expand Up @@ -200,9 +199,12 @@ func liveDebugging(host service.Host, callbackManager livedebugging.CallbackMana
return
}

flushTicker := time.NewTicker(time.Second)

defer func() {
close(dataCh)
callbackManager.DeleteCallback(id, componentID)
flushTicker.Stop()
}()

for {
Expand All @@ -216,7 +218,7 @@ func liveDebugging(host service.Host, callbackManager livedebugging.CallbackMana
if writeErr != nil {
return
}
// TODO: flushing at a regular interval might be better performance wise
case <-flushTicker.C:
w.(http.Flusher).Flush()
case <-ctx.Done():
return
Expand Down

0 comments on commit df1d04c

Please sign in to comment.