-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logging: Add HTTP API to change the log level at runtime (#9357)
**What this PR does / why we need it**: To be able to change the log level at runtime, particularly from info to debug to enable debugging information during an incident without restarting Loki. **Which issue(s) this PR fixes**: Fixes #6805 **Special notes for your reviewer**: **Checklist** - [x] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [x] Documentation added - [x] Tests updated - [x] `CHANGELOG.md` updated - [x] Changes that require user attention or interaction to upgrade are documented in `docs/sources/upgrading/_index.md`: No such changes --------- Co-authored-by: J Stickler <julie.stickler@grafana.com>
- Loading branch information
Showing
5 changed files
with
146 additions
and
4 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package log | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/weaveworks/common/logging" | ||
) | ||
|
||
func TestLevelHandler(t *testing.T) { | ||
var lvl logging.Level | ||
err := lvl.Set("info") | ||
assert.NoError(t, err) | ||
plogger = &prometheusLogger{ | ||
baseLogger: log.NewLogfmtLogger(io.Discard), | ||
} | ||
|
||
// Start test http server | ||
go func() { | ||
err := http.ListenAndServe(":8080", LevelHandler(&lvl)) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
testCases := []struct { | ||
testName string | ||
targetLogLevel string | ||
expectedResponse string | ||
expectedLogLevel string | ||
expectedStatusCode int | ||
}{ | ||
{"GetLogLevel", "", `{"message":"Current log level is info"}`, "info", 200}, | ||
{"PostLogLevelInvalid", "invalid", `{"message":"unrecognized log level \"invalid\"", "status":"failed"}`, "info", 400}, | ||
{"PostLogLevelEmpty", "", `{"message":"unrecognized log level \"\"", "status":"failed"}`, "info", 400}, | ||
{"PostLogLevelDebug", "debug", `{"status": "success", "message":"Log level set to debug"}`, "debug", 200}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.testName, func(t *testing.T) { | ||
var ( | ||
resp *http.Response | ||
err error | ||
) | ||
|
||
if strings.HasPrefix(testCase.testName, "Get") { | ||
resp, err = http.Get("http://localhost:8080/") | ||
} else if strings.HasPrefix(testCase.testName, "Post") { | ||
resp, err = http.PostForm("http://localhost:8080/", url.Values{"log_level": {testCase.targetLogLevel}}) | ||
} | ||
assert.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
assert.NoError(t, err) | ||
assert.JSONEq(t, testCase.expectedResponse, string(body)) | ||
assert.Equal(t, testCase.expectedStatusCode, resp.StatusCode) | ||
assert.Equal(t, testCase.expectedLogLevel, lvl.String()) | ||
}) | ||
} | ||
} |