-
Notifications
You must be signed in to change notification settings - Fork 542
/
Copy pathutils.ts
124 lines (113 loc) · 3.43 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright The OpenTelemetry Authors
*
* Licensed 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
*
* https://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.
*/
import { SpanAttributes } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import type {
ConnectionConfig,
PoolActualConfig,
Query,
QueryOptions,
} from 'mysql';
import * as mysqlTypes from 'mysql';
/**
* Get an SpanAttributes map from a mysql connection config object
*
* @param config ConnectionConfig
*/
export function getConnectionAttributes(
config: ConnectionConfig | PoolActualConfig
): SpanAttributes {
const { host, port, database, user } = getConfig(config);
return {
[SemanticAttributes.NET_PEER_NAME]: host,
[SemanticAttributes.NET_PEER_PORT]: port,
[SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString(
host,
port,
database
),
[SemanticAttributes.DB_NAME]: database,
[SemanticAttributes.DB_USER]: user,
};
}
function getConfig(config: any) {
const { host, port, database, user } =
(config && config.connectionConfig) || config || {};
return { host, port, database, user };
}
function getJDBCString(
host: string | undefined,
port: number | undefined,
database: string | undefined
) {
let jdbcString = `jdbc:mysql://${host || 'localhost'}`;
if (typeof port === 'number') {
jdbcString += `:${port}`;
}
if (typeof database === 'string') {
jdbcString += `/${database}`;
}
return jdbcString;
}
/**
* Conjures up the value for the db.statement attribute by formatting a SQL query.
*
* @returns the database statement being executed.
*/
export function getDbStatement(
query: string | Query | QueryOptions,
format: (
sql: string,
values: any[],
stringifyObjects?: boolean,
timeZone?: string
) => string,
values?: any[]
): string {
if (typeof query === 'string') {
return values ? format(query, values) : query;
} else {
// According to https://github.com/mysqljs/mysql#performing-queries
// The values argument will override the values in the option object.
return values || query.values
? format(query.sql, values || query.values)
: query.sql;
}
}
/**
* The span name SHOULD be set to a low cardinality value
* representing the statement executed on the database.
*
* @returns SQL statement without variable arguments or SQL verb
*/
export function getSpanName(query: string | Query | QueryOptions): string {
if (typeof query === 'object') {
return query.sql;
}
return query.split(' ')[0];
}
export function getPoolName(pool: mysqlTypes.Pool): string {
const c = pool.config.connectionConfig;
let poolName = '';
poolName += c.host ? `host: '${c.host}', ` : '';
poolName += c.port ? `port: ${c.port}, ` : '';
poolName += c.database ? `database: '${c.database}', ` : '';
poolName += c.user ? `user: '${c.user}'` : '';
if (!c.user) {
poolName = poolName.substring(0, poolName.length - 2); //omit last comma
}
return poolName.trim();
}