-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: support Tencent Cloud Log Service #7593
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
bd8c579
add tencent-cloud-cls
ychensha 3b370ef
add tencent-cloud-cls
ychensha 69e952b
add ut
ychensha f57cc30
fix lint
ychensha 136e212
add global tag
ychensha 4e0e8ff
add tencent-cloud-cls
ychensha e969b5e
add custom log format
ychensha b9ee01c
change random
ychensha fd30d49
refa error & proto file load
ychensha 6902513
fix ut
ychensha 0d10153
fix ci
ychensha b5c1d52
fix ut
ychensha c6b6e94
order
ychensha 5d86522
add some comment
ychensha e1066b2
Merge branch 'master' of github.com:ychensha/apisix into feature/cls-…
ychensha f4dd849
refa
ychensha 4ed9d20
fix ut style
ychensha 3c0ef7c
fix ut style
ychensha 3c80122
update ut
ychensha e8ea496
remove secret key console link
ychensha 4dec98c
refa ut
ychensha 93a98fb
refa ut
ychensha 75b9314
refa
ychensha 4309dc1
refa
ychensha 3f5a1f0
Merge branch 'master' of github.com:ychensha/apisix into feature/cls-…
ychensha 4461aaf
remove useless log
ychensha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| -- | ||
| -- Licensed to the Apache Software Foundation (ASF) under one or more | ||
| -- contributor license agreements. See the NOTICE file distributed with | ||
| -- this work for additional information regarding copyright ownership. | ||
| -- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| -- (the "License"); you may not use this file except in compliance with | ||
| -- the License. You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| -- See the License for the specific language governing permissions and | ||
| -- limitations under the License. | ||
| -- | ||
|
|
||
| local core = require("apisix.core") | ||
| local log_util = require("apisix.utils.log-util") | ||
| local bp_manager_mod = require("apisix.utils.batch-processor-manager") | ||
| local cls_sdk = require("apisix.plugins.tencent-cloud-cls.cls-sdk") | ||
| local plugin = require("apisix.plugin") | ||
| local math = math | ||
| local ngx = ngx | ||
| local pairs = pairs | ||
|
|
||
|
|
||
| local plugin_name = "tencent-cloud-cls" | ||
| local batch_processor_manager = bp_manager_mod.new(plugin_name) | ||
| local schema = { | ||
| type = "object", | ||
| properties = { | ||
| cls_host = { type = "string" }, | ||
| cls_topic = { type = "string" }, | ||
| secret_id = { type = "string" }, | ||
| secret_key = { type = "string" }, | ||
| sample_ratio = { | ||
| type = "number", | ||
| minimum = 0.00001, | ||
| maximum = 1, | ||
| default = 1 | ||
| }, | ||
| include_req_body = { type = "boolean", default = false }, | ||
| include_resp_body = { type = "boolean", default = false }, | ||
| global_tag = { type = "object" }, | ||
| }, | ||
| required = { "cls_host", "cls_topic", "secret_id", "secret_key" } | ||
| } | ||
|
|
||
|
|
||
| local metadata_schema = { | ||
| type = "object", | ||
| properties = { | ||
| log_format = log_util.metadata_schema_log_format, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| local _M = { | ||
| version = 0.1, | ||
| priority = 397, | ||
| name = plugin_name, | ||
| schema = batch_processor_manager:wrap_schema(schema), | ||
| metadata_schema = metadata_schema, | ||
| } | ||
|
|
||
|
|
||
| function _M.check_schema(conf, schema_type) | ||
| if schema_type == core.schema.TYPE_METADATA then | ||
| return core.schema.check(metadata_schema, conf) | ||
| end | ||
|
|
||
| local ok, err = core.schema.check(schema, conf) | ||
| if not ok then | ||
| return nil, err | ||
| end | ||
| return log_util.check_log_schema(conf) | ||
| end | ||
|
|
||
|
|
||
| function _M.access(conf, ctx) | ||
|
tzssangglass marked this conversation as resolved.
|
||
| ctx.cls_sample = false | ||
| if conf.sample_ratio == 1 or math.random() < conf.sample_ratio then | ||
| core.log.debug("cls sampled") | ||
| ctx.cls_sample = true | ||
| return | ||
| end | ||
| end | ||
|
|
||
|
|
||
| function _M.body_filter(conf, ctx) | ||
| if ctx.cls_sample then | ||
| log_util.collect_body(conf, ctx) | ||
| end | ||
| end | ||
|
|
||
|
|
||
| function _M.log(conf, ctx) | ||
| -- sample if set | ||
| if not ctx.cls_sample then | ||
| core.log.debug("cls not sampled, skip log") | ||
| return | ||
| end | ||
| local metadata = plugin.plugin_metadata(plugin_name) | ||
| core.log.info("metadata: ", core.json.delay_encode(metadata)) | ||
|
|
||
| local entry | ||
|
|
||
| if metadata and metadata.value.log_format | ||
| and core.table.nkeys(metadata.value.log_format) > 0 | ||
| then | ||
| core.log.debug("using custom format log") | ||
| entry = log_util.get_custom_format_log(ctx, metadata.value.log_format) | ||
| else | ||
| entry = log_util.get_full_log(ngx, conf) | ||
| end | ||
|
|
||
| if conf.global_tag then | ||
| for k, v in pairs(conf.global_tag) do | ||
| entry[k] = v | ||
| end | ||
| end | ||
|
|
||
| if batch_processor_manager:add_entry(conf, entry) then | ||
| return | ||
| end | ||
|
|
||
| local process = function(entries) | ||
| local sdk, err = cls_sdk.new(conf.cls_host, conf.cls_topic, conf.secret_id, conf.secret_key) | ||
| if err then | ||
| core.log.error("init sdk failed err:", err) | ||
| return false, err | ||
| end | ||
| return sdk:send_to_cls(entries) | ||
| end | ||
|
|
||
| batch_processor_manager:add_entry_to_new_processor(conf, entry, ctx, process) | ||
| end | ||
|
|
||
|
|
||
| return _M | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.