Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <cstdint>
#include <string_view>
 
namespace onnxruntime::telemetry_internal {
 
// Keep independent rates so high-volume, model-session, and process events can be tuned separately.
// 1DS popSample is metadata only; ShouldSampleSession performs the actual client-side sampling.
inline constexpr double kModelSessionSampleRatePercent = 1.0;
inline constexpr double kHighVolumeEventSampleRatePercent = 1.0;
inline constexpr double kOtherProcessEventSampleRatePercent = 1.0;
 
static_assert(kModelSessionSampleRatePercent >= 0.0 &&
              kModelSessionSampleRatePercent <= 100.0);
static_assert(kHighVolumeEventSampleRatePercent >= 0.0 &&
              kHighVolumeEventSampleRatePercent <= 100.0);
static_assert(kOtherProcessEventSampleRatePercent >= 0.0 &&
              kOtherProcessEventSampleRatePercent <= 100.0);
 
inline uint64_t HashSamplingKey(std::string_view app_session_guid, uint32_t session_id) {
  uint64_t hash = 14695981039346656037ULL;
  for (const unsigned char c : app_session_guid) {
    hash ^= c;
    hash *= 1099511628211ULL;
  }
  for (unsigned int shift = 0; shift < 32; shift += 8) {
    hash ^= static_cast<unsigned char>(session_id >> shift);
    hash *= 1099511628211ULL;
  }
  return hash;
}
 
inline bool ShouldSampleSession(std::string_view app_session_guid, uint32_t session_id,
                                double sample_rate_percent = kModelSessionSampleRatePercent) {
  if (!(sample_rate_percent > 0.0)) {
    return false;
  }
  if (sample_rate_percent >= 100.0) {
    return true;
  }
 
  // One million buckets support rates down to 0.0001% while keeping the decision stable for every
  // event in a model session. The random process GUID prevents sequential session IDs from biasing
  // the sample across processes.
  constexpr uint64_t kBucketCount = 1'000'000;
  const auto threshold = static_cast<uint64_t>(
      static_cast<long double>(sample_rate_percent) * kBucketCount / 100.0L);
  return HashSamplingKey(app_session_guid, session_id) % kBucketCount < threshold;
}
 
}  // namespace onnxruntime::telemetry_internal