instruments.go (8468B)
1 // Copyright The OpenTelemetry 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 global // import "go.opentelemetry.io/otel/internal/global" 16 17 import ( 18 "context" 19 "sync/atomic" 20 21 "go.opentelemetry.io/otel/metric" 22 "go.opentelemetry.io/otel/metric/embedded" 23 ) 24 25 // unwrapper unwraps to return the underlying instrument implementation. 26 type unwrapper interface { 27 Unwrap() metric.Observable 28 } 29 30 type afCounter struct { 31 embedded.Float64ObservableCounter 32 metric.Float64Observable 33 34 name string 35 opts []metric.Float64ObservableCounterOption 36 37 delegate atomic.Value //metric.Float64ObservableCounter 38 } 39 40 var _ unwrapper = (*afCounter)(nil) 41 var _ metric.Float64ObservableCounter = (*afCounter)(nil) 42 43 func (i *afCounter) setDelegate(m metric.Meter) { 44 ctr, err := m.Float64ObservableCounter(i.name, i.opts...) 45 if err != nil { 46 GetErrorHandler().Handle(err) 47 return 48 } 49 i.delegate.Store(ctr) 50 } 51 52 func (i *afCounter) Unwrap() metric.Observable { 53 if ctr := i.delegate.Load(); ctr != nil { 54 return ctr.(metric.Float64ObservableCounter) 55 } 56 return nil 57 } 58 59 type afUpDownCounter struct { 60 embedded.Float64ObservableUpDownCounter 61 metric.Float64Observable 62 63 name string 64 opts []metric.Float64ObservableUpDownCounterOption 65 66 delegate atomic.Value //metric.Float64ObservableUpDownCounter 67 } 68 69 var _ unwrapper = (*afUpDownCounter)(nil) 70 var _ metric.Float64ObservableUpDownCounter = (*afUpDownCounter)(nil) 71 72 func (i *afUpDownCounter) setDelegate(m metric.Meter) { 73 ctr, err := m.Float64ObservableUpDownCounter(i.name, i.opts...) 74 if err != nil { 75 GetErrorHandler().Handle(err) 76 return 77 } 78 i.delegate.Store(ctr) 79 } 80 81 func (i *afUpDownCounter) Unwrap() metric.Observable { 82 if ctr := i.delegate.Load(); ctr != nil { 83 return ctr.(metric.Float64ObservableUpDownCounter) 84 } 85 return nil 86 } 87 88 type afGauge struct { 89 embedded.Float64ObservableGauge 90 metric.Float64Observable 91 92 name string 93 opts []metric.Float64ObservableGaugeOption 94 95 delegate atomic.Value //metric.Float64ObservableGauge 96 } 97 98 var _ unwrapper = (*afGauge)(nil) 99 var _ metric.Float64ObservableGauge = (*afGauge)(nil) 100 101 func (i *afGauge) setDelegate(m metric.Meter) { 102 ctr, err := m.Float64ObservableGauge(i.name, i.opts...) 103 if err != nil { 104 GetErrorHandler().Handle(err) 105 return 106 } 107 i.delegate.Store(ctr) 108 } 109 110 func (i *afGauge) Unwrap() metric.Observable { 111 if ctr := i.delegate.Load(); ctr != nil { 112 return ctr.(metric.Float64ObservableGauge) 113 } 114 return nil 115 } 116 117 type aiCounter struct { 118 embedded.Int64ObservableCounter 119 metric.Int64Observable 120 121 name string 122 opts []metric.Int64ObservableCounterOption 123 124 delegate atomic.Value //metric.Int64ObservableCounter 125 } 126 127 var _ unwrapper = (*aiCounter)(nil) 128 var _ metric.Int64ObservableCounter = (*aiCounter)(nil) 129 130 func (i *aiCounter) setDelegate(m metric.Meter) { 131 ctr, err := m.Int64ObservableCounter(i.name, i.opts...) 132 if err != nil { 133 GetErrorHandler().Handle(err) 134 return 135 } 136 i.delegate.Store(ctr) 137 } 138 139 func (i *aiCounter) Unwrap() metric.Observable { 140 if ctr := i.delegate.Load(); ctr != nil { 141 return ctr.(metric.Int64ObservableCounter) 142 } 143 return nil 144 } 145 146 type aiUpDownCounter struct { 147 embedded.Int64ObservableUpDownCounter 148 metric.Int64Observable 149 150 name string 151 opts []metric.Int64ObservableUpDownCounterOption 152 153 delegate atomic.Value //metric.Int64ObservableUpDownCounter 154 } 155 156 var _ unwrapper = (*aiUpDownCounter)(nil) 157 var _ metric.Int64ObservableUpDownCounter = (*aiUpDownCounter)(nil) 158 159 func (i *aiUpDownCounter) setDelegate(m metric.Meter) { 160 ctr, err := m.Int64ObservableUpDownCounter(i.name, i.opts...) 161 if err != nil { 162 GetErrorHandler().Handle(err) 163 return 164 } 165 i.delegate.Store(ctr) 166 } 167 168 func (i *aiUpDownCounter) Unwrap() metric.Observable { 169 if ctr := i.delegate.Load(); ctr != nil { 170 return ctr.(metric.Int64ObservableUpDownCounter) 171 } 172 return nil 173 } 174 175 type aiGauge struct { 176 embedded.Int64ObservableGauge 177 metric.Int64Observable 178 179 name string 180 opts []metric.Int64ObservableGaugeOption 181 182 delegate atomic.Value //metric.Int64ObservableGauge 183 } 184 185 var _ unwrapper = (*aiGauge)(nil) 186 var _ metric.Int64ObservableGauge = (*aiGauge)(nil) 187 188 func (i *aiGauge) setDelegate(m metric.Meter) { 189 ctr, err := m.Int64ObservableGauge(i.name, i.opts...) 190 if err != nil { 191 GetErrorHandler().Handle(err) 192 return 193 } 194 i.delegate.Store(ctr) 195 } 196 197 func (i *aiGauge) Unwrap() metric.Observable { 198 if ctr := i.delegate.Load(); ctr != nil { 199 return ctr.(metric.Int64ObservableGauge) 200 } 201 return nil 202 } 203 204 // Sync Instruments. 205 type sfCounter struct { 206 embedded.Float64Counter 207 208 name string 209 opts []metric.Float64CounterOption 210 211 delegate atomic.Value //metric.Float64Counter 212 } 213 214 var _ metric.Float64Counter = (*sfCounter)(nil) 215 216 func (i *sfCounter) setDelegate(m metric.Meter) { 217 ctr, err := m.Float64Counter(i.name, i.opts...) 218 if err != nil { 219 GetErrorHandler().Handle(err) 220 return 221 } 222 i.delegate.Store(ctr) 223 } 224 225 func (i *sfCounter) Add(ctx context.Context, incr float64, opts ...metric.AddOption) { 226 if ctr := i.delegate.Load(); ctr != nil { 227 ctr.(metric.Float64Counter).Add(ctx, incr, opts...) 228 } 229 } 230 231 type sfUpDownCounter struct { 232 embedded.Float64UpDownCounter 233 234 name string 235 opts []metric.Float64UpDownCounterOption 236 237 delegate atomic.Value //metric.Float64UpDownCounter 238 } 239 240 var _ metric.Float64UpDownCounter = (*sfUpDownCounter)(nil) 241 242 func (i *sfUpDownCounter) setDelegate(m metric.Meter) { 243 ctr, err := m.Float64UpDownCounter(i.name, i.opts...) 244 if err != nil { 245 GetErrorHandler().Handle(err) 246 return 247 } 248 i.delegate.Store(ctr) 249 } 250 251 func (i *sfUpDownCounter) Add(ctx context.Context, incr float64, opts ...metric.AddOption) { 252 if ctr := i.delegate.Load(); ctr != nil { 253 ctr.(metric.Float64UpDownCounter).Add(ctx, incr, opts...) 254 } 255 } 256 257 type sfHistogram struct { 258 embedded.Float64Histogram 259 260 name string 261 opts []metric.Float64HistogramOption 262 263 delegate atomic.Value //metric.Float64Histogram 264 } 265 266 var _ metric.Float64Histogram = (*sfHistogram)(nil) 267 268 func (i *sfHistogram) setDelegate(m metric.Meter) { 269 ctr, err := m.Float64Histogram(i.name, i.opts...) 270 if err != nil { 271 GetErrorHandler().Handle(err) 272 return 273 } 274 i.delegate.Store(ctr) 275 } 276 277 func (i *sfHistogram) Record(ctx context.Context, x float64, opts ...metric.RecordOption) { 278 if ctr := i.delegate.Load(); ctr != nil { 279 ctr.(metric.Float64Histogram).Record(ctx, x, opts...) 280 } 281 } 282 283 type siCounter struct { 284 embedded.Int64Counter 285 286 name string 287 opts []metric.Int64CounterOption 288 289 delegate atomic.Value //metric.Int64Counter 290 } 291 292 var _ metric.Int64Counter = (*siCounter)(nil) 293 294 func (i *siCounter) setDelegate(m metric.Meter) { 295 ctr, err := m.Int64Counter(i.name, i.opts...) 296 if err != nil { 297 GetErrorHandler().Handle(err) 298 return 299 } 300 i.delegate.Store(ctr) 301 } 302 303 func (i *siCounter) Add(ctx context.Context, x int64, opts ...metric.AddOption) { 304 if ctr := i.delegate.Load(); ctr != nil { 305 ctr.(metric.Int64Counter).Add(ctx, x, opts...) 306 } 307 } 308 309 type siUpDownCounter struct { 310 embedded.Int64UpDownCounter 311 312 name string 313 opts []metric.Int64UpDownCounterOption 314 315 delegate atomic.Value //metric.Int64UpDownCounter 316 } 317 318 var _ metric.Int64UpDownCounter = (*siUpDownCounter)(nil) 319 320 func (i *siUpDownCounter) setDelegate(m metric.Meter) { 321 ctr, err := m.Int64UpDownCounter(i.name, i.opts...) 322 if err != nil { 323 GetErrorHandler().Handle(err) 324 return 325 } 326 i.delegate.Store(ctr) 327 } 328 329 func (i *siUpDownCounter) Add(ctx context.Context, x int64, opts ...metric.AddOption) { 330 if ctr := i.delegate.Load(); ctr != nil { 331 ctr.(metric.Int64UpDownCounter).Add(ctx, x, opts...) 332 } 333 } 334 335 type siHistogram struct { 336 embedded.Int64Histogram 337 338 name string 339 opts []metric.Int64HistogramOption 340 341 delegate atomic.Value //metric.Int64Histogram 342 } 343 344 var _ metric.Int64Histogram = (*siHistogram)(nil) 345 346 func (i *siHistogram) setDelegate(m metric.Meter) { 347 ctr, err := m.Int64Histogram(i.name, i.opts...) 348 if err != nil { 349 GetErrorHandler().Handle(err) 350 return 351 } 352 i.delegate.Store(ctr) 353 } 354 355 func (i *siHistogram) Record(ctx context.Context, x int64, opts ...metric.RecordOption) { 356 if ctr := i.delegate.Load(); ctr != nil { 357 ctr.(metric.Int64Histogram).Record(ctx, x, opts...) 358 } 359 }