metrics.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { NOOP_METER_PROVIDER } from '../metrics/NoopMeterProvider';
  17. import { getGlobal, registerGlobal, unregisterGlobal, } from '../internal/global-utils';
  18. import { DiagAPI } from './diag';
  19. var API_NAME = 'metrics';
  20. /**
  21. * Singleton object which represents the entry point to the OpenTelemetry Metrics API
  22. */
  23. var MetricsAPI = /** @class */ (function () {
  24. /** Empty private constructor prevents end users from constructing a new instance of the API */
  25. function MetricsAPI() {
  26. }
  27. /** Get the singleton instance of the Metrics API */
  28. MetricsAPI.getInstance = function () {
  29. if (!this._instance) {
  30. this._instance = new MetricsAPI();
  31. }
  32. return this._instance;
  33. };
  34. /**
  35. * Set the current global meter provider.
  36. * Returns true if the meter provider was successfully registered, else false.
  37. */
  38. MetricsAPI.prototype.setGlobalMeterProvider = function (provider) {
  39. return registerGlobal(API_NAME, provider, DiagAPI.instance());
  40. };
  41. /**
  42. * Returns the global meter provider.
  43. */
  44. MetricsAPI.prototype.getMeterProvider = function () {
  45. return getGlobal(API_NAME) || NOOP_METER_PROVIDER;
  46. };
  47. /**
  48. * Returns a meter from the global meter provider.
  49. */
  50. MetricsAPI.prototype.getMeter = function (name, version, options) {
  51. return this.getMeterProvider().getMeter(name, version, options);
  52. };
  53. /** Remove the global meter provider */
  54. MetricsAPI.prototype.disable = function () {
  55. unregisterGlobal(API_NAME, DiagAPI.instance());
  56. };
  57. return MetricsAPI;
  58. }());
  59. export { MetricsAPI };
  60. //# sourceMappingURL=metrics.js.map