Skip to content

Commit df1a672

Browse files
feat: send operation name (#27)
Co-authored-by: Fabian Eduardo Diaz Lizcano <fabian.diazlizcano@mercadolibre.com.co>
1 parent cecb698 commit df1a672

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

tracing/tracing.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"database/sql/driver"
66
"fmt"
77
"io"
8+
"regexp"
9+
"strings"
810

911
"go.opentelemetry.io/otel"
1012
"go.opentelemetry.io/otel/attribute"
@@ -16,7 +18,14 @@ import (
1618
"gorm.io/plugin/opentelemetry/metrics"
1719
)
1820

19-
var dbRowsAffected = attribute.Key("db.rows_affected")
21+
var (
22+
firstWordRegex = regexp.MustCompile(`^\w+`)
23+
cCommentRegex = regexp.MustCompile(`(?is)/\*.*?\*/`)
24+
lineCommentRegex = regexp.MustCompile(`(?im)(?:--|#).*?$`)
25+
sqlPrefixRegex = regexp.MustCompile(`^[\s;]*`)
26+
27+
dbRowsAffected = attribute.Key("db.rows_affected")
28+
)
2029

2130
type otelPlugin struct {
2231
provider trace.TracerProvider
@@ -124,7 +133,9 @@ func (p *otelPlugin) after() gormHookFunc {
124133
query = tx.Dialector.Explain(tx.Statement.SQL.String(), vars...)
125134
}
126135

127-
attrs = append(attrs, semconv.DBStatementKey.String(p.formatQuery(query)))
136+
formatQuery := p.formatQuery(query)
137+
attrs = append(attrs, semconv.DBStatementKey.String(formatQuery))
138+
attrs = append(attrs, semconv.DBOperationKey.String(dbOperation(formatQuery)))
128139
if tx.Statement.Table != "" {
129140
attrs = append(attrs, semconv.DBSQLTableKey.String(tx.Statement.Table))
130141
}
@@ -172,3 +183,10 @@ func dbSystem(tx *gorm.DB) attribute.KeyValue {
172183
return attribute.KeyValue{}
173184
}
174185
}
186+
187+
func dbOperation(query string) string {
188+
s := cCommentRegex.ReplaceAllString(query, "")
189+
s = lineCommentRegex.ReplaceAllString(s, "")
190+
s = sqlPrefixRegex.ReplaceAllString(s, "")
191+
return strings.ToLower(firstWordRegex.FindString(s))
192+
}

tracing/tracing_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func TestOtel(t *testing.T) {
4444
stmt, ok := m[semconv.DBStatementKey]
4545
require.True(t, ok)
4646
require.Equal(t, "SELECT 42", stmt.AsString())
47+
48+
operation, ok := m[semconv.DBOperationKey]
49+
require.True(t, ok)
50+
require.Equal(t, "select", operation.AsString())
4751
},
4852
},
4953
{
@@ -69,6 +73,10 @@ func TestOtel(t *testing.T) {
6973
stmt, ok := m[semconv.DBStatementKey]
7074
require.True(t, ok)
7175
require.Equal(t, "SELECT foo_bar", stmt.AsString())
76+
77+
operation, ok := m[semconv.DBOperationKey]
78+
require.True(t, ok)
79+
require.Equal(t, "select", operation.AsString())
7280
},
7381
},
7482
{
@@ -98,6 +106,10 @@ func TestOtel(t *testing.T) {
98106
stmt, ok := m[semconv.DBStatementKey]
99107
require.True(t, ok)
100108
require.Equal(t, "SELECT id FROM `foo` WHERE id = ?", stmt.AsString())
109+
110+
operation, ok := m[semconv.DBOperationKey]
111+
require.True(t, ok)
112+
require.Equal(t, "select", operation.AsString())
101113
},
102114
},
103115
}

0 commit comments

Comments
 (0)