Skip to content

Commit ee5433f

Browse files
fix: Update gocql instrumentation to meet latest contrib requirements (#298)
* fix: change options to accept providers * fix: include semver in instrumentation creation * fix: lint * fix: Change WithTraceProvider to WithTracerProvider Co-authored-by: Tyler Yahn <[email protected]>
1 parent e7d4f5f commit ee5433f

File tree

8 files changed

+143
-104
lines changed

8 files changed

+143
-104
lines changed

instrumentation/github.com/gocql/gocql/README.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

instrumentation/github.com/gocql/gocql/config.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ import (
1818
"github.com/gocql/gocql"
1919

2020
"go.opentelemetry.io/otel/api/global"
21+
"go.opentelemetry.io/otel/api/metric"
2122
"go.opentelemetry.io/otel/api/trace"
2223
)
2324

2425
// TracedSessionConfig provides configuration for sessions
2526
// created with NewSessionWithTracing.
2627
type TracedSessionConfig struct {
27-
tracer trace.Tracer
28+
tracerProvider trace.Provider
29+
meterProvider metric.Provider
2830
instrumentQuery bool
2931
instrumentBatch bool
3032
instrumentConnect bool
@@ -78,12 +80,20 @@ func WithConnectObserver(observer gocql.ConnectObserver) TracedSessionOption {
7880
})
7981
}
8082

81-
// WithTracer will set tracer to be the tracer used to create spans
82-
// for query, batch query, and connection instrumentation.
83-
// Defaults to global.Tracer("go.opentelemetry.io/contrib/instrumentation/github.com/gocql/gocql").
84-
func WithTracer(tracer trace.Tracer) TracedSessionOption {
83+
// WithTracerProvider will set the trace provider used to get a tracer
84+
// for creating spans. Defaults to global.TraceProvider()
85+
func WithTracerProvider(provider trace.Provider) TracedSessionOption {
8586
return TracedSessionOptionFunc(func(c *TracedSessionConfig) {
86-
c.tracer = tracer
87+
c.tracerProvider = provider
88+
})
89+
}
90+
91+
// WithMeterProvider will set the meter provider used to get a meter
92+
// for creating instruments.
93+
// Defaults to global.MeterProvider().
94+
func WithMeterProvider(provider metric.Provider) TracedSessionOption {
95+
return TracedSessionOptionFunc(func(c *TracedSessionConfig) {
96+
c.meterProvider = provider
8797
})
8898
}
8999

@@ -115,7 +125,8 @@ func WithConnectInstrumentation(enabled bool) TracedSessionOption {
115125

116126
func configure(options ...TracedSessionOption) *TracedSessionConfig {
117127
config := &TracedSessionConfig{
118-
tracer: global.Tracer(instrumentationName),
128+
tracerProvider: global.TraceProvider(),
129+
meterProvider: global.MeterProvider(),
119130
instrumentQuery: true,
120131
instrumentBatch: true,
121132
instrumentConnect: true,

instrumentation/github.com/gocql/gocql/example/client.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func main() {
110110

111111
func initMetrics() {
112112
// Start prometheus
113-
metricExporter, err := prometheus.NewExportPipeline(prometheus.Config{})
113+
metricExporter, err := prometheus.InstallNewPipeline(prometheus.Config{})
114114
if err != nil {
115115
log.Fatalf("failed to install metric exporter, %v", err)
116116
}
@@ -133,8 +133,6 @@ func initMetrics() {
133133
log.Print("gracefully shutting down server")
134134
}
135135
}()
136-
137-
otelGocql.InstrumentWithProvider(metricExporter.Provider())
138136
}
139137

140138
func initTracer() {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package gocql
16+
17+
import (
18+
"context"
19+
"log"
20+
21+
"github.com/gocql/gocql"
22+
)
23+
24+
func ExampleNewSessionWithTracing() {
25+
// Create a cluster
26+
host := "localhost"
27+
cluster := gocql.NewCluster(host)
28+
29+
// Create a session with tracing
30+
_, err := NewSessionWithTracing(
31+
context.Background(),
32+
cluster,
33+
// Include any options here
34+
)
35+
36+
if err != nil {
37+
log.Fatalf("failed to create session, %v", err)
38+
}
39+
40+
// Begin using the session
41+
}

instrumentation/github.com/gocql/gocql/gocql.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,39 @@ import (
1818
"context"
1919

2020
"github.com/gocql/gocql"
21+
22+
otelcontrib "go.opentelemetry.io/contrib"
23+
"go.opentelemetry.io/otel/api/trace"
2124
)
2225

2326
// NewSessionWithTracing creates a new session using the given cluster
2427
// configuration enabling tracing for queries, batch queries, and connection attempts.
2528
// You may use additional observers and disable specific tracing using the provided `TracedSessionOption`s.
2629
func NewSessionWithTracing(ctx context.Context, cluster *gocql.ClusterConfig, options ...TracedSessionOption) (*gocql.Session, error) {
2730
config := configure(options...)
31+
instruments := newInstruments(config.meterProvider)
32+
tracer := config.tracerProvider.Tracer(
33+
instrumentationName,
34+
trace.WithInstrumentationVersion(otelcontrib.SemVersion()),
35+
)
2836
cluster.QueryObserver = &OTelQueryObserver{
2937
enabled: config.instrumentQuery,
3038
observer: config.queryObserver,
31-
tracer: config.tracer,
39+
tracer: tracer,
40+
inst: instruments,
3241
}
3342
cluster.BatchObserver = &OTelBatchObserver{
3443
enabled: config.instrumentBatch,
3544
observer: config.batchObserver,
36-
tracer: config.tracer,
45+
tracer: tracer,
46+
inst: instruments,
3747
}
3848
cluster.ConnectObserver = &OTelConnectObserver{
3949
ctx: ctx,
4050
enabled: config.instrumentConnect,
4151
observer: config.connectObserver,
42-
tracer: config.tracer,
52+
tracer: tracer,
53+
inst: instruments,
4354
}
4455
return cluster.CreateSession()
4556
}

instrumentation/github.com/gocql/gocql/gocql_test.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ func mockExportPipeline(t *testing.T) *push.Controller {
8989
return controller
9090
}
9191

92+
type mockTracerProvider struct {
93+
tracer *mocktracer.Tracer
94+
}
95+
96+
func (p *mockTracerProvider) Tracer(name string, options ...trace.TracerOption) trace.Tracer {
97+
return p.tracer
98+
}
99+
100+
func newTracerProvider() *mockTracerProvider {
101+
return &mockTracerProvider{
102+
mocktracer.NewTracer(instrumentationName),
103+
}
104+
}
105+
92106
type mockConnectObserver struct {
93107
callCount int
94108
}
@@ -108,14 +122,15 @@ func TestQuery(t *testing.T) {
108122
controller := getController(t)
109123
defer afterEach()
110124
cluster := getCluster()
111-
tracer := mocktracer.NewTracer("gocql-test")
125+
tracerProvider := newTracerProvider()
112126

113-
ctx, parentSpan := tracer.Start(context.Background(), "gocql-test")
127+
ctx, parentSpan := tracerProvider.tracer.Start(context.Background(), "gocql-test")
114128

115129
session, err := NewSessionWithTracing(
116130
ctx,
117131
cluster,
118-
WithTracer(tracer),
132+
WithTracerProvider(tracerProvider),
133+
WithMeterProvider(controller.Provider()),
119134
WithConnectInstrumentation(false),
120135
)
121136
require.NoError(t, err)
@@ -132,7 +147,7 @@ func TestQuery(t *testing.T) {
132147
parentSpan.End()
133148

134149
// Get the spans and ensure that they are child spans to the local parent
135-
spans := tracer.EndedSpans()
150+
spans := tracerProvider.tracer.EndedSpans()
136151

137152
// Collect all the connection spans
138153
// total spans:
@@ -229,14 +244,15 @@ func TestBatch(t *testing.T) {
229244
controller := getController(t)
230245
defer afterEach()
231246
cluster := getCluster()
232-
tracer := mocktracer.NewTracer("gocql-test")
247+
tracerProvider := newTracerProvider()
233248

234-
ctx, parentSpan := tracer.Start(context.Background(), "gocql-test")
249+
ctx, parentSpan := tracerProvider.tracer.Start(context.Background(), "gocql-test")
235250

236251
session, err := NewSessionWithTracing(
237252
ctx,
238253
cluster,
239-
WithTracer(tracer),
254+
WithTracerProvider(tracerProvider),
255+
WithMeterProvider(controller.Provider()),
240256
WithConnectInstrumentation(false),
241257
)
242258
require.NoError(t, err)
@@ -255,7 +271,7 @@ func TestBatch(t *testing.T) {
255271

256272
parentSpan.End()
257273

258-
spans := tracer.EndedSpans()
274+
spans := tracerProvider.tracer.EndedSpans()
259275
// total spans:
260276
// 1 span for the query
261277
// 1 span for the local span
@@ -326,21 +342,22 @@ func TestConnection(t *testing.T) {
326342
controller := getController(t)
327343
defer afterEach()
328344
cluster := getCluster()
329-
tracer := mocktracer.NewTracer("gocql-test")
345+
tracerProvider := newTracerProvider()
330346
connectObserver := &mockConnectObserver{0}
331347
ctx := context.Background()
332348

333349
session, err := NewSessionWithTracing(
334350
ctx,
335351
cluster,
336-
WithTracer(tracer),
352+
WithTracerProvider(tracerProvider),
353+
WithMeterProvider(controller.Provider()),
337354
WithConnectObserver(connectObserver),
338355
)
339356
require.NoError(t, err)
340357
defer session.Close()
341358
require.NoError(t, session.AwaitSchemaAgreement(ctx))
342359

343-
spans := tracer.EndedSpans()
360+
spans := tracerProvider.tracer.EndedSpans()
344361

345362
assert.Less(t, 0, connectObserver.callCount)
346363

@@ -422,7 +439,6 @@ func getCluster() *gocql.ClusterConfig {
422439
// export pipeline.
423440
func getController(t *testing.T) *push.Controller {
424441
controller := mockExportPipeline(t)
425-
InstrumentWithProvider(controller.Provider())
426442
return controller
427443
}
428444

0 commit comments

Comments
 (0)