Skip to content

Commit

Permalink
contrib/gocql/gocql: implement observer api based tracing (#2805)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarguelloF authored and darccio committed Aug 19, 2024
1 parent da02d30 commit 86aa295
Show file tree
Hide file tree
Showing 11 changed files with 822 additions and 120 deletions.
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
67 changes: 65 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"))
session, _ := cluster.CreateSession()
query := session.Query("CREATE KEYSPACE if not exists trace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor': 1}")

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

func ExampleCreateTracedSession() {
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.CreateTracedSession(cluster, gocqltrace.WithServiceName("ServiceName"))
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)

// If you don't want a concrete query to be traced, you can do query.Observer(nil)

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

func ExampleNewObserver() {
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 observer using same set of options as gocqltrace.CreateTracedSession.
obs := gocqltrace.NewObserver(cluster, gocqltrace.WithServiceName("ServiceName"))

// Attach the observer to queries / batches individually.
tracedQuery := session.Query("SELECT something FROM somewhere").Observer(obs)
untracedQuery := session.Query("SELECT something FROM somewhere")

// 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"),
)
tracedQuery.WithContext(ctx)

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

0 comments on commit 86aa295

Please sign in to comment.