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

feat: sse-timing plugin for tracing events #1361

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions plugins/wasm-go/extensions/custom-response/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ description: 自定义应答插件配置参考

## 配置字段

| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 |
| -------- | -------- | -------- | -------- | -------- |
| status_code | number | 选填 | 200 | 自定义 HTTP 应答状态码 |
| headers | array of string | 选填 | - | 自定义 HTTP 应答头,key 和 value 用`=`分隔 |
| body | string | 选填 | - | 自定义 HTTP 应答 Body |
| enable_on_status | array of number | 选填 | - | 匹配原始状态码,生成自定义响应,不填写时,不判断原始状态码 |
| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 |
|------------------|-----------------|----|-------|--------------------------------|
| status_code | number | 选填 | 200 | 自定义 HTTP 应答状态码 |
| headers | array of string | 选填 | - | 自定义 HTTP 应答头,key 和 value 用`=`分隔 |
| body | string | 选填 | - | 自定义 HTTP 应答 Body |
| enable_on_status | array of number | 选填 | - | 匹配原始状态码,生成自定义响应,不填写时,不判断原始状态码 |
| is_stream | bool | 选填 | false | 如果是流式响应则不设置Content-Length |

## 配置示例

Expand Down
13 changes: 7 additions & 6 deletions plugins/wasm-go/extensions/custom-response/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ Plugin Execution Phase: `Authentication Phase`
Plugin Execution Priority: `910`

## Configuration Fields
| Name | Data Type | Requirements | Default Value | Description |
| -------- | -------- | -------- | -------- | -------- |
| status_code | number | Optional | 200 | Custom HTTP response status code |
| headers | array of string | Optional | - | Custom HTTP response headers, keys and values separated by `=` |
| body | string | Optional | - | Custom HTTP response body |
| enable_on_status | array of number | Optional | - | Match original status codes to generate custom responses; if not specified, the original status code is not checked |
| Name | Data Type | Requirements | Default Value | Description |
|------------------|-----------------|--------------|---------------|---------------------------------------------------------------------------------------------------------------------|
| status_code | number | Optional | 200 | Custom HTTP response status code |
| headers | array of string | Optional | - | Custom HTTP response headers, keys and values separated by `=` |
| body | string | Optional | - | Custom HTTP response body |
| enable_on_status | array of number | Optional | - | Match original status codes to generate custom responses; if not specified, the original status code is not checked |
| is_stream | bool | Optional | false | If stream response, will not set Content-Length header |
Copy link
Collaborator

Choose a reason for hiding this comment

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

In fact, it is not possible to control whether the content-length header is set, because when the plugin calls sendHTTPResponse, it will callback to the Envoy logic, which automatically adds this header


## Configuration Example
### Mock Response Scenario
Expand Down
24 changes: 21 additions & 3 deletions plugins/wasm-go/extensions/custom-response/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type CustomResponseConfig struct {
body string
enableOnStatus []uint32
contentType string
isStream bool
}

func parseConfig(gjson gjson.Result, config *CustomResponseConfig, log wrapper.Log) error {
Expand Down Expand Up @@ -71,7 +72,11 @@ func parseConfig(gjson gjson.Result, config *CustomResponseConfig, log wrapper.L
config.contentType = "text/plain; charset=utf-8"
}
}
config.headers = append(config.headers, [2]string{"content-type", config.contentType})
config.isStream = gjson.Get("is_stream").Bool()
if !config.isStream {
config.headers = append(config.headers, [2]string{"content-type", config.contentType})
config.headers = append(config.headers, [2]string{"content-length", strconv.Itoa(len(config.body))})
}

config.statusCode = 200
if gjson.Get("status_code").Exists() {
Expand Down Expand Up @@ -100,7 +105,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config CustomResponseConfig,
if len(config.enableOnStatus) != 0 {
return types.ActionContinue
}
err := proxywasm.SendHttpResponseWithDetail(config.statusCode, "custom-response", config.headers, []byte(config.body), -1)
err := SendHttpResponse(config.statusCode, config.headers, []byte(config.body))
if err != nil {
log.Errorf("send http response failed: %v", err)
}
Expand All @@ -124,7 +129,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config CustomResponseConfig,

for _, v := range config.enableOnStatus {
if uint32(statusCode) == v {
err = proxywasm.SendHttpResponseWithDetail(config.statusCode, "custom-response", config.headers, []byte(config.body), -1)
err = SendHttpResponse(config.statusCode, config.headers, []byte(config.body))
if err != nil {
log.Errorf("send http response failed: %v", err)
}
Expand All @@ -133,3 +138,16 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config CustomResponseConfig,

return types.ActionContinue
}

func SendHttpResponse(statusCode uint32, headers [][2]string, body []byte) error {
status := strconv.FormatUint(uint64(statusCode), 10)
if err := proxywasm.AddHttpResponseHeader(":status", status); err != nil {
return err
}
for _, kv := range headers {
if err := proxywasm.AddHttpRequestHeader(kv[0], kv[1]); err != nil {
return err
}
}
return proxywasm.AppendHttpResponseBody(body)
}
13 changes: 7 additions & 6 deletions plugins/wasm-rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ IMAGE_TAG = $(if $(strip $(PLUGIN_VERSION)),${PLUGIN_VERSION},${BUILD_TIME}-${CO
IMG ?= ${REGISTRY}${PLUGIN_NAME}:${IMAGE_TAG}

.DEFAULT:
lint-base:
cargo fmt --all --check
cargo clippy --workspace --all-features --all-targets
lint:
cargo fmt --all --check --manifest-path extensions/${PLUGIN_NAME}/Cargo.toml
cargo clippy --workspace --all-features --all-targets --manifest-path extensions/${PLUGIN_NAME}/Cargo.toml
build:
DOCKER_BUILDKIT=1 docker build \
--build-arg PLUGIN_NAME=${PLUGIN_NAME} \
Expand All @@ -20,3 +14,10 @@ build:
.
@echo ""
@echo "output wasm file: extensions/${PLUGIN_NAME}/plugin.wasm"

lint-base:
cargo fmt --all --check
cargo clippy --workspace --all-features --all-targets
lint:
cargo fmt --all --check --manifest-path extensions/${PLUGIN_NAME}/Cargo.toml
cargo clippy --workspace --all-features --all-targets --manifest-path extensions/${PLUGIN_NAME}/Cargo.toml
26 changes: 26 additions & 0 deletions plugins/wasm-rust/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
## 介绍

此 SDK 用于使用 Rust 语言开发 Higress 的 Wasm 插件。

### 创建新插件

在当前文件夹下(`plugins/wasm-rust`)执行下面的命令,将`${...}`替换为新插件的内容,其中`--name`为必填项。

```shell
sh ./scripts/gen.sh \
--name ${NAME} \
--keywords ${KEYWORDS} \
--description ${DESCRIPTION} \
```

执行完成后将会在`plugins/wasm-rust/extensions/${NAME}`目录下生成以下文件,其中`src/lib.rs`中的内容即为插件需要实现的逻辑

```tree
.
├── Cargo.toml
├── Makefile
├── README.md
├── README_EN.md
├── VERSION
└── src
└── lib.rs
```

执行`make docker-compose`可以在docker中运行higress实例,并通过`curl localhost:${PORT}`来测试插件是否正常运行
27 changes: 0 additions & 27 deletions plugins/wasm-rust/extensions/say-hello/docker-compose.yaml

This file was deleted.

86 changes: 0 additions & 86 deletions plugins/wasm-rust/extensions/say-hello/envoy.yaml

This file was deleted.

Loading
Loading