index.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import round from 'lodash/round';
  2. import {t} from 'sentry/locale';
  3. import {SeriesApi} from 'sentry/types';
  4. import {SamplingInnerName, SamplingRule, SamplingRuleType} from 'sentry/types/sampling';
  5. import {defined} from 'sentry/utils';
  6. import {projectStatsToSampleRates} from './projectStatsToSampleRates';
  7. export const SERVER_SIDE_SAMPLING_DOC_LINK =
  8. 'https://docs.sentry.io/product/data-management-settings/dynamic-sampling/';
  9. export function getInnerNameLabel(name: SamplingInnerName) {
  10. switch (name) {
  11. case SamplingInnerName.TRACE_ENVIRONMENT:
  12. return t('Environment');
  13. case SamplingInnerName.TRACE_RELEASE:
  14. return t('Release');
  15. default:
  16. return '';
  17. }
  18. }
  19. export const quantityField = 'sum(quantity)';
  20. export function isUniformRule(rule?: SamplingRule) {
  21. if (!rule) {
  22. return false;
  23. }
  24. return rule.type === SamplingRuleType.TRACE && rule.condition.inner.length === 0;
  25. }
  26. export function isValidSampleRate(sampleRate: number | undefined) {
  27. if (!defined(sampleRate)) {
  28. return false;
  29. }
  30. return !isNaN(sampleRate) && sampleRate <= 1 && sampleRate >= 0;
  31. }
  32. export function rateToPercentage(rate: number | undefined, decimalPlaces: number = 2) {
  33. if (!defined(rate)) {
  34. return rate;
  35. }
  36. return round(rate * 100, decimalPlaces);
  37. }
  38. export function percentageToRate(rate: number | undefined, decimalPlaces: number = 4) {
  39. if (!defined(rate)) {
  40. return rate;
  41. }
  42. return round(rate / 100, decimalPlaces);
  43. }
  44. export function getClientSampleRates(
  45. projectStats: SeriesApi | undefined,
  46. specifiedClientRate?: number
  47. ) {
  48. const {trueSampleRate, maxSafeSampleRate} = projectStatsToSampleRates(projectStats);
  49. const current =
  50. defined(specifiedClientRate) && !isNaN(specifiedClientRate)
  51. ? specifiedClientRate
  52. : defined(trueSampleRate) && !isNaN(trueSampleRate)
  53. ? trueSampleRate
  54. : undefined;
  55. const recommended =
  56. defined(maxSafeSampleRate) && !isNaN(maxSafeSampleRate)
  57. ? maxSafeSampleRate
  58. : undefined;
  59. let diff: number | undefined = undefined;
  60. if (defined(recommended) && defined(current)) {
  61. if (recommended >= current) {
  62. diff = ((recommended - current) / current) * 100;
  63. } else {
  64. diff = ((current - recommended) / recommended) * 100;
  65. }
  66. }
  67. return {
  68. current,
  69. recommended,
  70. diff,
  71. };
  72. }