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

Add support for custom labels #38

Merged
merged 1 commit into from
Aug 30, 2022
Merged

Add support for custom labels #38

merged 1 commit into from
Aug 30, 2022

Conversation

chaudum
Copy link
Collaborator

@chaudum chaudum commented Jun 10, 2022

Passing a Label object as fifth argument to the Config constructor allows you to override the built-in labels by custom ones.

The custom labels map requires at least the format key, which needs to hold one of the supported log formats. The instance key will override the otherwise automatically generated instance label.

Example:

const labels = loki.Labels({
  "format": ["logfmt"], // must contain at least one of the supported log formats
  "os": ["linux"],
  "cluster": ["k3d", "minikube"],
  "namespace": ["loki-prod", "loki-dev"],
  "container": ["distributor", "ingester", "querier", "query-frontend", "query-scheduler", "index-gateway", "compactor"],
  "instance": ["localhost"], // overrides the `instance` label which is otherwise derived from the hostname and VU
});

const conf = new loki.Config(BASE_URL, 10000, 1.0, {}, labels);

Signed-off-by: Christian Haudum christian.haudum@gmail.com

@chaudum chaudum marked this pull request as ready for review June 15, 2022 06:24
```js
import loki from 'k6/x/loki';
let labels = loki.Labels({
"format": ["json", "logfmt"], // must contain at least one of the supported log formats
Copy link
Contributor

Choose a reason for hiding this comment

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

Is format a special kind of label that is send to loki either way and it know what to do with it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it is. The label defines the format of the log line.
Because of this, we can make sure, that if we filter on that label when querying, the log lines can be parsed.

@CLAassistant
Copy link

CLAassistant commented Jun 15, 2022

CLA assistant check
All committers have signed the CLA.

Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
@chaudum chaudum enabled auto-merge (rebase) August 30, 2022 08:52
Copy link
Contributor

@vlad-diachenko vlad-diachenko left a comment

Choose a reason for hiding this comment

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

LGTM. only one nit

stream := &logproto.Stream{Labels: labels.String()}
batch.Streams[stream.Labels] = stream

var now time.Time
logFmt := string(labels[model.LabelName("format")])
if !isValidLogFormat(logFmt) {
panic(fmt.Sprintf("%s is not a valid log format", logFmt))
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure that we need panic here. can we use a construction like this:

common.Throw(rt, fmt.Errorf("%s is not a valid log format", logFmt))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed in #45

@chaudum chaudum merged commit 21599bf into main Aug 30, 2022
@chaudum chaudum deleted the chaudum/custom-labels-2 branch August 30, 2022 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants