@@ -11,8 +11,11 @@ import (
1111 "go.opentelemetry.io/otel"
1212 "go.opentelemetry.io/otel/attribute"
1313 "go.opentelemetry.io/otel/codes"
14- semconv "go.opentelemetry.io/otel/semconv/v1.10 .0"
14+ semconv "go.opentelemetry.io/otel/semconv/v1.30 .0"
1515 "go.opentelemetry.io/otel/trace"
16+ "gorm.io/driver/clickhouse"
17+ "gorm.io/driver/mysql"
18+ "gorm.io/driver/postgres"
1619 "gorm.io/gorm"
1720
1821 "gorm.io/plugin/opentelemetry/metrics"
@@ -106,7 +109,30 @@ func (p otelPlugin) Initialize(db *gorm.DB) (err error) {
106109
107110func (p * otelPlugin ) before (spanName string ) gormHookFunc {
108111 return func (tx * gorm.DB ) {
109- tx .Statement .Context , _ = p .tracer .Start (tx .Statement .Context , spanName , trace .WithSpanKind (trace .SpanKindClient ))
112+ ctx , span := p .tracer .Start (tx .Statement .Context , spanName , trace .WithSpanKind (trace .SpanKindClient ))
113+ tx .Statement .Context = ctx
114+
115+ // `server.address` is required in the latest semconv
116+ var serverAddrAttr attribute.KeyValue
117+ switch dialector := tx .Config .Dialector .(type ) {
118+ case * mysql.Dialector :
119+ if dialector .Config .DSNConfig != nil && dialector .Config .DSNConfig .Addr != "" {
120+ serverAddrAttr = semconv .ServerAddressKey .String (dialector .Config .DSNConfig .Addr )
121+ span .SetAttributes (serverAddrAttr )
122+ }
123+ case * clickhouse.Dialector :
124+ if dialector .Config .DSN != "" {
125+ serverAddrAttr = semconv .ServerAddressKey .String (dialector .Config .DSN )
126+ span .SetAttributes (serverAddrAttr )
127+ }
128+ case * postgres.Dialector :
129+ if dialector .Config .DSN != "" {
130+ serverAddrAttr = semconv .ServerAddressKey .String (dialector .Config .DSN )
131+ span .SetAttributes (serverAddrAttr )
132+ }
133+ default :
134+
135+ }
110136 }
111137}
112138
@@ -135,10 +161,19 @@ func (p *otelPlugin) after() gormHookFunc {
135161 }
136162
137163 formatQuery := p .formatQuery (query )
138- attrs = append (attrs , semconv .DBStatementKey .String (formatQuery ))
139- attrs = append (attrs , semconv .DBOperationKey .String (dbOperation (formatQuery )))
164+ attrs = append (attrs , semconv .DBQueryText (formatQuery ))
165+ operation := dbOperation (formatQuery )
166+ attrs = append (attrs , semconv .DBOperationName (operation ))
140167 if tx .Statement .Table != "" {
141- attrs = append (attrs , semconv .DBSQLTableKey .String (tx .Statement .Table ))
168+ attrs = append (attrs , semconv .DBCollectionName (tx .Statement .Table ))
169+ // add attr `db.query.summary`
170+ dbQuerySummary := operation + " " + tx .Statement .Table
171+ attrs = append (attrs , semconv .DBQuerySummary (dbQuerySummary ))
172+
173+ // according to semconv, we should update the span name here if `db.query.summary`is available
174+ // Use `db.query.summary` as span name directly here instead of keeping the original span name like `gorm.Query`,
175+ // as we cannot access the original span name here.
176+ span .SetName (dbQuerySummary )
142177 }
143178 if tx .Statement .RowsAffected != - 1 {
144179 attrs = append (attrs , dbRowsAffected .Int64 (tx .Statement .RowsAffected ))
@@ -169,19 +204,19 @@ func (p *otelPlugin) formatQuery(query string) string {
169204func dbSystem (tx * gorm.DB ) attribute.KeyValue {
170205 switch tx .Dialector .Name () {
171206 case "mysql" :
172- return semconv .DBSystemMySQL
207+ return semconv .DBSystemNameMySQL
173208 case "mssql" :
174- return semconv .DBSystemMSSQL
209+ return semconv .DBSystemNameMicrosoftSQLServer
175210 case "postgres" , "postgresql" :
176- return semconv .DBSystemPostgreSQL
211+ return semconv .DBSystemNamePostgreSQL
177212 case "sqlite" :
178- return semconv .DBSystemSqlite
213+ return semconv .DBSystemNameSqlite
179214 case "sqlserver" :
180- return semconv .DBSystemKey . String ( "sqlserver" )
215+ return semconv .DBSystemNameMicrosoftSQLServer
181216 case "clickhouse" :
182- return semconv .DBSystemKey . String ( "clickhouse" )
217+ return semconv .DBSystemNameClickhouse
183218 case "spanner" :
184- return semconv .DBSystemKey . String ( "spanner" )
219+ return semconv .DBSystemNameGCPSpanner
185220 default :
186221 return attribute.KeyValue {}
187222 }
0 commit comments