@@ -43,6 +43,9 @@ 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+ r .txMu .Lock ()
47+ defer r .txMu .Unlock ()
48+
4649 existingVar , r := setupMetric (r , name , opts )
4750 if existingVar != nil {
4851 cast , ok := existingVar .(* Int )
@@ -77,6 +80,9 @@ type Uint struct{ u atomic.Uint64 }
7780// variable will be available via expvars package as well, but can not be removed
7881// anymore.
7982func NewUint (r * Registry , name string , opts ... Option ) * Uint {
83+ r .txMu .Lock ()
84+ defer r .txMu .Unlock ()
85+
8086 existingVar , r := setupMetric (r , name , opts )
8187 if existingVar != nil {
8288 cast , ok := existingVar .(* Uint )
@@ -114,6 +120,9 @@ type Float struct{ f atomic.Uint64 }
114120// variable will be available via expvars package as well, but can not be removed
115121// anymore.
116122func NewFloat (r * Registry , name string , opts ... Option ) * Float {
123+ r .txMu .Lock ()
124+ defer r .txMu .Unlock ()
125+
117126 existingVar , r := setupMetric (r , name , opts )
118127 if existingVar != nil {
119128 cast , ok := existingVar .(* Float )
@@ -155,6 +164,9 @@ type Bool struct{ f atomic.Bool }
155164// variable will be available via expvars package as well, but can not be removed
156165// anymore.
157166func NewBool (r * Registry , name string , opts ... Option ) * Bool {
167+ r .txMu .Lock ()
168+ defer r .txMu .Unlock ()
169+
158170 existingVar , r := setupMetric (r , name , opts )
159171 if existingVar != nil {
160172 cast , ok := existingVar .(* Bool )
@@ -188,6 +200,9 @@ type String struct {
188200// variable will be available via expvars package as well, but can not be removed
189201// anymore.
190202func NewString (r * Registry , name string , opts ... Option ) * String {
203+ r .txMu .Lock ()
204+ defer r .txMu .Unlock ()
205+
191206 existingVar , r := setupMetric (r , name , opts )
192207 if existingVar != nil {
193208 cast , ok := existingVar .(* String )
@@ -239,6 +254,9 @@ type Func struct {
239254}
240255
241256func NewFunc (r * Registry , name string , f func (Mode , Visitor ), opts ... Option ) * Func {
257+ r .txMu .Lock ()
258+ defer r .txMu .Unlock ()
259+
242260 existingVar , r := setupMetric (r , name , opts )
243261 if existingVar != nil {
244262 cast , ok := existingVar .(* Func )
@@ -282,6 +300,9 @@ type Timestamp struct {
282300
283301// NewTimestamp creates and registers a new timestamp variable.
284302func NewTimestamp (r * Registry , name string , opts ... Option ) * Timestamp {
303+ r .txMu .Lock ()
304+ defer r .txMu .Unlock ()
305+
285306 existingVar , r := setupMetric (r , name , opts )
286307 if existingVar != nil {
287308 cast , ok := existingVar .(* Timestamp )
0 commit comments