Skip to content

Commit bab9ec7

Browse files
committed
replace global scoped otel tracers with function local ones
This fixes a issue where the trace spans were not being propagated because the tracers were globally scoped.
1 parent fc073d6 commit bab9ec7

File tree

4 files changed

+20
-46
lines changed

4 files changed

+20
-46
lines changed

internal/collector/collector.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/metal-toolbox/alloy/internal/store"
1717
"github.com/pkg/errors"
1818
"github.com/sirupsen/logrus"
19-
"go.opentelemetry.io/otel"
2019
"go.opentelemetry.io/otel/trace"
2120
)
2221

@@ -269,11 +268,6 @@ func NewAssetIterCollectorWithStore(
269268

270269
// Collect iterates over assets returned by the AssetIterator and collects their inventory, bios configuration data.
271270
func (d *AssetIterCollector) Collect(ctx context.Context) {
272-
tracer := otel.Tracer("collector.AssetIteratorCollector")
273-
ctx, span := tracer.Start(context.TODO(), "Collect()")
274-
275-
defer span.End()
276-
277271
// pauser helps throttle asset retrieval to match the data collection rate.
278272
pauser := NewPauser()
279273

@@ -339,7 +333,7 @@ Loop:
339333
atomic.AddInt32(&dispatched, 1)
340334

341335
// throttle asset iterator based on dispatched vs concurrency limit
342-
d.throttle(span, pauser, dispatched)
336+
d.throttle(nil, pauser, dispatched)
343337

344338
// run collection in routine
345339
go func(ctx context.Context, asset *model.Asset) {

internal/device/outofband/outofband.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,21 @@ import (
2323
)
2424

2525
var (
26-
// The outofband collector tracer
27-
tracer trace.Tracer
2826
ErrInventory = errors.New("inventory collection error")
2927
ErrBiosConfig = errors.New("BIOS configuration collection error")
3028
ErrConnect = errors.New("BMC connection error")
3129
ErrBMCSession = errors.New("BMC session error")
3230
)
3331

34-
func init() {
35-
tracer = otel.Tracer("outofband.Queryor")
36-
}
37-
3832
const (
3933
// logoutTimeout is the timeout value for each bmc logout attempt.
4034
logoutTimeout = "1m"
4135

4236
// bmclib will attempt multiple providers (drivers) - to perform an action,
4337
// this is maximum amount of time bmclib will spend performing a query on a BMC.
4438
bmclibProviderTimeout = 180 * time.Second
39+
40+
pkgName = "internal/outofband"
4541
)
4642

4743
// OutOfBand collector collects hardware, firmware inventory out of band
@@ -84,7 +80,8 @@ func NewQueryor(logger *logrus.Logger) *Queryor {
8480
// Inventory retrieves device component and firmware information
8581
// and updates the given asset object with the inventory
8682
func (o *Queryor) Inventory(ctx context.Context, asset *model.Asset) error {
87-
ctx, span := tracer.Start(ctx, "Inventory()")
83+
// attach child span
84+
ctx, span := otel.Tracer(pkgName).Start(ctx, "Inventory")
8885
defer span.End()
8986

9087
setTraceSpanAssetAttributes(span, asset)
@@ -119,9 +116,11 @@ func (o *Queryor) Inventory(ctx context.Context, asset *model.Asset) error {
119116

120117
func (o *Queryor) BiosConfiguration(ctx context.Context, asset *model.Asset) error {
121118
// attach child span
122-
ctx, span := tracer.Start(ctx, "BiosConfiguration()")
119+
ctx, span := otel.Tracer(pkgName).Start(ctx, "BiosConfiguration")
123120
defer span.End()
124121

122+
setTraceSpanAssetAttributes(span, asset)
123+
125124
// login
126125
bmc, err := o.bmcLogin(ctx, asset)
127126
if err != nil {
@@ -250,7 +249,7 @@ func (o *Queryor) bmcLogin(ctx context.Context, asset *model.Asset) (BMCQueryor,
250249
var bmc BMCQueryor
251250

252251
// attach child span
253-
ctx, span := tracer.Start(ctx, "bmcLogin()")
252+
ctx, span := otel.Tracer(pkgName).Start(ctx, "bmcLogin")
254253
defer span.End()
255254

256255
if o.mockClient == nil {
@@ -298,8 +297,7 @@ func (o *Queryor) bmcLogout(bmc BMCQueryor, asset *model.Asset) {
298297
ctx, cancel := context.WithTimeout(context.Background(), o.logoutTimeout)
299298
defer cancel()
300299

301-
// attach child span
302-
ctx, span := tracer.Start(ctx, "bmcLogout()")
300+
ctx, span := otel.Tracer(pkgName).Start(ctx, "bmclibLogOut")
303301
defer span.End()
304302

305303
o.logger.WithFields(

internal/store/serverservice/serverservice.go

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,14 @@ import (
1919
"go.opentelemetry.io/otel"
2020
"go.opentelemetry.io/otel/attribute"
2121
"go.opentelemetry.io/otel/codes"
22-
"go.opentelemetry.io/otel/trace"
2322

2423
serverserviceapi "go.hollow.sh/serverservice/pkg/api/v1"
2524
)
2625

27-
var (
28-
29-
// The serverservice asset getter tracer
30-
tracer trace.Tracer
26+
const (
27+
pkgName = "internal/store"
3128
)
3229

33-
func init() {
34-
tracer = otel.Tracer("store.serverservice")
35-
}
36-
3730
// Store is an asset inventory store
3831
type Store struct {
3932
*serverserviceapi.Client
@@ -100,8 +93,7 @@ func (r *Store) Kind() model.StoreKind {
10093

10194
// assetByID queries serverService for the hardware asset by ID and returns an Asset object
10295
func (r *Store) AssetByID(ctx context.Context, id string, fetchBmcCredentials bool) (*model.Asset, error) {
103-
// attach child span
104-
ctx, span := tracer.Start(ctx, "AssetByID()")
96+
ctx, span := otel.Tracer(pkgName).Start(ctx, "Serverservice.AssetByID")
10597
defer span.End()
10698

10799
sid, err := uuid.Parse(id)
@@ -136,8 +128,8 @@ func (r *Store) AssetByID(ctx context.Context, id string, fetchBmcCredentials bo
136128

137129
// assetByID queries serverService for the hardware asset by ID and returns an Asset object
138130
func (r *Store) AssetsByOffsetLimit(ctx context.Context, offset, limit int) (assets []*model.Asset, totalAssets int, err error) {
139-
// attach child span
140-
ctx, span := tracer.Start(ctx, "AssetsByOffsetLimit()")
131+
ctx, span := otel.Tracer(pkgName).Start(ctx, "Serverservice.AssetByOffsetLimit")
132+
defer span.End()
141133

142134
span.SetAttributes(
143135
attribute.Int("offset", offset),
@@ -192,8 +184,7 @@ func (r *Store) AssetsByOffsetLimit(ctx context.Context, offset, limit int) (ass
192184

193185
// AssetUpdate inserts/updates the asset data in the serverservice store
194186
func (r *Store) AssetUpdate(ctx context.Context, asset *model.Asset) error {
195-
// attach child span
196-
ctx, span := tracer.Start(ctx, "AssetUpdate()")
187+
ctx, span := otel.Tracer(pkgName).Start(ctx, "Serverservice.AssetUpdate")
197188
defer span.End()
198189

199190
if asset == nil {
@@ -303,8 +294,7 @@ func (r *Store) createUpdateInventory(ctx context.Context, device *model.Asset,
303294
//
304295
// nolint:gocyclo // the method caries out all steps to have device data compared and registered, for now its accepted as cyclomatic.
305296
func (r *Store) createUpdateServerComponents(ctx context.Context, serverID uuid.UUID, device *model.Asset) error {
306-
// attach child span
307-
ctx, span := tracer.Start(ctx, "createUpdateServerComponents()")
297+
ctx, span := otel.Tracer(pkgName).Start(ctx, "Serverservice.createUpdateServerComponents")
308298
defer span.End()
309299

310300
if device.Inventory == nil {
@@ -472,10 +462,6 @@ func diffFilter(_ []string, _ reflect.Type, field reflect.StructField) bool {
472462
// serverServiceChangeList compares the current vs newer slice of server components
473463
// and returns 3 lists - add, update, remove.
474464
func serverServiceChangeList(ctx context.Context, currentObjs, newObjs []*serverserviceapi.ServerComponent) (add, update, remove serverserviceapi.ServerComponentSlice, err error) {
475-
// attach child span
476-
_, span := tracer.Start(ctx, "serverServiceChangeList()")
477-
defer span.End()
478-
479465
// 1. list updated and removed objects
480466
for _, currentObj := range currentObjs {
481467
// changeObj is the component changes to be registered
@@ -565,8 +551,7 @@ func serverServiceComponentsUpdated(currentObj, newObj *serverserviceapi.ServerC
565551
}
566552

567553
func (r *Store) cacheServerComponentFirmwares(ctx context.Context) error {
568-
// attach child span
569-
ctx, span := tracer.Start(ctx, "cacheServerComponentFirmwares()")
554+
ctx, span := otel.Tracer(pkgName).Start(ctx, "Serverservice.cacheServerComponentFirmwares")
570555
defer span.End()
571556

572557
// Query ServerService for all firmware
@@ -590,8 +575,7 @@ func (r *Store) cacheServerComponentFirmwares(ctx context.Context) error {
590575
}
591576

592577
func (r *Store) cacheServerComponentTypes(ctx context.Context) error {
593-
// attach child span
594-
ctx, span := tracer.Start(ctx, "cacheServerComponentTypes()")
578+
ctx, span := otel.Tracer(pkgName).Start(ctx, "Serverservice.cacheServerComponentTypes")
595579
defer span.End()
596580

597581
serverComponentTypes, _, err := r.ListServerComponentTypes(ctx, nil)
@@ -613,8 +597,7 @@ func (r *Store) cacheServerComponentTypes(ctx context.Context) error {
613597
}
614598

615599
func (r *Store) createServerComponentTypes(ctx context.Context) error {
616-
// attach child span
617-
ctx, span := tracer.Start(ctx, "createServerComponentTypes()")
600+
ctx, span := otel.Tracer(pkgName).Start(ctx, "Serverservice.createServerComponentTypes")
618601
defer span.End()
619602

620603
existing, _, err := r.ListServerComponentTypes(ctx, nil)

internal/store/store.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ type Repository interface {
2323
// AssetByID returns one asset from the inventory identified by its identifier.
2424
AssetByID(ctx context.Context, assetID string, fetchBmcCredentials bool) (*model.Asset, error)
2525

26-
// AssetByOffsetLimit queries the inventory for the asset(s) at the given offset, limit values.
2726
AssetsByOffsetLimit(ctx context.Context, offset, limit int) (assets []*model.Asset, totalAssets int, err error)
2827

2928
// AssetUpdate inserts and updates collected data for the asset in the store.

0 commit comments

Comments
 (0)