Closed ankur22 closed 1 year ago
Taking a brief look at this, k6 has a Loki implementation of a Logrus hook which is initialized and set based on configuration defined by cmdline flags. So the logs are pushed to Loki through this hook via HTTP POST requests to the configured endpoint.
If we focus on the labels, which are set when building the Loki message from the Logrus Entry here, my understanding is that the fields that are set in the Logrus entry are compared with the configured allowed labels, if the field is allowed then it is set as a Loki label, which means that it will be indexed by Loki.
Based on this, if my understanding is correct, possibly the required changes in k6 browser to fulfill this issue would be:
logrus.FieldLogger
in order to always include the field corresponding to the "origin/source/..." label defined in point 1.allowedLabels
flag is not set, I believe all logrus entry fields are sent as labels).See also: https://k6.io/blog/using-loki-to-store-and-query-k6-logs/
I have verified that the explanation from https://github.com/grafana/xk6-browser/issues/934#issuecomment-1600575890 is correct and for what I understand we just need to add a label in our logger implementation here. E.g.:
diff --git a/log/logger.go b/log/logger.go
index 374e26c..78012ae 100644
--- a/log/logger.go
+++ b/log/logger.go
@@ -101,6 +101,7 @@ func (l *Logger) Logf(level logrus.Level, category string, msg string, args ...a
"category": category,
"elapsed": fmt.Sprintf("%d ms", elapsed),
"goroutine": goRoutineID(),
+ "module": "browser",
}
if l.iterID != "" && l.GetLevel() > logrus.InfoLevel {
fields["iteration_id"] = l.iterID
You can test this by running Loki locally with the following setup:
./
- loki-config.yaml
- promtail-local-config.yaml
- docker-compose.yaml
Where:
Such as:
docker-compose up
And executing a k6 browser test (with debug
enabled so we get more logs) such as:
K6_VERSION=master xk6 build --with github.com/grafana/xk6-browser="/path/to/xk6-browser/repo/with/logger/diff"
K6_BROWSER_DEBUG=true ./k6 run --log-output=loki=http://localhost:3100/loki/api/v1/push,label.testlabel=browser /path/to/test/script.js
Then we should be able to access Grafana instance configured with the Loki datasource at http://localhost:3000
, access the Explore
tab and find the k6 browser logs for the test filtering by labels module=browser
(set in src code) or testlabel=browser
(set in cmdline args).
@Carlos or @DefCon-007 I believe this request came from the backend team. Can you confirm that this is the desired result? Also, I'm not sure if there is a "labeling schema" already in place for other extensions. Any considerations about this or the specific label naming for k6 browser @mstoykov ?
From what I understood, it will require adding a new label (module
) for the logs on the k6 browser side, a small optional suggestion from my side would be to maybe use extension
as a label so that in the future we can distinguish logs from all the extensions very easily.
note: I haven't read the whole thread in depth, but did some quick searching through it and scan reeding
Also, I'm not sure if there is a "labeling schema" already in place for other extensions. Any considerations about this or the specific label naming for k6 browser @mstoykov ?
There isn't any labeling schema AFAIK. We decided not to have tons of labels as at least loki ~v1.x had problems with high cardinality, and we didn't want to get this overwhelmed.
The relevant currently enabled labels in the cloud are:
allowedLabels=[lz,level,test_run_id,instance_id,source]
As you can see there is a label called source
that seems pretty okay to be used, the current (known to me values) are:
console.log
and co.--http-debug
There are also two cases where browser already uses it:
https://github.com/grafana/xk6-browser/blob/44fce67d847b401a5a79260c709918ae52744430/common/frame_session.go#L758
https://github.com/grafana/xk6-browser/blob/44fce67d847b401a5a79260c709918ae52744430/common/frame_session.go#L585
There really is no problem for there to be a new label "extension" or "module" - but I would expect in most cases this won't be used as much and source
seems to cover most of the cases already :shrug:
And you will need to go edit every release on staging and production for the label to stick. While you won't need to that for source
.
When working with both the existing
k6
modules and browser module, logs currently can't be easily discerned from each other. We want to make this easier by tagging/labelling the logs so that we can easily separate the logs from the different origins. What needs to be clarified is whether we can tag our logs with a new tag with logrus or whether we need to do anything with loki. Once that has been clarified it needs to be tested end to end to ensure everyone that needs to query browser module logs can do so.