Lago
lago 插件在请求完成后将网关使用事件上报至 Lago。Lago 将每个事件与订阅和计费指标关联,以便将 API 流量纳入按量计费。
事件以异步方式发送,不会延迟代理响应。请监控网关错误日志中的被拒批次;当计费事件可能需要重放时,请保留持久的使用数据来源,因为重试次数耗尽后事件会被丢弃。
示例
以下示例将 API 调用上报至基于计数的指标,并附加用于差异化定价的属性。
准备 Lago Cloud 或自行托管的 Lago。运行自行托管的实例前,请查看 Lago 的许可证和部署要求:
- 在 Lago 的开发者设置中创建 API Key。
- 创建代码为
api_requests的计数型计费指标。 - 创建使用该指标计费的套餐。
- 创建客户并为其分配套餐,外部订阅 ID 为
docs-subscription。
export LAGO_API_URL="https://api.getlago.com"
export LAGO_API_KEY="replace-with-lago-api-key"
上报 API 调用
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes/lago-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"uri": "/anything/lago",
"plugins": {
"lago": {
"endpoint_addrs": [
"${LAGO_API_URL}"
],
"token": "${LAGO_API_KEY}",
"event_transaction_id": "request-\${request_id}",
"event_subscription_id": "docs-subscription",
"event_code": "api_requests",
"batch_max_size": 1
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}
EOF
adc.yaml
services:
- name: lago-service
labels:
docs-example: lago
routes:
- name: lago-route
uris:
- /anything/lago
plugins:
lago:
endpoint_addrs:
- "${LAGO_API_URL}"
token: "${LAGO_API_KEY}"
event_transaction_id: request-\${request_id}
event_subscription_id: docs-subscription
event_code: api_requests
batch_max_size: 1
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
反斜杠用于保留网关变量,避免 ADC 将其替换为环境变量。
预览变更:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=lago
检查 diff.yaml,然后同步带有该标签的服务:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=lago
继续操作前,请在 aic 命名空间中设置 Ingress Controller 和网关。通过部署的密钥分发工作流存储 Lago API Key。
- Gateway API
- APISIX CRD
lago-ic.yaml
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: lago-route
spec:
plugins:
- name: lago
config:
endpoint_addrs:
- https://api.getlago.com
token: replace-with-lago-api-key
event_transaction_id: request-${request_id}
event_subscription_id: docs-subscription
event_code: api_requests
batch_max_size: 1
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: lago-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything/lago
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: lago-route
backendRefs:
- name: httpbin-external-domain
port: 80
lago-ic.yaml
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
---
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: lago-route
spec:
ingressClassName: apisix
plugins:
- name: lago
enable: true
config:
endpoint_addrs:
- https://api.getlago.com
token: replace-with-lago-api-key
event_transaction_id: request-${request_id}
event_subscription_id: docs-subscription
event_code: api_requests
batch_max_size: 1
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: lago-route
spec:
ingressClassName: apisix
http:
- name: lago-route
match:
paths:
- /anything/lago
backends:
- serviceName: httpbin-external-domain
servicePort: 80
plugin_config_name: lago-route
应用清单:
kubectl apply -f lago-ic.yaml
发送请求:
curl "http://127.0.0.1:9080/anything/lago"
Lago 处理事件后,查询订阅事件并选取稳定的字段:
curl --get "${LAGO_API_URL}/api/v1/events" \
-H "Authorization: Bearer ${LAGO_API_KEY}" \
--data-urlencode "external_subscription_id=docs-subscription" \
--data-urlencode "code=api_requests" \
| jq '.events[0] | {
code,
external_subscription_id,
transaction_id
}'
最新事件应包含与以下内容类似的字段:
{
"code": "api_requests",
"external_subscription_id": "docs-subscription",
"transaction_id": "request-..."
}
添加差异化定价属性
在 Lago 指标或收费项上配置匹配的 tier 筛选器,然后更新现有插件,为每个事件附加定价层级和响应状态。
- Admin API
- ADC
- Ingress Controller
替换现有路由上的插件配置:
curl "http://127.0.0.1:9180/apisix/admin/routes/lago-route/plugins/lago" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"endpoint_addrs": ["${LAGO_API_URL}"],
"token": "${LAGO_API_KEY}",
"event_transaction_id": "request-\${request_id}",
"event_subscription_id": "docs-subscription",
"event_code": "api_requests",
"event_properties": {
"tier": "premium",
"status": "\${status}"
},
"batch_max_size": 1
}
EOF
在 adc.yaml 中,替换现有路由上的插件配置:
adc.yaml(路由插件)
plugins:
lago:
endpoint_addrs:
- "${LAGO_API_URL}"
token: "${LAGO_API_KEY}"
event_transaction_id: request-\${request_id}
event_subscription_id: docs-subscription
event_code: api_requests
event_properties:
tier: premium
status: \${status}
batch_max_size: 1
反斜杠用于保留网关变量,以便在请求时解析。
预览更新后的服务:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=lago
检查 diff.yaml,然后同步带有该标签的服务:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=lago
使用部署所用的 Kubernetes API 更新现有插件配置:
- Gateway API
- APISIX CRD
lago-ic.yaml
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: lago-route
spec:
plugins:
- name: lago
config:
endpoint_addrs:
- https://api.getlago.com
token: replace-with-lago-api-key
event_transaction_id: request-${request_id}
event_subscription_id: docs-subscription
event_code: api_requests
event_properties:
tier: premium
status: "${status}"
batch_max_size: 1
lago-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: lago-route
spec:
ingressClassName: apisix
plugins:
- name: lago
enable: true
config:
endpoint_addrs:
- https://api.getlago.com
token: replace-with-lago-api-key
event_transaction_id: request-${request_id}
event_subscription_id: docs-subscription
event_code: api_requests
event_properties:
tier: premium
status: "${status}"
batch_max_size: 1
应用更新后的插件配置:
kubectl apply -f lago-ic.yaml
再次发送请求:
curl "http://127.0.0.1:9080/anything/lago"
Lago 处理事件后,查询订阅并选取包含定价层级的最新事件:
curl --get "${LAGO_API_URL}/api/v1/events" \
-H "Authorization: Bearer ${LAGO_API_KEY}" \
--data-urlencode "external_subscription_id=docs-subscription" \
--data-urlencode "code=api_requests" \
| jq '[.events[] | select(.properties.tier == "premium")][0] | {
code,
properties
}'
该事件应包含定价属性:
{
"code": "api_requests",
"properties": {
"tier": "premium",
"status": "200"
}
}