Skip to content

Commit

Permalink
[contrib/gocql] reporting both cluster and datacenter (#2577)
Browse files Browse the repository at this point in the history
Co-authored-by: Rodrigo Arguello <rodrigo.arguello@datadoghq.com>
Co-authored-by: Dario Castañé <dario.castane@datadoghq.com>
  • Loading branch information
3 people committed Jul 9, 2024
1 parent 79fd68c commit a20143b
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
DD_POOL_TRACE_CHECK_FAILURES: true
DD_DISABLE_ERROR_RESPONSES: true
cassandra:
image: cassandra:3.7
image: cassandra:3.11
env:
JVM_OPTS: "-Xms750m -Xmx750m"
ports:
Expand Down
10 changes: 9 additions & 1 deletion contrib/gocql/gocql/gocql.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,15 @@ func (tq *Query) Iter() *Iter {
if tIter.Host() != nil {
tIter.span.SetTag(ext.TargetHost, tIter.Iter.Host().HostID())
tIter.span.SetTag(ext.TargetPort, strconv.Itoa(tIter.Iter.Host().Port()))
tIter.span.SetTag(ext.CassandraCluster, tIter.Iter.Host().DataCenter())

cluster := tIter.Iter.Host().ClusterName()
dc := tIter.Iter.Host().DataCenter()
if tq.config.clusterTagLegacyMode {
tIter.span.SetTag(ext.CassandraCluster, dc)
} else {
tIter.span.SetTag(ext.CassandraCluster, cluster)
}
tIter.span.SetTag(ext.CassandraDatacenter, dc)
}
return tIter
}
Expand Down
69 changes: 67 additions & 2 deletions contrib/gocql/gocql/gocql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func TestErrorWrapper(t *testing.T) {
if iter.Host() != nil {
assert.Equal(span.Tag(ext.TargetPort), "9042")
assert.Equal(span.Tag(ext.TargetHost), iter.Host().HostID())
assert.Equal(span.Tag(ext.CassandraCluster), "datacenter1")
assert.Equal(span.Tag(ext.CassandraCluster), "Test Cluster")
assert.Equal(span.Tag(ext.CassandraDatacenter), "datacenter1")
}
}

Expand Down Expand Up @@ -152,7 +153,71 @@ func TestChildWrapperSpan(t *testing.T) {
if iter.Host() != nil {
assert.Equal(childSpan.Tag(ext.TargetPort), "9042")
assert.Equal(childSpan.Tag(ext.TargetHost), iter.Host().HostID())
assert.Equal(childSpan.Tag(ext.CassandraCluster), "datacenter1")
assert.Equal(childSpan.Tag(ext.CassandraCluster), "Test Cluster")
assert.Equal(childSpan.Tag(ext.CassandraDatacenter), "datacenter1")
}
}

func TestCompatMode(t *testing.T) {
genSpans := func(t *testing.T) []mocktracer.Span {
mt := mocktracer.Start()
defer mt.Stop()

cluster := newCassandraCluster()
session, err := cluster.CreateSession()
require.NoError(t, err)

q := session.Query("SELECT * FROM trace.person").WithContext(context.Background())
tq := WrapQuery(q, WithServiceName("TestServiceName"))
iter := tq.Iter()
err = iter.Close()
require.NoError(t, err)

spans := mt.FinishedSpans()
require.Len(t, spans, 1)
return spans
}
testCases := []struct {
name string
gocqlCompat string
wantCluster string
}{
{
name: "== v1.65",
gocqlCompat: "v1.65",
wantCluster: "datacenter1",
},
{
name: "< v1.65",
gocqlCompat: "v1.64",
wantCluster: "datacenter1",
},
{
name: "> v1.65",
gocqlCompat: "v1.66",
wantCluster: "Test Cluster",
},
{
name: "empty",
gocqlCompat: "",
wantCluster: "Test Cluster",
},
{
name: "bad version",
gocqlCompat: "bad-version",
wantCluster: "Test Cluster",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("DD_TRACE_GOCQL_COMPAT", tc.gocqlCompat)
spans := genSpans(t)
s := spans[0]
assert.Equal(t, s.Tag(ext.TargetPort), "9042")
assert.NotEmpty(t, s.Tag(ext.TargetHost))
assert.Equal(t, tc.wantCluster, s.Tag(ext.CassandraCluster))
assert.Equal(t, "datacenter1", s.Tag(ext.CassandraDatacenter))
})
}
}

Expand Down
12 changes: 12 additions & 0 deletions contrib/gocql/gocql/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ package gocql

import (
"math"
"os"

"golang.org/x/mod/semver"

"gopkg.in/DataDog/dd-trace-go.v1/internal"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"gopkg.in/DataDog/dd-trace-go.v1/internal/namingschema"
)

Expand All @@ -21,6 +25,7 @@ type queryConfig struct {
analyticsRate float64
errCheck func(err error) bool
customTags map[string]interface{}
clusterTagLegacyMode bool
}

// WrapOption represents an option that can be passed to WrapQuery.
Expand All @@ -37,6 +42,13 @@ func defaultConfig() *queryConfig {
} else {
cfg.analyticsRate = math.NaN()
}
if compatMode := os.Getenv("DD_TRACE_GOCQL_COMPAT"); compatMode != "" {
if semver.IsValid(compatMode) {
cfg.clusterTagLegacyMode = semver.Compare(semver.MajorMinor(compatMode), "v1.65") <= 0
} else {
log.Warn("ignoring DD_TRACE_GOCQL_COMPAT: invalid version %q", compatMode)
}
}
cfg.errCheck = func(error) bool { return true }
return cfg
}
Expand Down
3 changes: 3 additions & 0 deletions ddtrace/ext/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ const (
// CassandraCluster specifies the tag name that is used to set the cluster.
CassandraCluster = "cassandra.cluster"

// CassandraDatacenter specifies the tag name that is used to set the datacenter.
CassandraDatacenter = "cassandra.datacenter"

// CassandraRowCount specifies the tag name to use when settings the row count.
CassandraRowCount = "cassandra.row_count"

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.3" # optional since v1.27.0
services:
cassandra:
image: cassandra:3.7
image: cassandra:3.11
environment:
JVM_OPTS: "-Xms750m -Xmx750m"
ports:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ require (
go.opentelemetry.io/otel v1.20.0
go.opentelemetry.io/otel/trace v1.20.0
go.uber.org/atomic v1.11.0
golang.org/x/mod v0.14.0
golang.org/x/net v0.23.0
golang.org/x/oauth2 v0.9.0
golang.org/x/sys v0.20.0
Expand Down Expand Up @@ -260,7 +261,6 @@ require (
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down

0 comments on commit a20143b

Please sign in to comment.