Skip to content

Commit ee6cc41

Browse files
authored
[hotrod] Handle both OT and OTEL baggage (#4572)
## Which problem is this PR solving? - Part of #3380 ## Short description of the changes - Move explicit baggage access into a util func that checks both OT and OTEL baggage. Later the OT part can be retired completely. - Update usage sites to use the new function instead of OT `Span.BaggageItem` A couple of less related changes in `tracing/mutex.go`: - Use OTEL Span API instead of OT - Replace direct-to-span logging with the regular logger that logs both to stdout and to the span Signed-off-by: Yuri Shkuro <[email protected]>
1 parent 10b7fe3 commit ee6cc41

File tree

4 files changed

+80
-27
lines changed

4 files changed

+80
-27
lines changed
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2023 The Jaeger 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 tracing
16+
17+
import (
18+
"context"
19+
20+
"github.com/opentracing/opentracing-go"
21+
"go.opentelemetry.io/otel/baggage"
22+
)
23+
24+
func BaggageItem(ctx context.Context, key string) string {
25+
val := opentracingBaggageItem(ctx, key)
26+
if val != "" {
27+
return val
28+
}
29+
return otelBaggageItem(ctx, key)
30+
}
31+
32+
func opentracingBaggageItem(ctx context.Context, key string) string {
33+
span := opentracing.SpanFromContext(ctx)
34+
if span == nil {
35+
return ""
36+
}
37+
return span.BaggageItem(key)
38+
}
39+
40+
func otelBaggageItem(ctx context.Context, key string) string {
41+
b := baggage.FromContext(ctx)
42+
m := b.Member(key)
43+
return m.Value()
44+
}

examples/hotrod/pkg/tracing/mutex.go

+19-16
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ import (
2020
"fmt"
2121
"sync"
2222

23-
"github.com/opentracing/opentracing-go"
24-
"github.com/opentracing/opentracing-go/log"
23+
"go.opentelemetry.io/otel/attribute"
24+
"go.opentelemetry.io/otel/trace"
25+
"go.uber.org/zap"
26+
27+
"github.com/jaegertracing/jaeger/examples/hotrod/pkg/log"
2528
)
2629

2730
// Mutex is just like the standard sync.Mutex, except that it is aware of the Context
2831
// and logs some diagnostic information into the current span.
2932
type Mutex struct {
3033
SessionBaggageKey string
34+
LogFactory log.Factory
3135

3236
realLock sync.Mutex
3337
holder string
@@ -38,19 +42,17 @@ type Mutex struct {
3842

3943
// Lock acquires an exclusive lock.
4044
func (sm *Mutex) Lock(ctx context.Context) {
41-
var session string
42-
activeSpan := opentracing.SpanFromContext(ctx)
43-
if activeSpan != nil {
44-
session = activeSpan.BaggageItem(sm.SessionBaggageKey)
45-
activeSpan.SetTag(sm.SessionBaggageKey, session)
46-
}
45+
logger := sm.LogFactory.For(ctx)
46+
session := BaggageItem(ctx, sm.SessionBaggageKey)
47+
activeSpan := trace.SpanFromContext(ctx)
48+
activeSpan.SetAttributes(attribute.String(sm.SessionBaggageKey, session))
4749

4850
sm.waitersLock.Lock()
4951
if waiting := len(sm.waiters); waiting > 0 && activeSpan != nil {
50-
activeSpan.LogFields(
51-
log.String("event", fmt.Sprintf("Waiting for lock behind %d transactions", waiting)),
52-
log.String("blockers", fmt.Sprintf("%v", sm.waiters))) // avoid deferred slice.String()
53-
fmt.Printf("%s Waiting for lock behind %d transactions: %v\n", session, waiting, sm.waiters)
52+
logger.Info(
53+
fmt.Sprintf("Waiting for lock behind %d transactions", waiting),
54+
zap.String("blockers", fmt.Sprintf("%v", sm.waiters)),
55+
)
5456
}
5557
sm.waiters = append(sm.waiters, session)
5658
sm.waitersLock.Unlock()
@@ -60,12 +62,13 @@ func (sm *Mutex) Lock(ctx context.Context) {
6062

6163
sm.waitersLock.Lock()
6264
behindLen := len(sm.waiters) - 1
65+
behindIDs := fmt.Sprintf("%v", sm.waiters[1:]) // skip self
6366
sm.waitersLock.Unlock()
6467

65-
if activeSpan != nil {
66-
activeSpan.LogFields(log.String("event",
67-
fmt.Sprintf("Acquired lock with %d transactions waiting behind", behindLen)))
68-
}
68+
logger.Info(
69+
fmt.Sprintf("Acquired lock; %d transactions waiting behind", behindLen),
70+
zap.String("waiters", behindIDs),
71+
)
6972
}
7073

7174
// Unlock releases the lock.

examples/hotrod/services/customer/database.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func newDatabase(tracer trace.Tracer, logger log.Factory) *database {
4444
logger: logger,
4545
lock: &tracing.Mutex{
4646
SessionBaggageKey: "request",
47+
LogFactory: logger,
4748
},
4849
customers: map[string]*Customer{
4950
"123": {
@@ -76,7 +77,10 @@ func (d *database) Get(ctx context.Context, customerID string) (*Customer, error
7677
// simulate opentracing instrumentation of an SQL query
7778
ctx, span := d.tracer.Start(ctx, "SQL SELECT", trace.WithSpanKind(trace.SpanKindClient))
7879
// #nosec
79-
span.SetAttributes(semconv.PeerServiceKey.String("mysql"), attribute.Key("sql.query").String("SELECT * FROM customer WHERE customer_id=" + customerID))
80+
span.SetAttributes(
81+
semconv.PeerServiceKey.String("mysql"),
82+
attribute.Key("sql.query").String("SELECT * FROM customer WHERE customer_id="+customerID),
83+
)
8084
defer span.End()
8185

8286
if !config.MySQLMutexDisabled {

examples/hotrod/services/route/stats.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,30 @@ import (
2020
"expvar"
2121
"time"
2222

23-
"github.com/opentracing/opentracing-go"
23+
"github.com/jaegertracing/jaeger/examples/hotrod/pkg/tracing"
2424
)
2525

2626
var routeCalcByCustomer = expvar.NewMap("route.calc.by.customer.sec")
2727
var routeCalcBySession = expvar.NewMap("route.calc.by.session.sec")
2828

2929
var stats = []struct {
30-
expvar *expvar.Map
31-
baggage string
30+
expvar *expvar.Map
31+
baggageKey string
3232
}{
33-
{routeCalcByCustomer, "customer"},
34-
{routeCalcBySession, "session"},
33+
{
34+
expvar: routeCalcByCustomer,
35+
baggageKey: "customer",
36+
},
37+
{
38+
expvar: routeCalcBySession,
39+
baggageKey: "session",
40+
},
3541
}
3642

3743
func updateCalcStats(ctx context.Context, delay time.Duration) {
38-
span := opentracing.SpanFromContext(ctx)
39-
if span == nil {
40-
return
41-
}
4244
delaySec := float64(delay/time.Millisecond) / 1000.0
4345
for _, s := range stats {
44-
key := span.BaggageItem(s.baggage)
46+
key := tracing.BaggageItem(ctx, s.baggageKey)
4547
if key != "" {
4648
s.expvar.AddFloat(key, delaySec)
4749
}

0 commit comments

Comments
 (0)