You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following example demonstrates how you can disable the Prometheus export server that, by default, exposes an endpoint on port `9091`, and expose APISIX Prometheus metrics on a new public API endpoint on port `9080`, which APISIX uses to listen to other client requests.
294
294
295
+
:::caution
296
+
297
+
If a large quantity of metrics are being collected, the Plugin could take up a significant amount of CPU resources for metric computations and negatively impact the processing of regular requests.
298
+
299
+
To address this issue, APISIX uses [privileged agent](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/process.md#enable_privileged_agent) and offloads metric computations to a separate process. This optimization applies automatically if you use the metric endpoint configured in the configuration files, as demonstrated [above](#get-apisix-metrics). When you expose the metric endpoint with the `public-api` Plugin, the offloading and cached metric serving still apply; however, the endpoint is then exposed on the public listener and requests to it go through the normal public API request path, which can add request-path overhead compared with the dedicated export server.
300
+
301
+
:::
302
+
295
303
Disable the Prometheus export server in the configuration file and reload APISIX for changes to take effect:
296
304
297
305
```yaml title="conf/config.yaml"
@@ -300,7 +308,7 @@ plugin_attr:
300
308
enable_export_server: false
301
309
```
302
310
303
-
Next, create a Route with [`public-api`](../../../en/latest/plugins/public-api.md) Plugin and expose a public API endpoint for APISIX metrics:
311
+
Next, create a Route with [`public-api`](./public-api.md) Plugin and expose a public API endpoint for APISIX metrics:
304
312
305
313
```shell
306
314
curl "http://127.0.0.1:9180/apisix/admin/routes/prometheus-metrics" -X PUT \
@@ -434,7 +442,7 @@ curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
434
442
-H "X-API-KEY: ${admin_key}" \
435
443
-d '{
436
444
"id": "prometheus-route",
437
-
Include the following configurations in the configuration file to add labels for metrics and reload APISIX for changes to take effect:
445
+
"uri": "/get",
438
446
"name": "extra-label",
439
447
"plugins": {
440
448
"prometheus": {}
@@ -494,7 +502,7 @@ Create a Stream Route with the `prometheus` Plugin:
494
502
curl "http://127.0.0.1:9180/apisix/admin/stream_routes" -X PUT \
495
503
-H "X-API-KEY: ${admin_key}" \
496
504
-d '{
497
-
Include the following configurations in `config.yaml` to enable stream proxy and enable `prometheus` Plugin for stream proxy. Reload APISIX for changes to take effect:
Copy file name to clipboardExpand all lines: docs/en/latest/plugins/serverless.md
+30-51Lines changed: 30 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ keywords:
5
5
- API Gateway
6
6
- Plugin
7
7
- Serverless
8
-
description: This document contains information about the Apache APISIX serverless Plugin.
8
+
description: The serverless plugins, `serverless-pre-function` and `serverless-post-function`, allow you to dynamically run Lua functions at specified phases in APISIX.
9
9
---
10
10
11
11
<!--
@@ -35,12 +35,12 @@ Both Plugins have the same attributes.
35
35
36
36
## Attributes
37
37
38
-
| Name | Type | Required | Default | Valid values | Description |
| phase | string | False |["access"]|["rewrite", "access", "header_filter", "body_filter", "log", "before_proxy"]| Phase before or after which the serverless function is executed. |
41
-
| functions | array[string]| True ||| List of functions that are executed sequentially. |
38
+
| Name | Type | Required | Default | Valid values | Description |
| phase | string | False | "access" |["rewrite", "access", "header_filter", "body_filter", "log", "before_proxy"]| Phase before or after which the serverless function is executed. |
41
+
| functions | array[string]| True ||| List of functions that are executed sequentially. |
42
42
43
-
:::info IMPORTANT
43
+
:::note
44
44
45
45
Only Lua functions are allowed here and not other Lua code.
46
46
@@ -69,21 +69,16 @@ local count = 1
69
69
ngx.say(count)
70
70
```
71
71
72
-
:::
73
-
74
-
:::note
75
-
76
72
From v2.6, `conf` and `ctx` are passed as the first two arguments to a serverless function like regular Plugins.
77
73
78
74
Prior to v2.12.0, the phase `before_proxy` was called `balancer`. This was updated considering that this method would run after `access` and before the request goes Upstream and is unrelated to `balancer`.
79
75
80
76
:::
81
77
82
-
## Enable Plugin
83
-
84
-
The example below enables the Plugin on a specific Route:
78
+
## Examples
85
79
86
80
:::note
81
+
87
82
You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command:
88
83
89
84
```bash
@@ -92,53 +87,37 @@ admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"/
92
87
93
88
:::
94
89
90
+
The following example enables `serverless-pre-function` and `serverless-post-function` Plugins on a Route:
91
+
95
92
```shell
96
-
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
97
-
{
93
+
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
94
+
-H "X-API-KEY: ${admin_key}" \
95
+
-d '{
96
+
"id": "serverless-route",
98
97
"uri": "/index.html",
99
98
"plugins": {
100
-
"serverless-pre-function": {
101
-
"phase": "rewrite",
102
-
"functions": ["return function() ngx.log(ngx.ERR, \"serverless pre function\"); end"]
103
-
},
104
-
"serverless-post-function": {
105
-
"phase": "rewrite",
106
-
"functions": ["return function(conf, ctx) ngx.log(ngx.ERR, \"match uri \", ctx.curr_req_matched and ctx.curr_req_matched._path); end"]
107
-
}
99
+
"serverless-pre-function": {
100
+
"phase": "rewrite",
101
+
"functions": ["return function() ngx.log(ngx.ERR, \"serverless pre function\"); end"]
102
+
},
103
+
"serverless-post-function": {
104
+
"phase": "rewrite",
105
+
"functions": ["return function(conf, ctx) ngx.log(ngx.ERR, \"match uri \", ctx.curr_req_matched and ctx.curr_req_matched._path); end"]
106
+
}
108
107
},
109
108
"upstream": {
110
-
"type": "roundrobin",
111
-
"nodes": {
112
-
"127.0.0.1:1980": 1
113
-
}
109
+
"type": "roundrobin",
110
+
"nodes": {
111
+
"127.0.0.1:1980": 1
112
+
}
114
113
}
115
-
}'
114
+
}'
116
115
```
117
116
118
-
## Example usage
119
-
120
-
Once you have configured the Plugin as shown above, you can make a request as shown below:
117
+
Send a request to the Route:
121
118
122
119
```shell
123
-
curl -i http://127.0.0.1:9080/index.html
120
+
curl -i "http://127.0.0.1:9080/index.html"
124
121
```
125
122
126
-
You will find a message "serverless pre-function" and "match uri /index.html" in the error.log.
127
-
128
-
## Delete Plugin
129
-
130
-
To remove the `serverless` Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.
131
-
132
-
```shell
133
-
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
134
-
{
135
-
"methods": ["GET"],
136
-
"uri": "/index.html",
137
-
"upstream": {
138
-
"type": "roundrobin",
139
-
"nodes": {
140
-
"127.0.0.1:1980": 1
141
-
}
142
-
}
143
-
}'
144
-
```
123
+
You will find messages `serverless pre function` and `match uri /index.html` in the error log.
0 commit comments