meter.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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(
  139. name string,
  140. options ...metric.Int64UpDownCounterOption,
  141. ) (metric.Int64UpDownCounter, error) {
  142. m.mtx.Lock()
  143. defer m.mtx.Unlock()
  144. if m.delegate != nil {
  145. return m.delegate.Int64UpDownCounter(name, options...)
  146. }
  147. cfg := metric.NewInt64UpDownCounterConfig(options...)
  148. id := instID{
  149. name: name,
  150. kind: reflect.TypeOf((*siUpDownCounter)(nil)),
  151. description: cfg.Description(),
  152. unit: cfg.Unit(),
  153. }
  154. if f, ok := m.instruments[id]; ok {
  155. return f.(metric.Int64UpDownCounter), nil
  156. }
  157. i := &siUpDownCounter{name: name, opts: options}
  158. m.instruments[id] = i
  159. return i, nil
  160. }
  161. func (m *meter) Int64Histogram(name string, options ...metric.Int64HistogramOption) (metric.Int64Histogram, error) {
  162. m.mtx.Lock()
  163. defer m.mtx.Unlock()
  164. if m.delegate != nil {
  165. return m.delegate.Int64Histogram(name, options...)
  166. }
  167. cfg := metric.NewInt64HistogramConfig(options...)
  168. id := instID{
  169. name: name,
  170. kind: reflect.TypeOf((*siHistogram)(nil)),
  171. description: cfg.Description(),
  172. unit: cfg.Unit(),
  173. }
  174. if f, ok := m.instruments[id]; ok {
  175. return f.(metric.Int64Histogram), nil
  176. }
  177. i := &siHistogram{name: name, opts: options}
  178. m.instruments[id] = i
  179. return i, nil
  180. }
  181. func (m *meter) Int64Gauge(name string, options ...metric.Int64GaugeOption) (metric.Int64Gauge, error) {
  182. m.mtx.Lock()
  183. defer m.mtx.Unlock()
  184. if m.delegate != nil {
  185. return m.delegate.Int64Gauge(name, options...)
  186. }
  187. cfg := metric.NewInt64GaugeConfig(options...)
  188. id := instID{
  189. name: name,
  190. kind: reflect.TypeOf((*siGauge)(nil)),
  191. description: cfg.Description(),
  192. unit: cfg.Unit(),
  193. }
  194. if f, ok := m.instruments[id]; ok {
  195. return f.(metric.Int64Gauge), nil
  196. }
  197. i := &siGauge{name: name, opts: options}
  198. m.instruments[id] = i
  199. return i, nil
  200. }
  201. func (m *meter) Int64ObservableCounter(
  202. name string,
  203. options ...metric.Int64ObservableCounterOption,
  204. ) (metric.Int64ObservableCounter, error) {
  205. m.mtx.Lock()
  206. defer m.mtx.Unlock()
  207. if m.delegate != nil {
  208. return m.delegate.Int64ObservableCounter(name, options...)
  209. }
  210. cfg := metric.NewInt64ObservableCounterConfig(options...)
  211. id := instID{
  212. name: name,
  213. kind: reflect.TypeOf((*aiCounter)(nil)),
  214. description: cfg.Description(),
  215. unit: cfg.Unit(),
  216. }
  217. if f, ok := m.instruments[id]; ok {
  218. return f.(metric.Int64ObservableCounter), nil
  219. }
  220. i := &aiCounter{name: name, opts: options}
  221. m.instruments[id] = i
  222. return i, nil
  223. }
  224. func (m *meter) Int64ObservableUpDownCounter(
  225. name string,
  226. options ...metric.Int64ObservableUpDownCounterOption,
  227. ) (metric.Int64ObservableUpDownCounter, error) {
  228. m.mtx.Lock()
  229. defer m.mtx.Unlock()
  230. if m.delegate != nil {
  231. return m.delegate.Int64ObservableUpDownCounter(name, options...)
  232. }
  233. cfg := metric.NewInt64ObservableUpDownCounterConfig(options...)
  234. id := instID{
  235. name: name,
  236. kind: reflect.TypeOf((*aiUpDownCounter)(nil)),
  237. description: cfg.Description(),
  238. unit: cfg.Unit(),
  239. }
  240. if f, ok := m.instruments[id]; ok {
  241. return f.(metric.Int64ObservableUpDownCounter), nil
  242. }
  243. i := &aiUpDownCounter{name: name, opts: options}
  244. m.instruments[id] = i
  245. return i, nil
  246. }
  247. func (m *meter) Int64ObservableGauge(
  248. name string,
  249. options ...metric.Int64ObservableGaugeOption,
  250. ) (metric.Int64ObservableGauge, error) {
  251. m.mtx.Lock()
  252. defer m.mtx.Unlock()
  253. if m.delegate != nil {
  254. return m.delegate.Int64ObservableGauge(name, options...)
  255. }
  256. cfg := metric.NewInt64ObservableGaugeConfig(options...)
  257. id := instID{
  258. name: name,
  259. kind: reflect.TypeOf((*aiGauge)(nil)),
  260. description: cfg.Description(),
  261. unit: cfg.Unit(),
  262. }
  263. if f, ok := m.instruments[id]; ok {
  264. return f.(metric.Int64ObservableGauge), nil
  265. }
  266. i := &aiGauge{name: name, opts: options}
  267. m.instruments[id] = i
  268. return i, nil
  269. }
  270. func (m *meter) Float64Counter(name string, options ...metric.Float64CounterOption) (metric.Float64Counter, error) {
  271. m.mtx.Lock()
  272. defer m.mtx.Unlock()
  273. if m.delegate != nil {
  274. return m.delegate.Float64Counter(name, options...)
  275. }
  276. cfg := metric.NewFloat64CounterConfig(options...)
  277. id := instID{
  278. name: name,
  279. kind: reflect.TypeOf((*sfCounter)(nil)),
  280. description: cfg.Description(),
  281. unit: cfg.Unit(),
  282. }
  283. if f, ok := m.instruments[id]; ok {
  284. return f.(metric.Float64Counter), nil
  285. }
  286. i := &sfCounter{name: name, opts: options}
  287. m.instruments[id] = i
  288. return i, nil
  289. }
  290. func (m *meter) Float64UpDownCounter(
  291. name string,
  292. options ...metric.Float64UpDownCounterOption,
  293. ) (metric.Float64UpDownCounter, error) {
  294. m.mtx.Lock()
  295. defer m.mtx.Unlock()
  296. if m.delegate != nil {
  297. return m.delegate.Float64UpDownCounter(name, options...)
  298. }
  299. cfg := metric.NewFloat64UpDownCounterConfig(options...)
  300. id := instID{
  301. name: name,
  302. kind: reflect.TypeOf((*sfUpDownCounter)(nil)),
  303. description: cfg.Description(),
  304. unit: cfg.Unit(),
  305. }
  306. if f, ok := m.instruments[id]; ok {
  307. return f.(metric.Float64UpDownCounter), nil
  308. }
  309. i := &sfUpDownCounter{name: name, opts: options}
  310. m.instruments[id] = i
  311. return i, nil
  312. }
  313. func (m *meter) Float64Histogram(
  314. name string,
  315. options ...metric.Float64HistogramOption,
  316. ) (metric.Float64Histogram, error) {
  317. m.mtx.Lock()
  318. defer m.mtx.Unlock()
  319. if m.delegate != nil {
  320. return m.delegate.Float64Histogram(name, options...)
  321. }
  322. cfg := metric.NewFloat64HistogramConfig(options...)
  323. id := instID{
  324. name: name,
  325. kind: reflect.TypeOf((*sfHistogram)(nil)),
  326. description: cfg.Description(),
  327. unit: cfg.Unit(),
  328. }
  329. if f, ok := m.instruments[id]; ok {
  330. return f.(metric.Float64Histogram), nil
  331. }
  332. i := &sfHistogram{name: name, opts: options}
  333. m.instruments[id] = i
  334. return i, nil
  335. }
  336. func (m *meter) Float64Gauge(name string, options ...metric.Float64GaugeOption) (metric.Float64Gauge, error) {
  337. m.mtx.Lock()
  338. defer m.mtx.Unlock()
  339. if m.delegate != nil {
  340. return m.delegate.Float64Gauge(name, options...)
  341. }
  342. cfg := metric.NewFloat64GaugeConfig(options...)
  343. id := instID{
  344. name: name,
  345. kind: reflect.TypeOf((*sfGauge)(nil)),
  346. description: cfg.Description(),
  347. unit: cfg.Unit(),
  348. }
  349. if f, ok := m.instruments[id]; ok {
  350. return f.(metric.Float64Gauge), nil
  351. }
  352. i := &sfGauge{name: name, opts: options}
  353. m.instruments[id] = i
  354. return i, nil
  355. }
  356. func (m *meter) Float64ObservableCounter(
  357. name string,
  358. options ...metric.Float64ObservableCounterOption,
  359. ) (metric.Float64ObservableCounter, error) {
  360. m.mtx.Lock()
  361. defer m.mtx.Unlock()
  362. if m.delegate != nil {
  363. return m.delegate.Float64ObservableCounter(name, options...)
  364. }
  365. cfg := metric.NewFloat64ObservableCounterConfig(options...)
  366. id := instID{
  367. name: name,
  368. kind: reflect.TypeOf((*afCounter)(nil)),
  369. description: cfg.Description(),
  370. unit: cfg.Unit(),
  371. }
  372. if f, ok := m.instruments[id]; ok {
  373. return f.(metric.Float64ObservableCounter), nil
  374. }
  375. i := &afCounter{name: name, opts: options}
  376. m.instruments[id] = i
  377. return i, nil
  378. }
  379. func (m *meter) Float64ObservableUpDownCounter(
  380. name string,
  381. options ...metric.Float64ObservableUpDownCounterOption,
  382. ) (metric.Float64ObservableUpDownCounter, error) {
  383. m.mtx.Lock()
  384. defer m.mtx.Unlock()
  385. if m.delegate != nil {
  386. return m.delegate.Float64ObservableUpDownCounter(name, options...)
  387. }
  388. cfg := metric.NewFloat64ObservableUpDownCounterConfig(options...)
  389. id := instID{
  390. name: name,
  391. kind: reflect.TypeOf((*afUpDownCounter)(nil)),
  392. description: cfg.Description(),
  393. unit: cfg.Unit(),
  394. }
  395. if f, ok := m.instruments[id]; ok {
  396. return f.(metric.Float64ObservableUpDownCounter), nil
  397. }
  398. i := &afUpDownCounter{name: name, opts: options}
  399. m.instruments[id] = i
  400. return i, nil
  401. }
  402. func (m *meter) Float64ObservableGauge(
  403. name string,
  404. options ...metric.Float64ObservableGaugeOption,
  405. ) (metric.Float64ObservableGauge, error) {
  406. m.mtx.Lock()
  407. defer m.mtx.Unlock()
  408. if m.delegate != nil {
  409. return m.delegate.Float64ObservableGauge(name, options...)
  410. }
  411. cfg := metric.NewFloat64ObservableGaugeConfig(options...)
  412. id := instID{
  413. name: name,
  414. kind: reflect.TypeOf((*afGauge)(nil)),
  415. description: cfg.Description(),
  416. unit: cfg.Unit(),
  417. }
  418. if f, ok := m.instruments[id]; ok {
  419. return f.(metric.Float64ObservableGauge), nil
  420. }
  421. i := &afGauge{name: name, opts: options}
  422. m.instruments[id] = i
  423. return i, nil
  424. }
  425. // RegisterCallback captures the function that will be called during Collect.
  426. func (m *meter) RegisterCallback(f metric.Callback, insts ...metric.Observable) (metric.Registration, error) {
  427. m.mtx.Lock()
  428. defer m.mtx.Unlock()
  429. if m.delegate != nil {
  430. return m.delegate.RegisterCallback(unwrapCallback(f), unwrapInstruments(insts)...)
  431. }
  432. reg := &registration{instruments: insts, function: f}
  433. e := m.registry.PushBack(reg)
  434. reg.unreg = func() error {
  435. m.mtx.Lock()
  436. _ = m.registry.Remove(e)
  437. m.mtx.Unlock()
  438. return nil
  439. }
  440. return reg, nil
  441. }
  442. func unwrapInstruments(instruments []metric.Observable) []metric.Observable {
  443. out := make([]metric.Observable, 0, len(instruments))
  444. for _, inst := range instruments {
  445. if in, ok := inst.(unwrapper); ok {
  446. out = append(out, in.unwrap())
  447. } else {
  448. out = append(out, inst)
  449. }
  450. }
  451. return out
  452. }
  453. type registration struct {
  454. embedded.Registration
  455. instruments []metric.Observable
  456. function metric.Callback
  457. unreg func() error
  458. unregMu sync.Mutex
  459. }
  460. type unwrapObs struct {
  461. embedded.Observer
  462. obs metric.Observer
  463. }
  464. // unwrapFloat64Observable returns an expected metric.Float64Observable after
  465. // unwrapping the global object.
  466. func unwrapFloat64Observable(inst metric.Float64Observable) metric.Float64Observable {
  467. if unwrapped, ok := inst.(unwrapper); ok {
  468. if floatObs, ok := unwrapped.unwrap().(metric.Float64Observable); ok {
  469. // Note: if the unwrapped object does not
  470. // unwrap as an observable for either of the
  471. // predicates here, it means an internal bug in
  472. // this package. We avoid logging an error in
  473. // this case, because the SDK has to try its
  474. // own type conversion on the object. The SDK
  475. // will see this and be forced to respond with
  476. // its own error.
  477. //
  478. // This code uses a double-nested if statement
  479. // to avoid creating a branch that is
  480. // impossible to cover.
  481. inst = floatObs
  482. }
  483. }
  484. return inst
  485. }
  486. // unwrapInt64Observable returns an expected metric.Int64Observable after
  487. // unwrapping the global object.
  488. func unwrapInt64Observable(inst metric.Int64Observable) metric.Int64Observable {
  489. if unwrapped, ok := inst.(unwrapper); ok {
  490. if unint, ok := unwrapped.unwrap().(metric.Int64Observable); ok {
  491. // See the comment in unwrapFloat64Observable().
  492. inst = unint
  493. }
  494. }
  495. return inst
  496. }
  497. func (uo *unwrapObs) ObserveFloat64(inst metric.Float64Observable, value float64, opts ...metric.ObserveOption) {
  498. uo.obs.ObserveFloat64(unwrapFloat64Observable(inst), value, opts...)
  499. }
  500. func (uo *unwrapObs) ObserveInt64(inst metric.Int64Observable, value int64, opts ...metric.ObserveOption) {
  501. uo.obs.ObserveInt64(unwrapInt64Observable(inst), value, opts...)
  502. }
  503. func unwrapCallback(f metric.Callback) metric.Callback {
  504. return func(ctx context.Context, obs metric.Observer) error {
  505. return f(ctx, &unwrapObs{obs: obs})
  506. }
  507. }
  508. func (c *registration) setDelegate(m metric.Meter) {
  509. c.unregMu.Lock()
  510. defer c.unregMu.Unlock()
  511. if c.unreg == nil {
  512. // Unregister already called.
  513. return
  514. }
  515. reg, err := m.RegisterCallback(unwrapCallback(c.function), unwrapInstruments(c.instruments)...)
  516. if err != nil {
  517. GetErrorHandler().Handle(err)
  518. return
  519. }
  520. c.unreg = reg.Unregister
  521. }
  522. func (c *registration) Unregister() error {
  523. c.unregMu.Lock()
  524. defer c.unregMu.Unlock()
  525. if c.unreg == nil {
  526. // Unregister already called.
  527. return nil
  528. }
  529. var err error
  530. err, c.unreg = c.unreg(), nil
  531. return err
  532. }