Skip to content

Commit

Permalink
fix: TestCreateCollectorProxy unit test failing on go-tip (#6204)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
- Resolves #6198 

## Description of the changes
- After the addition of `t.Parallel` in `cmd/agent/app/builder_test.go`
in `TestCreateCollectorProxy`, its test started to fail during random
ittterations. This was because in
`/cmd/agent/app/reporter/connect_metrics.go` under `NewConnectMetrics`
function we are doing `expvar.NewString("gRPCTarget")` which isnt
thread-safe and it should not be changed on initialized
- Now we use sync.Once to tackle the above bug/problem

## How was this change tested?
- running `go test -v`

## Checklist
- [x] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [ ] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `yarn lint` and `yarn test`

Signed-off-by: Saumya Shah <[email protected]>
Co-authored-by: Yuri Shkuro <[email protected]>
  • Loading branch information
Saumya40-codes and yurishkuro authored Nov 12, 2024
1 parent d43fc84 commit 6e695b3
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions cmd/agent/app/reporter/connect_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package reporter

import (
"expvar"
"sync"

"github.com/jaegertracing/jaeger/pkg/metrics"
)
Expand All @@ -23,17 +24,21 @@ type ConnectMetrics struct {
target *expvar.String
}

var (
gRPCTarget *expvar.String
gRPCTargetOnce sync.Once
)

// NewConnectMetrics will be initialize ConnectMetrics
func NewConnectMetrics(mf metrics.Factory) *ConnectMetrics {
cm := &ConnectMetrics{}
metrics.MustInit(&cm.metrics, mf.Namespace(metrics.NSOptions{Name: "connection_status"}), nil)

if r := expvar.Get("gRPCTarget"); r == nil {
cm.target = expvar.NewString("gRPCTarget")
} else {
cm.target = r.(*expvar.String)
}

// expvar.String is not thread-safe, so we need to initialize it only once
gRPCTargetOnce.Do(func() {
gRPCTarget = expvar.NewString("gRPCTarget")
})
cm.target = gRPCTarget
return cm
}

Expand Down

0 comments on commit 6e695b3

Please sign in to comment.