forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
329 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/semconv.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package otelmongo // import "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo" | ||
|
||
import ( | ||
"os" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
semconv1170 "go.opentelemetry.io/otel/semconv/v1.17.0" | ||
semconv1260 "go.opentelemetry.io/otel/semconv/v1.26.0" | ||
) | ||
|
||
const ( | ||
semconvOptIn = "OTEL_SEMCONV_STABILITY_OPT_IN" | ||
semconvOptIn1260 = "mongo/v1.26.0" | ||
semconvOptInDup = "mongo/dup" | ||
) | ||
|
||
func appendAttrs[T string | int]( | ||
attrs []attribute.KeyValue, | ||
semconvMap1170 func(T) attribute.KeyValue, | ||
semconvMap1260 func(T) attribute.KeyValue, | ||
val T, | ||
) []attribute.KeyValue { | ||
switch os.Getenv(semconvOptIn) { | ||
case semconvOptIn1260: | ||
if semconvMap1260 != nil { | ||
attrs = append(attrs, semconvMap1260(val)) | ||
} | ||
case semconvOptInDup: | ||
if semconvMap1170 != nil { | ||
attrs = append(attrs, semconvMap1170(val)) | ||
} | ||
|
||
if semconvMap1260 != nil { | ||
attrs = append(attrs, semconvMap1260(val)) | ||
} | ||
default: | ||
if semconvMap1170 != nil { | ||
attrs = append(attrs, semconvMap1170(val)) | ||
} | ||
} | ||
|
||
return attrs | ||
} | ||
|
||
func appendOpNameAttrs(attrs []attribute.KeyValue, op string) []attribute.KeyValue { | ||
return appendAttrs(attrs, semconv1170.DBOperation, semconv1260.DBOperationName, op) | ||
} | ||
|
||
func appendDBNamespace(attrs []attribute.KeyValue, ns string) []attribute.KeyValue { | ||
return appendAttrs(attrs, semconv1170.DBName, semconv1260.DBNamespace, ns) | ||
} | ||
|
||
func appendDBStatement(attrs []attribute.KeyValue, stmt string) []attribute.KeyValue { | ||
return appendAttrs(attrs, semconv1170.DBStatement, semconv1260.DBQueryText, stmt) | ||
} | ||
|
||
func appendNetworkPort(attrs []attribute.KeyValue, p int) []attribute.KeyValue { | ||
return appendAttrs(attrs, semconv1170.NetPeerPort, semconv1260.NetworkPeerPort, p) | ||
} | ||
|
||
func appendNetworkHost(attrs []attribute.KeyValue, h string) []attribute.KeyValue { | ||
return appendAttrs(attrs, semconv1170.NetPeerName, nil, h) | ||
} | ||
|
||
func appendNetworkAddress(attrs []attribute.KeyValue, addr string) []attribute.KeyValue { | ||
return appendAttrs(attrs, nil, semconv1260.NetworkPeerAddress, addr) | ||
} | ||
|
||
func appendNetworkTransport(attrs []attribute.KeyValue) []attribute.KeyValue { | ||
optIn := os.Getenv(semconvOptIn) | ||
useSemconv1260 := optIn == semconvOptIn1260 | ||
useSemconvDup := optIn == semconvOptInDup | ||
|
||
if useSemconv1260 || useSemconvDup { | ||
attrs = append(attrs, semconv1260.NetworkTransportTCP) | ||
} | ||
|
||
if !useSemconv1260 || useSemconvDup { | ||
attrs = append(attrs, semconv1170.NetTransportTCP) | ||
} | ||
|
||
return attrs | ||
} | ||
|
||
func appendCollection(attrs []attribute.KeyValue, coll string) []attribute.KeyValue { | ||
return appendAttrs(attrs, semconv1170.DBMongoDBCollection, semconv1260.DBCollectionName, coll) | ||
} |
111 changes: 111 additions & 0 deletions
111
instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/semconv_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otelmongo | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
) | ||
|
||
func Test_appendOpNameAttrs(t *testing.T) { | ||
const ( | ||
opName = "opName" | ||
dbNamespace = "dbNamespace" | ||
port = 1 | ||
host = "host" | ||
address = "host:1" | ||
stmt = `{insert: "users"}` | ||
coll = "coll" | ||
) | ||
|
||
v1170 := []attribute.KeyValue{ | ||
{Key: "db.operation", Value: attribute.StringValue(opName)}, | ||
{Key: "db.name", Value: attribute.StringValue(dbNamespace)}, | ||
{Key: "db.statement", Value: attribute.StringValue(stmt)}, | ||
{Key: "net.peer.port", Value: attribute.IntValue(port)}, | ||
{Key: "net.peer.name", Value: attribute.StringValue(host)}, | ||
{Key: "net.transport", Value: attribute.StringValue("ip_tcp")}, | ||
{Key: "db.mongodb.collection", Value: attribute.StringValue("coll")}, | ||
} | ||
|
||
v1260 := []attribute.KeyValue{ | ||
{Key: "db.operation.name", Value: attribute.StringValue(opName)}, | ||
{Key: "db.namespace", Value: attribute.StringValue(dbNamespace)}, | ||
{Key: "db.query.text", Value: attribute.StringValue(stmt)}, | ||
{Key: "network.peer.port", Value: attribute.IntValue(port)}, | ||
{Key: "network.peer.address", Value: attribute.StringValue(address)}, | ||
{Key: "network.transport", Value: attribute.StringValue("tcp")}, | ||
{Key: "db.collection.name", Value: attribute.StringValue("coll")}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
initAttrs []attribute.KeyValue | ||
version string | ||
want []attribute.KeyValue | ||
}{ | ||
{ | ||
name: "no version", | ||
initAttrs: []attribute.KeyValue{}, | ||
version: "", | ||
want: v1170, | ||
}, | ||
{ | ||
name: "unsupported version", | ||
initAttrs: []attribute.KeyValue{}, | ||
version: "mongo/foo", | ||
want: v1170, | ||
}, | ||
{ | ||
name: "mongo/v1.26.0", | ||
initAttrs: []attribute.KeyValue{}, | ||
version: "mongo/v1.26.0", | ||
want: v1260, | ||
}, | ||
{ | ||
name: "mongo/dup", | ||
initAttrs: []attribute.KeyValue{}, | ||
version: "mongo/dup", | ||
want: append(v1170, v1260...), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Setenv(semconvOptIn, test.version) | ||
|
||
attrs := appendOpNameAttrs(test.initAttrs, opName) | ||
attrs = appendDBNamespace(attrs, dbNamespace) | ||
attrs = appendDBStatement(attrs, stmt) | ||
attrs = appendNetworkPort(attrs, port) | ||
attrs = appendNetworkHost(attrs, host) | ||
attrs = appendNetworkAddress(attrs, address) | ||
attrs = appendNetworkTransport(attrs) | ||
attrs = appendCollection(attrs, coll) | ||
|
||
assert.ElementsMatch(t, test.want, attrs) | ||
}) | ||
} | ||
} | ||
|
||
func Benchmark_appendAttrs(b *testing.B) { | ||
ini := []attribute.KeyValue{} | ||
|
||
b.ResetTimer() | ||
b.ReportAllocs() | ||
|
||
for i := 0; i < b.N; i++ { | ||
ini = appendOpNameAttrs(ini, "opName") | ||
ini = appendDBNamespace(ini, "dbNamespace") | ||
ini = appendDBStatement(ini, `{insert: "users"}`) | ||
ini = appendNetworkPort(ini, 1) | ||
ini = appendNetworkHost(ini, "host") | ||
ini = appendNetworkAddress(ini, "host:1") | ||
ini = appendNetworkTransport(ini) | ||
ini = appendCollection(ini, "coll") | ||
} | ||
} |
Oops, something went wrong.