Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contrib/gocql/gocql: implement observer api based tracing #2805

Merged
merged 8 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/unit-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ jobs:
image: cassandra:3.11
env:
JVM_OPTS: "-Xms750m -Xmx750m"
CASSANDRA_CLUSTER_NAME: "dd-trace-go-test-cluster"
CASSANDRA_DC: "dd-trace-go-test-datacenter"
CASSANDRA_ENDPOINT_SNITCH: "GossipingPropertyFileSnitch"
ports:
- 9042:9042
mysql:
Expand Down
93 changes: 91 additions & 2 deletions contrib/gocql/gocql/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ package gocql_test

import (
"context"
"log"

"github.com/gocql/gocql"

gocqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gocql/gocql"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func Example() {
func ExampleNewCluster() {
// Initialise a wrapped Cassandra session and create a query.
cluster := gocqltrace.NewCluster([]string{"127.0.0.1"}, gocqltrace.WithServiceName("ServiceName"))
cluster := gocqltrace.NewCluster([]string{"127.0.0.1:9043"}, gocqltrace.WithServiceName("ServiceName"))
rarguelloF marked this conversation as resolved.
Show resolved Hide resolved
session, _ := cluster.CreateSession()
query := session.Query("CREATE KEYSPACE if not exists trace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor': 1}")

Expand All @@ -34,3 +37,89 @@ func Example() {
// Execute your query as usual
query.Exec()
}

func ExampleNewTracedSession() {
cluster := gocql.NewCluster("127.0.0.1:9042")
cluster.Keyspace = "my-keyspace"

// Create a new traced session using any number of options
session, err := gocqltrace.NewTracedSession(cluster, gocqltrace.WithServiceName("ServiceName"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ my understanding is that this specific call will NOT trace anything.

[EDIT] me wrong, indeed by default this is true, forget about this. Maybe only comment somewhere that the default is "it's traced".

Suggested change
session, err := gocqltrace.NewTracedSession(cluster, gocqltrace.WithServiceName("ServiceName"))
session, err := gocqltrace.NewTracedSession(cluster, gocqltrace.WithServiceName("ServiceName"), gocqltrace.WithTraceQuery(true), gocqltrace.WithTraceBatch(true), gocqltrace.WithTraceConnect(true))

as we have to explicitly pass WithTrace<Something> to activate tracing (but I may be wrong, just asking)

if err != nil {
log.Fatal(err)
}
query := session.Query("CREATE KEYSPACE if not exists trace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor': 1}")

// Use context to pass information down the call chain
_, ctx := tracer.StartSpanFromContext(context.Background(), "parent.request",
tracer.SpanType(ext.SpanTypeCassandra),
tracer.ServiceName("web"),
tracer.ResourceName("/home"),
)
query.WithContext(ctx)

// Finally, execute the query
if err := query.Exec(); err != nil {
log.Fatal(err)
}
}

func ExampleTraceQuery() {
cluster := gocql.NewCluster("127.0.0.1:9042")
cluster.Keyspace = "my-keyspace"

// Create a new regular gocql session
session, err := cluster.CreateSession()
if err != nil {
log.Fatal(err)
}
query := session.Query("CREATE KEYSPACE if not exists trace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor': 1}")

// Use context to pass information down the call chain
_, ctx := tracer.StartSpanFromContext(context.Background(), "parent.request",
tracer.SpanType(ext.SpanTypeCassandra),
tracer.ServiceName("web"),
tracer.ResourceName("/home"),
)
query.WithContext(ctx)

// Enable tracing this query only.
query = gocqltrace.TraceQuery(query, cluster, gocqltrace.WithServiceName("ServiceName"))

// Finally, execute the query
if err := query.Exec(); err != nil {
log.Fatal(err)
}
}

func ExampleTraceBatch() {
cluster := gocql.NewCluster("127.0.0.1:9042")
cluster.Keyspace = "my-keyspace"

// Create a new regular gocql session
session, err := cluster.CreateSession()
if err != nil {
log.Fatal(err)
}

// Create a new regular gocql batch and add some queries to it
stmt := "INSERT INTO trace.person (name, age, description) VALUES (?, ?, ?)"
batch := session.NewBatch(gocql.UnloggedBatch)
batch.Query(stmt, "Kate", 80, "Cassandra's sister running in kubernetes")
batch.Query(stmt, "Lucas", 60, "Another person")

// Use context to pass information down the call chain
_, ctx := tracer.StartSpanFromContext(context.Background(), "parent.request",
tracer.SpanType(ext.SpanTypeCassandra),
tracer.ServiceName("web"),
tracer.ResourceName("/home"),
)
batch.WithContext(ctx)

// Enable tracing this batch only
batch = gocqltrace.TraceBatch(batch, cluster, gocqltrace.WithServiceName("ServiceName"))

// Finally, execute the batch
if err := session.ExecuteBatch(batch); err != nil {
log.Fatal(err)
}
}
Loading
Loading