未验证 提交 38d93b30 编写于 作者: H huawei 提交者: GitHub

Add ability to collect SQL Parameters in PyMsql plugin (#39)

上级 82e76066
......@@ -58,6 +58,8 @@ Environment Variable | Description | Default
| `SW_AGENT_AUTHENTICATION` | The authentication token to verify that the agent is trusted by the backend OAP, as for how to configure the backend, refer to [the yaml](https://github.com/apache/skywalking/blob/4f0f39ffccdc9b41049903cc540b8904f7c9728e/oap-server/server-bootstrap/src/main/resources/application.yml#L155-L158). | not set |
| `SW_AGENT_LOGGING_LEVEL` | The logging level, could be one of `CRITICAL`, `FATAL`, `ERROR`, `WARN`(`WARNING`), `INFO`, `DEBUG` | `INFO` |
| `SW_AGENT_DISABLE_PLUGINS` | The name patterns in CSV pattern, plugins whose name matches one of the pattern won't be installed | `''` |
| `SW_MYSQL_TRACE_SQL_PARAMETERS` | Indicates whether to collect the sql parameters or not | `False` |
| `SW_MYSQL_SQL_PARAMETERS_MAX_LENGTH` | The maximum length of the collected parameter, parameters longer than the specified length will be truncated | `512` |
## Supported Libraries
......
......@@ -26,6 +26,9 @@ protocol = (os.getenv('SW_AGENT_PROTOCOL') or 'grpc').lower() # type: str
authentication = os.getenv('SW_AGENT_AUTHENTICATION') # type: str
logging_level = os.getenv('SW_AGENT_LOGGING_LEVEL') or 'INFO' # type: str
disable_plugins = (os.getenv('SW_AGENT_DISABLE_PLUGINS') or '').split(',') # type: List[str]
mysql_trace_sql_parameters = True if os.getenv('SW_MYSQL_TRACE_SQL_PARAMETERS') and \
os.getenv('SW_MYSQL_TRACE_SQL_PARAMETERS') == 'True' else False # type: bool
mysql_sql_parameters_max_length = int(os.getenv('SW_MYSQL_SQL_PARAMETERS_MAX_LENGTH') or '512') # type: int
def init(
......
......@@ -15,8 +15,7 @@
# limitations under the License.
#
import logging
from skywalking import Layer, Component
from skywalking import Layer, Component, config
from skywalking.trace import tags
from skywalking.trace.carrier import Carrier
from skywalking.trace.context import get_context
......@@ -47,6 +46,12 @@ def install():
span.tag(Tag(key=tags.DbInstance, val=this.connection.db.decode("utf-8")))
span.tag(Tag(key=tags.DbStatement, val=query))
if config.mysql_trace_sql_parameters and args:
parameter = ",".join([str(arg) for arg in args])
max_len = config.mysql_sql_parameters_max_length
parameter = parameter[0:max_len] + "..." if len(parameter) > max_len else parameter
span.tag(Tag(key=tags.DbSqlParameters, val='[' + parameter + ']'))
except BaseException as e:
span.raised()
raise e
......
......@@ -26,3 +26,4 @@ HttpStatus = 'status.code'
DbType = 'db.type'
DbInstance = 'db.instance'
DbStatement = 'db.statement'
DbSqlParameters = 'db.sql.parameters'
......@@ -30,9 +30,11 @@ segmentItems:
- key: db.type
value: mysql
- key: db.instance
value: test
value: mysql
- key: db.statement
value: select 1
value: 'select * from user where user = %s'
- key: db.sql.parameters
value: '[root]'
startTime: gt 0
endTime: gt 0
componentId: 7003
......
......@@ -22,6 +22,7 @@ from skywalking import agent, config
if __name__ == '__main__':
config.service_name = 'provider'
config.logging_level = 'DEBUG'
config.mysql_trace_sql_parameters = True
agent.start()
from flask import Flask, jsonify
......@@ -32,10 +33,10 @@ if __name__ == '__main__':
@app.route("/users", methods=["POST", "GET"])
def application():
time.sleep(0.5)
connection = pymysql.connect(host='mysql', user='root', password='root', db='test', charset='utf8mb4')
connection = pymysql.connect(host='mysql', user='root', password='root', db='mysql', charset='utf8mb4')
with connection.cursor() as cursor:
sql = "select 1"
cursor.execute(sql)
sql = "select * from user where user = %s"
cursor.execute(sql, ("root",))
connection.close()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册