instruments.go 8.4 KB

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