@@ -43,6 +43,13 @@ type Int struct{ i atomic.Int64 }
4343// variable will be available via expvars package as well, but can not be removed
4444// anymore.
4545func NewInt (r * Registry , name string , opts ... Option ) * Int {
46+ rr := r
47+ if rr == nil {
48+ rr = Default
49+ }
50+ rr .txMu .Lock ()
51+ defer rr .txMu .Unlock ()
52+
4653 existingVar , r := setupMetric (r , name , opts )
4754 if existingVar != nil {
4855 cast , ok := existingVar .(* Int )
@@ -77,6 +84,13 @@ type Uint struct{ u atomic.Uint64 }
7784// variable will be available via expvars package as well, but can not be removed
7885// anymore.
7986func NewUint (r * Registry , name string , opts ... Option ) * Uint {
87+ rr := r
88+ if rr == nil {
89+ rr = Default
90+ }
91+ rr .txMu .Lock ()
92+ defer rr .txMu .Unlock ()
93+
8094 existingVar , r := setupMetric (r , name , opts )
8195 if existingVar != nil {
8296 cast , ok := existingVar .(* Uint )
@@ -114,6 +128,13 @@ type Float struct{ f atomic.Uint64 }
114128// variable will be available via expvars package as well, but can not be removed
115129// anymore.
116130func NewFloat (r * Registry , name string , opts ... Option ) * Float {
131+ rr := r
132+ if rr == nil {
133+ rr = Default
134+ }
135+ rr .txMu .Lock ()
136+ defer rr .txMu .Unlock ()
137+
117138 existingVar , r := setupMetric (r , name , opts )
118139 if existingVar != nil {
119140 cast , ok := existingVar .(* Float )
@@ -155,6 +176,13 @@ type Bool struct{ f atomic.Bool }
155176// variable will be available via expvars package as well, but can not be removed
156177// anymore.
157178func NewBool (r * Registry , name string , opts ... Option ) * Bool {
179+ rr := r
180+ if rr == nil {
181+ rr = Default
182+ }
183+ rr .txMu .Lock ()
184+ defer rr .txMu .Unlock ()
185+
158186 existingVar , r := setupMetric (r , name , opts )
159187 if existingVar != nil {
160188 cast , ok := existingVar .(* Bool )
@@ -188,6 +216,13 @@ type String struct {
188216// variable will be available via expvars package as well, but can not be removed
189217// anymore.
190218func NewString (r * Registry , name string , opts ... Option ) * String {
219+ rr := r
220+ if rr == nil {
221+ rr = Default
222+ }
223+ rr .txMu .Lock ()
224+ defer rr .txMu .Unlock ()
225+
191226 existingVar , r := setupMetric (r , name , opts )
192227 if existingVar != nil {
193228 cast , ok := existingVar .(* String )
@@ -239,6 +274,13 @@ type Func struct {
239274}
240275
241276func NewFunc (r * Registry , name string , f func (Mode , Visitor ), opts ... Option ) * Func {
277+ rr := r
278+ if rr == nil {
279+ rr = Default
280+ }
281+ rr .txMu .Lock ()
282+ defer rr .txMu .Unlock ()
283+
242284 existingVar , r := setupMetric (r , name , opts )
243285 if existingVar != nil {
244286 cast , ok := existingVar .(* Func )
@@ -282,6 +324,13 @@ type Timestamp struct {
282324
283325// NewTimestamp creates and registers a new timestamp variable.
284326func NewTimestamp (r * Registry , name string , opts ... Option ) * Timestamp {
327+ rr := r
328+ if rr == nil {
329+ rr = Default
330+ }
331+ rr .txMu .Lock ()
332+ defer rr .txMu .Unlock ()
333+
285334 existingVar , r := setupMetric (r , name , opts )
286335 if existingVar != nil {
287336 cast , ok := existingVar .(* Timestamp )
0 commit comments