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: Support plugin for "aliyun" log service #2177

Merged
merged 27 commits into from
Nov 18, 2020
Merged

feat: Support plugin for "aliyun" log service #2177

merged 27 commits into from
Nov 18, 2020

Conversation

gy09535
Copy link
Contributor

@gy09535 gy09535 commented Sep 7, 2020

What this PR does / why we need it:

Fix:#2169

Pre-submission checklist:

  • Did you explain what problem does this PR solve? Or what new features have been added?
  • Have you added corresponding test cases?
  • Have you modified the corresponding document?
  • Is this PR backward compatible?

@gy09535 gy09535 closed this Sep 7, 2020
@gy09535 gy09535 reopened this Sep 10, 2020
@gy09535 gy09535 changed the title WIP:(feature) support send log to aliyun log service feat: support send log to aliyun log service Sep 10, 2020
…9535/incubator-apisix into feature/support-alicloud-sls

* 'feature/support-alicloud-sls' of https://github.com/gy09535/incubator-apisix:
  doc: fixed typo in `health-check.md` (#2200)
  bugfix: grpc-transcode plugin converts http/json parameters abnormally (#2139)
  chore: add string utility for simplicity and correctness (#2181)
  improve: cache parsed certs and pkeys to LRU cache (#2163)
  bugfix: serverless plugin not work in header_filter phase (#2148)
@gy09535 gy09535 changed the title feat: support send log to aliyun log service feat: support log for aliyun log service Sep 11, 2020
@gy09535 gy09535 changed the title feat: support log for aliyun log service feat: support logger for aliyun log service Sep 11, 2020



=== TEST 4: add plugin
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to verify the response of Alibaba Cloud?
If not, we can mock Alibaba Cloud

Copy link
Contributor Author

@gy09535 gy09535 Sep 14, 2020

Choose a reason for hiding this comment

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

The aliyun log service can not response anything. I can just send it to collector node and then verify it on log service UI, for collector not response anything.I think if the message is send successful with no error log, sending message is success.

Copy link
Member

Choose a reason for hiding this comment

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

we can add a comment in the source code, and mark this plugin is experiment. we'll remove this after the community confirm this plugin is stable

@moonming
Copy link
Member

@membphis @nic-chen please take a look

@moonming
Copy link
Member

@gy09535 Apache APISIX already has the syslog plugin, can these two plugins be combined into one?

@moonming moonming changed the title feat: support logger for aliyun log service feat: support plugin for aliyun log service Sep 14, 2020
@moonming moonming changed the title feat: support plugin for aliyun log service feature: support plugin for aliyun log service Sep 14, 2020
@gy09535
Copy link
Contributor Author

gy09535 commented Sep 14, 2020

@gy09535 Apache APISIX already has the syslog plugin, can these two plugins be combined into one?

For combined into one I think the aliyun has some special config, we should has different plugin to config it and I reference tcp logger plugin to implement it, I think I can refactor tcp logger and sls logger plugin , change the common code into one module, For sys-logger plugin I think some code is duplicate , for example the batch processor has retry the "resty.logger.socket" also has retry etc. so I prefer use cosocket .

@moonming
Copy link
Member

@liuhengloveyou please take a look at this PR

@liuhengloveyou
Copy link
Contributor

@liuhengloveyou please take a look at this PR

working on it.

@gy09535
Copy link
Contributor Author

gy09535 commented Oct 16, 2020

any update?

@membphis
Copy link
Member

any update?

you can rebase your branch first. conflicted

@membphis
Copy link
Member

@gy09535 I think we need to way to confirm this plugin can work fine with aliyun. if we can check this in the test, it should be great.


if not sock then
err_msg = "failed to init the socket" .. soc_err
core.log.error(err_msg)
Copy link
Member

Choose a reason for hiding this comment

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

we should avoid to concat string in Lua land, it'll make the GC busy.

we should use this style:
core.log.error("failed to init the socket", soc_err)

please fix the similar points.

Copy link
Contributor Author

@gy09535 gy09535 Nov 6, 2020

Choose a reason for hiding this comment

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

fix it.

return
end

local rf5424_data = rf5424.encode("SYSLOG", "INFO", ngx.var.host
Copy link
Member

Choose a reason for hiding this comment

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

in APISIX, we shuold use ctx.var.**** to fetch nginx variable.

here is an example: https://github.com/apache/apisix/blob/master/apisix/plugins/limit-count.lua#L166

Copy link
Contributor Author

@gy09535 gy09535 Nov 6, 2020

Choose a reason for hiding this comment

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

fix it.

end

return string_format(rfc5424_format, pri, t, hostname, appname, pid, project,
logstore, access_key_id, access_key_secret, msg)
Copy link
Member

Choose a reason for hiding this comment

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

bad indentation

Copy link
Contributor Author

@gy09535 gy09535 Nov 6, 2020

Choose a reason for hiding this comment

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

fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do not think is my format is right, I reference this doc: https://github.com/apache/apisix/blob/master/CODE_STYLE.md#fromHistory

@gy09535
Copy link
Contributor Author

gy09535 commented Nov 9, 2020

please review

ok, err = sock:setkeepalive(120 * 1000, 20)
if not ok then
can_close = true
core.log.warn("failed to set socket keepalive: host[" .. route_conf.host
Copy link
Member

Choose a reason for hiding this comment

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

bad performance, avoid concat string in Lua land.

core.log.warn("xxxx [", conf.host, "] port[", ...), please use this way. and please update other similar points

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it.

end

data = data .. entry.data
core.log.info(entry.data)
Copy link
Member

Choose a reason for hiding this comment

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

need a simple description?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it.

end

local rf5424_data = rf5424.encode("SYSLOG", "INFO", ctx.var.host,"apisix",
ngx.var.pid, conf.project, conf.logstore,
Copy link
Member

Choose a reason for hiding this comment

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

please use ctx.var.*** instead of ngx.var.***

ctx.var.*** is better performance

Copy link
Member

Choose a reason for hiding this comment

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

please fix other similar poitns

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it.

local string_format = string.format
local rfc5424_timestamp_format = "!%Y-%m-%dT%H:%M:%S.000Z"
local rfc5424_format = "<%d>1 %s %s %s %d - [logservice project=\"%s\" logstore=\"%s\"" ..
" access-key-id=\"%s\" access-key-secret=\"%s\"] %s\n"
Copy link
Member

Choose a reason for hiding this comment

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

bad indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it.

```shell
curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
Copy link
Member

Choose a reason for hiding this comment

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

the indentation should be 4 spaces here. please update it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it.


* check log in ali cloud log service

![](../images/plugin/sls-logger-1.png)
Copy link
Member

Choose a reason for hiding this comment

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

[****] please set a title, when failed to load the image, it will show the title

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it.

```shell
$ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d value='
{
"methods": ["GET"],
Copy link
Member

Choose a reason for hiding this comment

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

remove this useless line

Copy link
Member

Choose a reason for hiding this comment

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

and please update the Chinese doc too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it.

@moonming
Copy link
Member

moonming commented Nov 9, 2020

@membphis please check test cases and feature first, we can put code style issues at the end.

@membphis
Copy link
Member

membphis commented Nov 9, 2020

@membphis please check test cases and feature first, we can put code style issues at the end.

I read the doc and feature before, they are fine. sure about this.

@gy09535
Copy link
Contributor Author

gy09535 commented Nov 10, 2020

style fixed.

@gy09535
Copy link
Contributor Author

gy09535 commented Nov 12, 2020

Today Ido a test , I find using string format can costing more cpu , after I change it to string link, it costing little. Here is two picture is before change and after change .

Test Script

./wrk -c 1000 -t 10 -d 60s --latency   http://192.168.100.96:9080/whoami

Before

image

After

image

@gy09535 gy09535 requested a review from membphis November 13, 2020 11:24
Copy link
Member

@membphis membphis left a comment

Choose a reason for hiding this comment

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

LGTM, except minor doc and style things

local LOG_LOCAL7 = 23 -- reserved for local use

local Facility = {
["KERN"] = LOG_KERN,
Copy link
Member

Choose a reason for hiding this comment

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

code style:

change to KERN = LOG_KERN, which is clearer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

"access_key_id": "your_access_key_id",
"access_key_secret": "your_access_key_secret",
"timeout": 30000
}
Copy link
Member

Choose a reason for hiding this comment

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

bad indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

"access_key_secret": "your_access_key_secret",
"timeout": 30000
}
},
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.




=== TEST 4: add plugin
Copy link
Member

Choose a reason for hiding this comment

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

we can add a comment in the source code, and mark this plugin is experiment. we'll remove this after the community confirm this plugin is stable

@gy09535 gy09535 requested a review from moonming November 16, 2020 11:31
@gy09535
Copy link
Contributor Author

gy09535 commented Nov 18, 2020

Please review CC @moonming

Copy link
Member

@membphis membphis left a comment

Choose a reason for hiding this comment

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

LGTM

@membphis membphis merged commit 5b39602 into apache:master Nov 18, 2020
@moonming moonming added this to the 2.1 milestone Nov 24, 2020
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.

6 participants