meter.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package global // import "go.opentelemetry.io/otel/internal/global"
  4. import (
  5. "container/list"
  6. "context"
  7. "reflect"
  8. "sync"
  9. "go.opentelemetry.io/otel/metric"
  10. "go.opentelemetry.io/otel/metric/embedded"
  11. )
  12. // meterProvider is a placeholder for a configured SDK MeterProvider.
  13. //
  14. // All MeterProvider functionality is forwarded to a delegate once
  15. // configured.
  16. type meterProvider struct {
  17. embedded.MeterProvider
  18. mtx sync.Mutex
  19. meters map[il]*meter
  20. delegate metric.MeterProvider
  21. }
  22. // setDelegate configures p to delegate all MeterProvider functionality to
  23. // provider.
  24. //
  25. // All Meters provided prior to this function call are switched out to be
  26. // Meters provided by provider. All instruments and callbacks are recreated and
  27. // delegated.
  28. //
  29. // It is guaranteed by the caller that this happens only once.
  30. func (p *meterProvider) setDelegate(provider metric.MeterProvider) {
  31. p.mtx.Lock()
  32. defer p.mtx.Unlock()
  33. p.delegate = provider
  34. if len(p.meters) == 0 {
  35. return
  36. }
  37. for _, meter := range p.meters {
  38. meter.setDelegate(provider)
  39. }
  40. p.meters = nil
  41. }
  42. // Meter implements MeterProvider.
  43. func (p *meterProvider) Meter(name string, opts ...metric.MeterOption) metric.Meter {
  44. p.mtx.Lock()
  45. defer p.mtx.Unlock()
  46. if p.delegate != nil {
  47. return p.delegate.Meter(name, opts...)
  48. }
  49. // At this moment it is guaranteed that no sdk is installed, save the meter in the meters map.
  50. c := metric.NewMeterConfig(opts...)
  51. key := il{
  52. name: name,
  53. version: c.InstrumentationVersion(),
  54. schema: c.SchemaURL(),
  55. attrs: c.InstrumentationAttributes(),
  56. }
  57. if p.meters == nil {
  58. p.meters = make(map[il]*meter)
  59. }
  60. if val, ok := p.meters[key]; ok {
  61. return val
  62. }
  63. t := &meter{name: name, opts: opts, instruments: make(map[instID]delegatedInstrument)}
  64. p.meters[key] = t
  65. return t
  66. }
  67. // meter is a placeholder for a metric.Meter.
  68. //
  69. // All Meter functionality is forwarded to a delegate once configured.
  70. // Otherwise, all functionality is forwarded to a NoopMeter.
  71. type meter struct {
  72. embedded.Meter
  73. name string
  74. opts []metric.MeterOption
  75. mtx sync.Mutex
  76. instruments map[instID]delegatedInstrument
  77. registry list.List
  78. delegate metric.Meter
  79. }
  80. type delegatedInstrument interface {
  81. setDelegate(metric.Meter)
  82. }
  83. // instID are the identifying properties of a instrument.
  84. type instID struct {
  85. // name is the name of the stream.
  86. name string
  87. // description is the description of the stream.
  88. description string
  89. // kind defines the functional group of the instrument.
  90. kind reflect.Type
  91. // unit is the unit of the stream.
  92. unit string
  93. }
  94. // setDelegate configures m to delegate all Meter functionality to Meters
  95. // created by provider.
  96. //
  97. // All subsequent calls to the Meter methods will be passed to the delegate.
  98. //
  99. // It is guaranteed by the caller that this happens only once.
  100. func (m *meter) setDelegate(provider metric.MeterProvider) {
  101. m.mtx.Lock()
  102. defer m.mtx.Unlock()
  103. meter := provider.Meter(m.name, m.opts...)
  104. m.delegate = meter
  105. for _, inst := range m.instruments {
  106. inst.setDelegate(meter)
  107. }
  108. var n *list.Element
  109. for e := m.registry.Front(); e != nil; e = n {
  110. r := e.Value.(*registration)
  111. r.setDelegate(meter)
  112. n = e.Next()
  113. m.registry.Remove(e)
  114. }
  115. m.instruments = nil
  116. m.registry.Init()
  117. }
  118. func (m *meter) Int64Counter(name string, options ...metric.Int64CounterOption) (metric.Int64Counter, error) {
  119. m.mtx.Lock()
  120. defer m.mtx.Unlock()
  121. if m.delegate != nil {
  122. return m.delegate.Int64Counter(name, options...)
  123. }
  124. cfg := metric.NewInt64CounterConfig(options...)
  125. id := instID{
  126. name: name,
  127. kind: reflect.TypeOf((*siCounter)(nil)),
  128. description: cfg.Description(),
  129. unit: cfg.Unit(),
  130. }
  131. if f, ok := m.instruments[id]; ok {
  132. return f.(metric.Int64Counter), nil
  133. }
  134. i := &siCounter{name: name, opts: options}
  135. m.instruments[id] = i
  136. return i, nil
  137. }
  138. func (m *meter) Int64UpDownCounter(name string, options ...metric.Int64UpDownCounterOption) (metric.Int64UpDownCounter, error) {
  139. m.mtx.Lock()
  140. defer m.mtx.Unlock()
  141. if m.delegate != nil {
  142. return m.delegate.Int64UpDownCounter(name, options...)
  143. }
  144. cfg := metric.NewInt64UpDownCounterConfig(options...)
  145. id := instID{
  146. name: name,
  147. kind: reflect.TypeOf((*siUpDownCounter)(nil)),
  148. description: cfg.Description(),
  149. unit: cfg.Unit(),
  150. }
  151. if f, ok := m.instruments[id]; ok {
  152. return f.(metric.Int64UpDownCounter), nil
  153. }
  154. i := &siUpDownCounter{name: name, opts: options}
  155. m.instruments[id] = i
  156. return i, nil
  157. }
  158. func (m *meter) Int64Histogram(name string, options ...metric.Int64HistogramOption) (metric.Int64Histogram, error) {
  159. m.mtx.Lock()
  160. defer m.mtx.Unlock()
  161. if m.delegate != nil {
  162. return m.delegate.Int64Histogram(name, options...)
  163. }
  164. cfg := metric.NewInt64HistogramConfig(options...)
  165. id := instID{
  166. name: name,
  167. kind: reflect.TypeOf((*siHistogram)(nil)),
  168. description: cfg.Description(),
  169. unit: cfg.Unit(),
  170. }
  171. if f, ok := m.instruments[id]; ok {
  172. return f.(metric.Int64Histogram), nil
  173. }
  174. i := &siHistogram{name: name, opts: options}
  175. m.instruments[id] = i
  176. return i, nil
  177. }
  178. func (m *meter) Int64Gauge(name string, options ...metric.Int64GaugeOption) (metric.Int64Gauge, error) {
  179. m.mtx.Lock()
  180. defer m.mtx.Unlock()
  181. if m.delegate != nil {
  182. return m.delegate.Int64Gauge(name, options...)
  183. }
  184. cfg := metric.NewInt64GaugeConfig(options...)
  185. id := instID{
  186. name: name,
  187. kind: reflect.TypeOf((*siGauge)(nil)),
  188. description: cfg.Description(),
  189. unit: cfg.Unit(),
  190. }
  191. if f, ok := m.instruments[id]; ok {
  192. return f.(metric.Int64Gauge), nil
  193. }
  194. i := &siGauge{name: name, opts: options}
  195. m.instruments[id] = i
  196. return i, nil
  197. }
  198. func (m *meter) Int64ObservableCounter(name string, options ...metric.Int64ObservableCounterOption) (metric.Int64ObservableCounter, error) {
  199. m.mtx.Lock()
  200. defer m.mtx.Unlock()
  201. if m.delegate != nil {
  202. return m.delegate.Int64ObservableCounter(name, options...)
  203. }
  204. cfg := metric.NewInt64ObservableCounterConfig(options...)
  205. id := instID{
  206. name: name,
  207. kind: reflect.TypeOf((*aiCounter)(nil)),
  208. description: cfg.Description(),
  209. unit: cfg.Unit(),
  210. }
  211. if f, ok := m.instruments[id]; ok {
  212. return f.(metric.Int64ObservableCounter), nil
  213. }
  214. i := &aiCounter{name: name, opts: options}
  215. m.instruments[id] = i
  216. return i, nil
  217. }
  218. func (m *meter) Int64ObservableUpDownCounter(name string, options ...metric.Int64ObservableUpDownCounterOption) (metric.Int64ObservableUpDownCounter, error) {
  219. m.mtx.Lock()
  220. defer m.mtx.Unlock()
  221. if m.delegate != nil {
  222. return m.delegate.Int64ObservableUpDownCounter(name, options...)
  223. }
  224. cfg := metric.NewInt64ObservableUpDownCounterConfig(options...)
  225. id := instID{
  226. name: name,
  227. kind: reflect.TypeOf((*aiUpDownCounter)(nil)),
  228. description: cfg.Description(),
  229. unit: cfg.Unit(),
  230. }
  231. if f, ok := m.instruments[id]; ok {
  232. return f.(metric.Int64ObservableUpDownCounter), nil
  233. }
  234. i := &aiUpDownCounter{name: name, opts: options}
  235. m.instruments[id] = i
  236. return i, nil
  237. }
  238. func (m *meter) Int64ObservableGauge(name string, options ...metric.Int64ObservableGaugeOption) (metric.Int64ObservableGauge, error) {
  239. m.mtx.Lock()
  240. defer m.mtx.Unlock()
  241. if m.delegate != nil {
  242. return m.delegate.Int64ObservableGauge(name, options...)
  243. }
  244. cfg := metric.NewInt64ObservableGaugeConfig(options...)
  245. id := instID{
  246. name: name,
  247. kind: reflect.TypeOf((*aiGauge)(nil)),
  248. description: cfg.Description(),
  249. unit: cfg.Unit(),
  250. }
  251. if f, ok := m.instruments[id]; ok {
  252. return f.(metric.Int64ObservableGauge), nil
  253. }
  254. i := &aiGauge{name: name, opts: options}
  255. m.instruments[id] = i
  256. return i, nil
  257. }
  258. func (m *meter) Float64Counter(name string, options ...metric.Float64CounterOption) (metric.Float64Counter, error) {
  259. m.mtx.Lock()
  260. defer m.mtx.Unlock()
  261. if m.delegate != nil {
  262. return m.delegate.Float64Counter(name, options...)
  263. }
  264. cfg := metric.NewFloat64CounterConfig(options...)
  265. id := instID{
  266. name: name,
  267. kind: reflect.TypeOf((*sfCounter)(nil)),
  268. description: cfg.Description(),
  269. unit: cfg.Unit(),
  270. }
  271. if f, ok := m.instruments[id]; ok {
  272. return f.(metric.Float64Counter), nil
  273. }
  274. i := &sfCounter{name: name, opts: options}
  275. m.instruments[id] = i
  276. return i, nil
  277. }
  278. func (m *meter) Float64UpDownCounter(name string, options ...metric.Float64UpDownCounterOption) (metric.Float64UpDownCounter, error) {
  279. m.mtx.Lock()
  280. defer m.mtx.Unlock()
  281. if m.delegate != nil {
  282. return m.delegate.Float64UpDownCounter(name, options...)
  283. }
  284. cfg := metric.NewFloat64UpDownCounterConfig(options...)
  285. id := instID{
  286. name: name,
  287. kind: reflect.TypeOf((*sfUpDownCounter)(nil)),
  288. description: cfg.Description(),
  289. unit: cfg.Unit(),
  290. }
  291. if f, ok := m.instruments[id]; ok {
  292. return f.(metric.Float64UpDownCounter), nil
  293. }
  294. i := &sfUpDownCounter{name: name, opts: options}
  295. m.instruments[id] = i
  296. return i, nil
  297. }
  298. func (m *meter) Float64Histogram(name string, options ...metric.Float64HistogramOption) (metric.Float64Histogram, error) {
  299. m.mtx.Lock()
  300. defer m.mtx.Unlock()
  301. if m.delegate != nil {
  302. return m.delegate.Float64Histogram(name, options...)
  303. }
  304. cfg := metric.NewFloat64HistogramConfig(options...)
  305. id := instID{
  306. name: name,
  307. kind: reflect.TypeOf((*sfHistogram)(nil)),
  308. description: cfg.Description(),
  309. unit: cfg.Unit(),
  310. }
  311. if f, ok := m.instruments[id]; ok {
  312. return f.(metric.Float64Histogram), nil
  313. }
  314. i := &sfHistogram{name: name, opts: options}
  315. m.instruments[id] = i
  316. return i, nil
  317. }
  318. func (m *meter) Float64Gauge(name string, options ...metric.Float64GaugeOption) (metric.Float64Gauge, error) {
  319. m.mtx.Lock()
  320. defer m.mtx.Unlock()
  321. if m.delegate != nil {
  322. return m.delegate.Float64Gauge(name, options...)
  323. }
  324. cfg := metric.NewFloat64GaugeConfig(options...)
  325. id := instID{
  326. name: name,
  327. kind: reflect.TypeOf((*sfGauge)(nil)),
  328. description: cfg.Description(),
  329. unit: cfg.Unit(),
  330. }
  331. if f, ok := m.instruments[id]; ok {
  332. return f.(metric.Float64Gauge), nil
  333. }
  334. i := &sfGauge{name: name, opts: options}
  335. m.instruments[id] = i
  336. return i, nil
  337. }
  338. func (m *meter) Float64ObservableCounter(name string, options ...metric.Float64ObservableCounterOption) (metric.Float64ObservableCounter, error) {
  339. m.mtx.Lock()
  340. defer m.mtx.Unlock()
  341. if m.delegate != nil {
  342. return m.delegate.Float64ObservableCounter(name, options...)
  343. }
  344. cfg := metric.NewFloat64ObservableCounterConfig(options...)
  345. id := instID{
  346. name: name,
  347. kind: reflect.TypeOf((*afCounter)(nil)),
  348. description: cfg.Description(),
  349. unit: cfg.Unit(),
  350. }
  351. if f, ok := m.instruments[id]; ok {
  352. return f.(metric.Float64ObservableCounter), nil
  353. }
  354. i := &afCounter{name: name, opts: options}
  355. m.instruments[id] = i
  356. return i, nil
  357. }
  358. func (m *meter) Float64ObservableUpDownCounter(name string, options ...metric.Float64ObservableUpDownCounterOption) (metric.Float64ObservableUpDownCounter, error) {
  359. m.mtx.Lock()
  360. defer m.mtx.Unlock()
  361. if m.delegate != nil {
  362. return m.delegate.Float64ObservableUpDownCounter(name, options...)
  363. }
  364. cfg := metric.NewFloat64ObservableUpDownCounterConfig(options...)
  365. id := instID{
  366. name: name,
  367. kind: reflect.TypeOf((*afUpDownCounter)(nil)),
  368. description: cfg.Description(),
  369. unit: cfg.Unit(),
  370. }
  371. if f, ok := m.instruments[id]; ok {
  372. return f.(metric.Float64ObservableUpDownCounter), nil
  373. }
  374. i := &afUpDownCounter{name: name, opts: options}
  375. m.instruments[id] = i
  376. return i, nil
  377. }
  378. func (m *meter) Float64ObservableGauge(name string, options ...metric.Float64ObservableGaugeOption) (metric.Float64ObservableGauge, error) {
  379. m.mtx.Lock()
  380. defer m.mtx.Unlock()
  381. if m.delegate != nil {
  382. return m.delegate.Float64ObservableGauge(name, options...)
  383. }
  384. cfg := metric.NewFloat64ObservableGaugeConfig(options...)
  385. id := instID{
  386. name: name,
  387. kind: reflect.TypeOf((*afGauge)(nil)),
  388. description: cfg.Description(),
  389. unit: cfg.Unit(),
  390. }
  391. if f, ok := m.instruments[id]; ok {
  392. return f.(metric.Float64ObservableGauge), nil
  393. }
  394. i := &afGauge{name: name, opts: options}
  395. m.instruments[id] = i
  396. return i, nil
  397. }
  398. // RegisterCallback captures the function that will be called during Collect.
  399. func (m *meter) RegisterCallback(f metric.Callback, insts ...metric.Observable) (metric.Registration, error) {
  400. m.mtx.Lock()
  401. defer m.mtx.Unlock()
  402. if m.delegate != nil {
  403. return m.delegate.RegisterCallback(unwrapCallback(f), unwrapInstruments(insts)...)
  404. }
  405. reg := &registration{instruments: insts, function: f}
  406. e := m.registry.PushBack(reg)
  407. reg.unreg = func() error {
  408. m.mtx.Lock()
  409. _ = m.registry.Remove(e)
  410. m.mtx.Unlock()
  411. return nil
  412. }
  413. return reg, nil
  414. }
  415. func unwrapInstruments(instruments []metric.Observable) []metric.Observable {
  416. out := make([]metric.Observable, 0, len(instruments))
  417. for _, inst := range instruments {
  418. if in, ok := inst.(unwrapper); ok {
  419. out = append(out, in.unwrap())
  420. } else {
  421. out = append(out, inst)
  422. }
  423. }
  424. return out
  425. }
  426. type registration struct {
  427. embedded.Registration
  428. instruments []metric.Observable
  429. function metric.Callback
  430. unreg func() error
  431. unregMu sync.Mutex
  432. }
  433. type unwrapObs struct {
  434. embedded.Observer
  435. obs metric.Observer
  436. }
  437. // unwrapFloat64Observable returns an expected metric.Float64Observable after
  438. // unwrapping the global object.
  439. func unwrapFloat64Observable(inst metric.Float64Observable) metric.Float64Observable {
  440. if unwrapped, ok := inst.(unwrapper); ok {
  441. if floatObs, ok := unwrapped.unwrap().(metric.Float64Observable); ok {
  442. // Note: if the unwrapped object does not
  443. // unwrap as an observable for either of the
  444. // predicates here, it means an internal bug in
  445. // this package. We avoid logging an error in
  446. // this case, because the SDK has to try its
  447. // own type conversion on the object. The SDK
  448. // will see this and be forced to respond with
  449. // its own error.
  450. //
  451. // This code uses a double-nested if statement
  452. // to avoid creating a branch that is
  453. // impossible to cover.
  454. inst = floatObs
  455. }
  456. }
  457. return inst
  458. }
  459. // unwrapInt64Observable returns an expected metric.Int64Observable after
  460. // unwrapping the global object.
  461. func unwrapInt64Observable(inst metric.Int64Observable) metric.Int64Observable {
  462. if unwrapped, ok := inst.(unwrapper); ok {
  463. if unint, ok := unwrapped.unwrap().(metric.Int64Observable); ok {
  464. // See the comment in unwrapFloat64Observable().
  465. inst = unint
  466. }
  467. }
  468. return inst
  469. }
  470. func (uo *unwrapObs) ObserveFloat64(inst metric.Float64Observable, value float64, opts ...metric.ObserveOption) {
  471. uo.obs.ObserveFloat64(unwrapFloat64Observable(inst), value, opts...)
  472. }
  473. func (uo *unwrapObs) ObserveInt64(inst metric.Int64Observable, value int64, opts ...metric.ObserveOption) {
  474. uo.obs.ObserveInt64(unwrapInt64Observable(inst), value, opts...)
  475. }
  476. func unwrapCallback(f metric.Callback) metric.Callback {
  477. return func(ctx context.Context, obs metric.Observer) error {
  478. return f(ctx, &unwrapObs{obs: obs})
  479. }
  480. }
  481. func (c *registration) setDelegate(m metric.Meter) {
  482. c.unregMu.Lock()
  483. defer c.unregMu.Unlock()
  484. if c.unreg == nil {
  485. // Unregister already called.
  486. return
  487. }
  488. reg, err := m.RegisterCallback(unwrapCallback(c.function), unwrapInstruments(c.instruments)...)
  489. if err != nil {
  490. GetErrorHandler().Handle(err)
  491. return
  492. }
  493. c.unreg = reg.Unregister
  494. }
  495. func (c *registration) Unregister() error {
  496. c.unregMu.Lock()
  497. defer c.unregMu.Unlock()
  498. if c.unreg == nil {
  499. // Unregister already called.
  500. return nil
  501. }
  502. var err error
  503. err, c.unreg = c.unreg(), nil
  504. return err
  505. }