-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
sql.go
96 lines (86 loc) · 2.43 KB
/
sql.go
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package translator // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter/internal/translator"
import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
awsxray "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray"
)
func makeSQL(span ptrace.Span, attributes map[string]pcommon.Value) (map[string]pcommon.Value, *awsxray.SQLData) {
var (
filtered = make(map[string]pcommon.Value)
sqlData awsxray.SQLData
dbURL string
dbConnectionString string
dbSystem string
dbInstance string
dbStatement string
dbUser string
)
for key, value := range attributes {
switch key {
case conventions.AttributeDBConnectionString:
dbConnectionString = value.Str()
case conventions.AttributeDBSystem:
dbSystem = value.Str()
case conventions.AttributeDBName:
dbInstance = value.Str()
case conventions.AttributeDBStatement:
dbStatement = value.Str()
case conventions.AttributeDBUser:
dbUser = value.Str()
default:
filtered[key] = value
}
}
if !isSQL(dbSystem) {
// Either no DB attributes or this is not an SQL DB.
return attributes, nil
}
// Despite what the X-Ray documents say, having the DB connection string
// set as the URL value of the segment is not useful. So let's use the
// current span name instead
dbURL = span.Name()
// Let's keep the original format for connection_string
if dbConnectionString == "" {
dbConnectionString = "localhost"
}
dbConnectionString = dbConnectionString + "/" + dbInstance
sqlData = awsxray.SQLData{
URL: awsxray.String(dbURL),
ConnectionString: awsxray.String(dbConnectionString),
DatabaseType: awsxray.String(dbSystem),
User: awsxray.String(dbUser),
SanitizedQuery: awsxray.String(dbStatement),
}
return filtered, &sqlData
}
func isSQL(system string) bool {
switch system {
case "db2":
fallthrough
case "derby":
fallthrough
case "hive":
fallthrough
case "mariadb":
fallthrough
case "mssql":
fallthrough
case "mysql":
fallthrough
case "oracle":
fallthrough
case "postgresql":
fallthrough
case "sqlite":
fallthrough
case "teradata":
fallthrough
case "other_sql":
return true
default:
}
return false
}