-
Notifications
You must be signed in to change notification settings - Fork 851
Add support for custom logging field #12872
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
Open
maskit
wants to merge
25
commits into
apache:master
Choose a base branch
from
maskit:log_custom_field
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+640
−13
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
c3faea3
Add support for custom logging field
maskit 9796a03
Don't call the hook on traffic_logstats
maskit 319d90b
Use TSstrlcpy instead of strlcpy
maskit 4cf0fb6
Initialize the variables with nullptr
maskit aa2bd48
Fix the plugin name on the source code
maskit 40e4445
Add support for overriding built-in fields
maskit 544d31b
Adjust the timing to invoke the lifecycle hook
maskit 09867fe
Fix a build error
maskit f73be02
Add support for integer field
maskit b4105ed
Include charconv header
maskit 87dd785
Add support for socket address field
maskit 0b8fdb0
Add more marshaling functions
maskit 3367913
Use string_view
maskit e25597e
Revert unnecessary changes
maskit ea6d8ee
Address warnings
maskit 3b1e57f
Address build errors
maskit 4ed806f
Add API documentation
maskit b1d67e4
Add documentation about LOG_INITIALIZED hook and event
maskit 3c5fd7f
Fix typo
maskit d82a3e0
Fix typo INITIAZLIED
maskit 5cc7352
Remove an invalid assertion
maskit 8ad9a9f
Fix null termination
maskit 46b78e8
Update sample plugin description
maskit 4765b63
Hide log type DINT
maskit 2243064
Add more comments
maskit 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
99 changes: 99 additions & 0 deletions
99
doc/developer-guide/api/functions/TSLogFieldRegister.en.rst
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,99 @@ | ||||||||||||||||||||||
| .. 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. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. include:: ../../../common.defs | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. default-domain:: cpp | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| TSLogFieldRegister | ||||||||||||||||||||||
| ****************** | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Registers a custom log field, or modifies an existing log field with a new definition. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Synopsis | ||||||||||||||||||||||
| ======== | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. code-block:: cpp | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #include <ts/ts.h> | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. function:: TSReturnCode TSLogFieldRegister(std::string_view name, std::string_view symbol, TSLogType type, TSLogMarshalCallback marshal_cb, TSLogUnmarshalCallback unmarshal_cb, bool replace = false); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. enum:: TSLogType | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Specify the type of a log field | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. enumerator:: TS_LOG_TYPE_INT | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Integer field. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. enumerator:: TS_LOG_TYPE_STRING | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| String field. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. enumerator:: TS_LOG_TYPE_ADDR | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Address field. It supports IPv4 address, IPv6 address, and Unix Domain Socket address (path). | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. type:: int (*TSLogMarshalCallback)(TSHttpTxn, char *); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Callback sginature for functions to marshal log fields. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| .. type:: std::tuple<int, int> (*TSLogUnmarshalCallback)(char **, char *, int); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Callback sginature for functions to unmarshal log fields. | ||||||||||||||||||||||
|
Comment on lines
+53
to
+57
|
||||||||||||||||||||||
| Callback sginature for functions to marshal log fields. | |
| .. type:: std::tuple<int, int> (*TSLogUnmarshalCallback)(char **, char *, int); | |
| Callback sginature for functions to unmarshal log fields. | |
| Callback signature for functions to marshal log fields. | |
| .. type:: std::tuple<int, int> (*TSLogUnmarshalCallback)(char **, char *, int); | |
| Callback signature for functions to unmarshal log fields. |
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
230 changes: 230 additions & 0 deletions
230
example/plugins/c-api/custom_logfield/custom_logfield.cc
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,230 @@ | ||
| /** @file | ||
|
|
||
| This plugin demonstrates custom log field registration and usage. | ||
| It populates custom log fields from per-transaction user arguments. | ||
|
|
||
| @section license License | ||
|
|
||
| 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. | ||
| */ | ||
|
|
||
| #include <inttypes.h> | ||
| #include <sys/socket.h> | ||
| #include <netinet/in.h> | ||
| #include <arpa/inet.h> | ||
|
|
||
| #include <ts/ts.h> | ||
| #include <ts/remap.h> | ||
|
|
||
| DbgCtl dbg_ctl{"custom_logfield"}; | ||
|
|
||
| char PLUGIN_NAME[] = "custom_logfield"; | ||
| char VENDOR_NAME[] = "Apache Software Foundation"; | ||
| char SUPPORT_EMAIL[] = "dev@trafficserver.apache.org"; | ||
| char USER_ARG_CSTM[] = "cstm_field"; | ||
| char USER_ARG_CSTMI[] = "cstmi_field"; | ||
| char USER_ARG_CSSN[] = "cssn_field"; | ||
|
|
||
| int | ||
| write_text_from_user_arg(TSHttpTxn txnp, char *buf, const char *user_arg_name) | ||
| { | ||
| int len = 0; | ||
| int index; | ||
|
|
||
| if (TSUserArgIndexNameLookup(TS_USER_ARGS_TXN, user_arg_name, &index, nullptr) == TS_SUCCESS) { | ||
| Dbg(dbg_ctl, "User Arg Index: %d", index); | ||
| if (char *value = static_cast<char *>(TSUserArgGet(txnp, index)); value) { | ||
| Dbg(dbg_ctl, "Value: %s", value); | ||
| len = strlen(value); | ||
| if (buf) { | ||
| TSstrlcpy(buf, value, len + 1); | ||
| } | ||
| } | ||
| } | ||
| return len + 1; | ||
| } | ||
|
|
||
| int | ||
| marshal_function_cstm(TSHttpTxn txnp, char *buf) | ||
| { | ||
| if (buf) { | ||
| Dbg(dbg_ctl, "Marshaling a custom field cstm"); | ||
| } else { | ||
| Dbg(dbg_ctl, "Marshaling a custom field cstm for size calculation"); | ||
| } | ||
| return write_text_from_user_arg(txnp, buf, USER_ARG_CSTM); | ||
| } | ||
|
|
||
| int | ||
| marshal_function_cssn(TSHttpTxn txnp, char *buf) | ||
| { | ||
| if (buf) { | ||
| Dbg(dbg_ctl, "Marshaling a built-in field cssn"); | ||
| } else { | ||
| Dbg(dbg_ctl, "Marshaling a built-in field cssn for size calculation"); | ||
| } | ||
| return write_text_from_user_arg(txnp, buf, USER_ARG_CSSN); | ||
| } | ||
|
|
||
| int | ||
| marshal_function_cstmi(TSHttpTxn txnp, char *buf) | ||
| { | ||
| // This implementation is just to demonstrate marshaling an integer value. | ||
| // Predefined marshal function, TSLogIntMarshal, works for simple integer values | ||
|
|
||
| int index; | ||
|
|
||
| if (buf) { | ||
| Dbg(dbg_ctl, "Marshaling a custom field cstmi"); | ||
| } else { | ||
| Dbg(dbg_ctl, "Marshaling a custom field cstmi for size calculation"); | ||
| } | ||
|
|
||
| if (buf) { | ||
| if (TSUserArgIndexNameLookup(TS_USER_ARGS_TXN, USER_ARG_CSTMI, &index, nullptr) == TS_SUCCESS) { | ||
| Dbg(dbg_ctl, "User Arg Index: %d", index); | ||
| if (int64_t value = reinterpret_cast<int64_t>(TSUserArgGet(txnp, index)); value) { | ||
| Dbg(dbg_ctl, "Value: %" PRId64, value); | ||
| *(reinterpret_cast<int64_t *>(buf)) = value; | ||
| } | ||
| } | ||
| } | ||
| return sizeof(int64_t); | ||
| } | ||
|
|
||
| std::tuple<int, int> | ||
| unmarshal_function_string(char **buf, char *dest, int len) | ||
| { | ||
| Dbg(dbg_ctl, "Unmarshaling a string field"); | ||
|
|
||
| // This implementation is just to demonstrate unmarshaling a string value. | ||
| // Predefined unmarshal function, TSLogStringUnmarshal, works for simple string values | ||
|
|
||
| int l = strlen(*buf); | ||
| Dbg(dbg_ctl, "Dest buf size: %d", len); | ||
| Dbg(dbg_ctl, "Unmarshaled value length: %d", l); | ||
| if (l < len) { | ||
| memcpy(dest, *buf, l); | ||
| Dbg(dbg_ctl, "Unmarshaled value: %.*s", l, dest); | ||
| return { | ||
| l, // The length of data read from buf | ||
| l // The length of data written to dest | ||
| }; | ||
| } else { | ||
| return {-1, -1}; | ||
| } | ||
| } | ||
|
|
||
| int | ||
| lifecycle_event_handler(TSCont /* contp ATS_UNUSED */, TSEvent event, void * /* edata ATS_UNUSED */) | ||
| { | ||
| TSAssert(event == TS_EVENT_LIFECYCLE_LOG_INITIALIZED); | ||
|
|
||
| // This registers a custom log field "cstm". | ||
| Dbg(dbg_ctl, "Registering cstm log field"); | ||
| TSLogFieldRegister("custom log field", "cstm", TS_LOG_TYPE_STRING, marshal_function_cstm, unmarshal_function_string); | ||
|
|
||
| // This replaces marshaling and unmarshaling functions for a built-in log field "cssn". | ||
| Dbg(dbg_ctl, "Overriding cssn log field"); | ||
| TSLogFieldRegister("modified cssn", "cssn", TS_LOG_TYPE_STRING, marshal_function_cssn, TSLogStringUnmarshal, true); | ||
|
|
||
| // This registers a custom log field "cstmi" | ||
| Dbg(dbg_ctl, "Registering cstmi log field"); | ||
| TSLogFieldRegister("custom integer log field", "cstmi", TS_LOG_TYPE_INT, marshal_function_cstmi, TSLogIntUnmarshal); | ||
|
|
||
| // This replaces marshaling and unmarshaling functions for a built-in log field "chi". | ||
| Dbg(dbg_ctl, "Overriding chi log field"); | ||
| TSLogFieldRegister( | ||
| "modified cssn", "chi", TS_LOG_TYPE_ADDR, | ||
| [](TSHttpTxn /* txnp */, char *buf) -> int { | ||
| sockaddr_in addr; | ||
| addr.sin_family = AF_INET; | ||
| addr.sin_port = htons(80); | ||
| addr.sin_addr.s_addr = inet_addr("192.168.0.1"); | ||
| return TSLogAddrMarshal(buf, reinterpret_cast<sockaddr *>(&addr)); | ||
| }, | ||
| TSLogAddrUnmarshal, true); | ||
|
|
||
| return TS_SUCCESS; | ||
| } | ||
|
|
||
| void | ||
| TSPluginInit(int /* argc ATS_UNUSED */, const char ** /* argv ATS_UNUSED */) | ||
| { | ||
| Dbg(dbg_ctl, "Initializing plugin"); | ||
|
|
||
| TSPluginRegistrationInfo info = {PLUGIN_NAME, VENDOR_NAME, SUPPORT_EMAIL}; | ||
| if (TSPluginRegister(&info) != TS_SUCCESS) { | ||
| TSError("[%s](%s) Plugin registration failed. \n", PLUGIN_NAME, __FUNCTION__); | ||
| } | ||
|
|
||
| TSCont cont = TSContCreate(lifecycle_event_handler, nullptr); | ||
| TSLifecycleHookAdd(TS_LIFECYCLE_LOG_INITIALIZED_HOOK, cont); | ||
|
|
||
| int argIndex; | ||
| TSUserArgIndexReserve(TS_USER_ARGS_TXN, USER_ARG_CSTM, "This is for cstm log field", &argIndex); | ||
| Dbg(dbg_ctl, "User Arg Index: %d", argIndex); | ||
| TSUserArgIndexReserve(TS_USER_ARGS_TXN, USER_ARG_CSSN, "This is for cssn log field", &argIndex); | ||
| Dbg(dbg_ctl, "User Arg Index: %d", argIndex); | ||
| TSUserArgIndexReserve(TS_USER_ARGS_TXN, USER_ARG_CSTMI, "This is for cstmi log field", &argIndex); | ||
| Dbg(dbg_ctl, "User Arg Index: %d", argIndex); | ||
| } | ||
|
|
||
| TSReturnCode | ||
| TSRemapInit(TSRemapInterface *, char *, int) | ||
| { | ||
| return TS_SUCCESS; | ||
| } | ||
|
|
||
| TSReturnCode | ||
| TSRemapNewInstance(int, char **, void **, char *, int) | ||
| { | ||
| return TS_SUCCESS; | ||
| } | ||
|
|
||
| void | ||
| TSRemapDeleteInstance(void *) | ||
| { | ||
| } | ||
|
|
||
| TSRemapStatus | ||
| TSRemapDoRemap(void *, TSHttpTxn txn, TSRemapRequestInfo *) | ||
| { | ||
| Dbg(dbg_ctl, "Remapping"); | ||
|
|
||
| int index; | ||
|
|
||
| // Store a string value for cstm field | ||
| if (TSUserArgIndexNameLookup(TS_USER_ARGS_TXN, USER_ARG_CSTM, &index, nullptr) == TS_SUCCESS) { | ||
| Dbg(dbg_ctl, "User Arg Index: %d", index); | ||
| TSUserArgSet(txn, index, const_cast<char *>("abc")); | ||
| } | ||
|
|
||
| // Store a string value for cssn field | ||
| if (TSUserArgIndexNameLookup(TS_USER_ARGS_TXN, USER_ARG_CSSN, &index, nullptr) == TS_SUCCESS) { | ||
| Dbg(dbg_ctl, "User Arg Index: %d", index); | ||
| TSUserArgSet(txn, index, const_cast<char *>("xyz")); | ||
| } | ||
|
|
||
| // Store an integer value for cstmi field | ||
| if (TSUserArgIndexNameLookup(TS_USER_ARGS_TXN, USER_ARG_CSTMI, &index, nullptr) == TS_SUCCESS) { | ||
| Dbg(dbg_ctl, "User Arg Index: %d", index); | ||
| TSUserArgSet(txn, index, reinterpret_cast<void *>(43)); | ||
| } | ||
|
|
||
| return TSREMAP_NO_REMAP; | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spelling error: "sginature" should be "signature".