Skip to content

feat(prometheus): extra labels from nginx var for http request metrics#7549

Merged
spacewander merged 10 commits into
apache:masterfrom
kingluo:prometheus_ngx_var_labels
Aug 1, 2022
Merged

feat(prometheus): extra labels from nginx var for http request metrics#7549
spacewander merged 10 commits into
apache:masterfrom
kingluo:prometheus_ngx_var_labels

Conversation

@kingluo

@kingluo kingluo commented Jul 27, 2022

Copy link
Copy Markdown
Contributor

close #4273

Description

Add extra labels from nginx variables for http request metrics.

Fixes #4273

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

Comment thread conf/config-default.yaml Outdated
export_addr:
ip: 127.0.0.1
port: 9091
# ngx_var_labels:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we don't need to highlight the var. Instead, we can just use the name custom_labels.

Comment thread conf/config-default.yaml Outdated
export_addr:
ip: 127.0.0.1
port: 9091
# custom_labels:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would be better to use:

metrics:
    xxx:
       extra_labels:
           label_name_xxx: $label_var
     yyy:

So we can add more options to a metric in the future. And we don't need to require the label name should be the variable name.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And please add a comment to show that the commented configurations are the example but not the default value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The labels of extra_labels should be a list, because fields of dictionary in lua table are out of order, which would cause the metric full name may change each time it reloads the conf.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can provide order on the implementation side, for example sorting them before using them.

Comment thread apisix/plugins/prometheus/exporter.lua Outdated
for _, name in ipairs(labels) do
local val = ctx.var[name]
if val == nil then
val = "nil"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would suggest using "", which is language-independent.

Comment thread t/plugin/prometheus4.t Outdated
end
}
}
--- error_code: 200

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually we don't need to specify --- error_code: 200 as it is checked by default

Comment thread t/plugin/prometheus4.t Outdated
custom_labels:
http_status:
- dummy
bandwidth:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't need the bandwidth?

Comment thread docs/en/latest/plugins/prometheus.md Outdated
tzssangglass
tzssangglass previously approved these changes Jul 29, 2022
Comment thread t/plugin/prometheus4.t Outdated
- upstream_status: $upstream_status
--- request
GET /hello
--- error_code: 200

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually we don't need to specify --- error_code: 200 as it is checked by default

Comment thread t/plugin/prometheus4.t Outdated



=== TEST 6: fetch the prometheus metric data, with nil label

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is no longer a nil label?

Comment thread docs/en/latest/plugins/prometheus.md Outdated

### Specifying `metrics`

For http request related metrics, you could specify extra labels, which match the nginx variables.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's use APISIX variable like elsewhere in the doc.

@spacewander
spacewander merged commit 3d9e2fd into apache:master Aug 1, 2022
Liu-Junlin pushed a commit to Liu-Junlin/apisix that referenced this pull request Nov 4, 2022
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.

discuss: enrich the Available metrics of plugin prometheus

5 participants