prometheus-client-mmap 0.20.3-x86_64-linux

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +253 -0
  3. data/ext/fast_mmaped_file/extconf.rb +30 -0
  4. data/ext/fast_mmaped_file/fast_mmaped_file.c +122 -0
  5. data/ext/fast_mmaped_file/file_format.c +5 -0
  6. data/ext/fast_mmaped_file/file_format.h +11 -0
  7. data/ext/fast_mmaped_file/file_parsing.c +195 -0
  8. data/ext/fast_mmaped_file/file_parsing.h +27 -0
  9. data/ext/fast_mmaped_file/file_reading.c +102 -0
  10. data/ext/fast_mmaped_file/file_reading.h +30 -0
  11. data/ext/fast_mmaped_file/globals.h +14 -0
  12. data/ext/fast_mmaped_file/mmap.c +427 -0
  13. data/ext/fast_mmaped_file/mmap.h +61 -0
  14. data/ext/fast_mmaped_file/rendering.c +199 -0
  15. data/ext/fast_mmaped_file/rendering.h +8 -0
  16. data/ext/fast_mmaped_file/utils.c +56 -0
  17. data/ext/fast_mmaped_file/utils.h +22 -0
  18. data/ext/fast_mmaped_file/value_access.c +242 -0
  19. data/ext/fast_mmaped_file/value_access.h +15 -0
  20. data/ext/fast_mmaped_file_rs/.cargo/config.toml +23 -0
  21. data/ext/fast_mmaped_file_rs/Cargo.lock +790 -0
  22. data/ext/fast_mmaped_file_rs/Cargo.toml +30 -0
  23. data/ext/fast_mmaped_file_rs/README.md +52 -0
  24. data/ext/fast_mmaped_file_rs/extconf.rb +30 -0
  25. data/ext/fast_mmaped_file_rs/src/error.rs +174 -0
  26. data/ext/fast_mmaped_file_rs/src/file_entry.rs +579 -0
  27. data/ext/fast_mmaped_file_rs/src/file_info.rs +190 -0
  28. data/ext/fast_mmaped_file_rs/src/lib.rs +79 -0
  29. data/ext/fast_mmaped_file_rs/src/macros.rs +14 -0
  30. data/ext/fast_mmaped_file_rs/src/map.rs +492 -0
  31. data/ext/fast_mmaped_file_rs/src/mmap.rs +151 -0
  32. data/ext/fast_mmaped_file_rs/src/parser.rs +346 -0
  33. data/ext/fast_mmaped_file_rs/src/raw_entry.rs +473 -0
  34. data/ext/fast_mmaped_file_rs/src/testhelper.rs +222 -0
  35. data/ext/fast_mmaped_file_rs/src/util.rs +121 -0
  36. data/lib/2.7/fast_mmaped_file.so +0 -0
  37. data/lib/2.7/fast_mmaped_file_rs.so +0 -0
  38. data/lib/3.0/fast_mmaped_file.so +0 -0
  39. data/lib/3.0/fast_mmaped_file_rs.so +0 -0
  40. data/lib/3.1/fast_mmaped_file.so +0 -0
  41. data/lib/3.1/fast_mmaped_file_rs.so +0 -0
  42. data/lib/3.2/fast_mmaped_file.so +0 -0
  43. data/lib/3.2/fast_mmaped_file_rs.so +0 -0
  44. data/lib/prometheus/client/configuration.rb +23 -0
  45. data/lib/prometheus/client/counter.rb +27 -0
  46. data/lib/prometheus/client/formats/text.rb +118 -0
  47. data/lib/prometheus/client/gauge.rb +40 -0
  48. data/lib/prometheus/client/helper/entry_parser.rb +132 -0
  49. data/lib/prometheus/client/helper/file_locker.rb +50 -0
  50. data/lib/prometheus/client/helper/json_parser.rb +23 -0
  51. data/lib/prometheus/client/helper/metrics_processing.rb +45 -0
  52. data/lib/prometheus/client/helper/metrics_representation.rb +51 -0
  53. data/lib/prometheus/client/helper/mmaped_file.rb +64 -0
  54. data/lib/prometheus/client/helper/plain_file.rb +29 -0
  55. data/lib/prometheus/client/histogram.rb +80 -0
  56. data/lib/prometheus/client/label_set_validator.rb +86 -0
  57. data/lib/prometheus/client/metric.rb +80 -0
  58. data/lib/prometheus/client/mmaped_dict.rb +79 -0
  59. data/lib/prometheus/client/mmaped_value.rb +154 -0
  60. data/lib/prometheus/client/page_size.rb +17 -0
  61. data/lib/prometheus/client/push.rb +203 -0
  62. data/lib/prometheus/client/rack/collector.rb +88 -0
  63. data/lib/prometheus/client/rack/exporter.rb +96 -0
  64. data/lib/prometheus/client/registry.rb +65 -0
  65. data/lib/prometheus/client/simple_value.rb +31 -0
  66. data/lib/prometheus/client/summary.rb +69 -0
  67. data/lib/prometheus/client/support/unicorn.rb +35 -0
  68. data/lib/prometheus/client/uses_value_type.rb +20 -0
  69. data/lib/prometheus/client/version.rb +5 -0
  70. data/lib/prometheus/client.rb +58 -0
  71. data/lib/prometheus.rb +3 -0
  72. data/vendor/c/hashmap/.gitignore +52 -0
  73. data/vendor/c/hashmap/LICENSE +21 -0
  74. data/vendor/c/hashmap/README.md +90 -0
  75. data/vendor/c/hashmap/_config.yml +1 -0
  76. data/vendor/c/hashmap/src/hashmap.c +692 -0
  77. data/vendor/c/hashmap/src/hashmap.h +267 -0
  78. data/vendor/c/hashmap/test/Makefile +22 -0
  79. data/vendor/c/hashmap/test/hashmap_test.c +608 -0
  80. data/vendor/c/jsmn/.travis.yml +4 -0
  81. data/vendor/c/jsmn/LICENSE +20 -0
  82. data/vendor/c/jsmn/Makefile +41 -0
  83. data/vendor/c/jsmn/README.md +168 -0
  84. data/vendor/c/jsmn/example/jsondump.c +126 -0
  85. data/vendor/c/jsmn/example/simple.c +76 -0
  86. data/vendor/c/jsmn/jsmn.c +314 -0
  87. data/vendor/c/jsmn/jsmn.h +76 -0
  88. data/vendor/c/jsmn/library.json +16 -0
  89. data/vendor/c/jsmn/test/test.h +27 -0
  90. data/vendor/c/jsmn/test/tests.c +407 -0
  91. data/vendor/c/jsmn/test/testutil.h +94 -0
  92. metadata +243 -0
@@ -0,0 +1,190 @@
1
+ use magnus::exception::*;
2
+ use magnus::{Error, RString, StaticSymbol, Symbol, Value};
3
+ use std::ffi::OsString;
4
+ use std::fs::File;
5
+ use std::io::{self, Read, Seek};
6
+ use std::os::unix::ffi::OsStringExt;
7
+ use std::path::PathBuf;
8
+
9
+ use crate::err;
10
+ use crate::error::{MmapError, RubyError};
11
+ use crate::util;
12
+ use crate::Result;
13
+
14
+ /// The details of a `*.db` file.
15
+ #[derive(Debug)]
16
+ pub struct FileInfo {
17
+ pub file: File,
18
+ pub path: PathBuf,
19
+ pub len: usize,
20
+ pub multiprocess_mode: Symbol,
21
+ pub type_: StaticSymbol,
22
+ pub pid: String,
23
+ }
24
+
25
+ impl FileInfo {
26
+ /// Receive the details of a file from Ruby and store as a `FileInfo`.
27
+ pub fn open_from_params(params: &[Value; 4]) -> magnus::error::Result<Self> {
28
+ if params.len() != 4 {
29
+ return Err(err!(
30
+ arg_error(),
31
+ "wrong number of arguments {} instead of 4",
32
+ params.len()
33
+ ));
34
+ }
35
+
36
+ let filepath = RString::from_value(params[0])
37
+ .ok_or_else(|| err!(arg_error(), "can't convert filepath to String"))?;
38
+
39
+ // SAFETY: We immediately copy the string buffer from Ruby, preventing
40
+ // it from being mutated out from under us.
41
+ let path_bytes: Vec<_> = unsafe { filepath.as_slice().to_owned() };
42
+ let path = PathBuf::from(OsString::from_vec(path_bytes));
43
+
44
+ let mut file = File::open(&path).map_err(|_| {
45
+ err!(
46
+ arg_error(),
47
+ "Can't open {}, errno: {}",
48
+ path.display(),
49
+ util::errno()
50
+ )
51
+ })?;
52
+
53
+ let stat = file
54
+ .metadata()
55
+ .map_err(|_| err!(io_error(), "Can't stat file, errno: {}", util::errno()))?;
56
+
57
+ let length = util::cast_chk::<_, usize>(stat.len(), "file size")?;
58
+
59
+ let multiprocess_mode = Symbol::from_value(params[1])
60
+ .ok_or_else(|| err!(arg_error(), "expected multiprocess_mode to be a symbol"))?;
61
+
62
+ let type_ = StaticSymbol::from_value(params[2])
63
+ .ok_or_else(|| err!(arg_error(), "expected file type to be a symbol"))?;
64
+
65
+ let pid = RString::from_value(params[3])
66
+ .ok_or_else(|| err!(arg_error(), "expected pid to be a String"))?;
67
+
68
+ file.rewind()
69
+ .map_err(|_| err!(io_error(), "Can't fseek 0, errno: {}", util::errno()))?;
70
+
71
+ Ok(Self {
72
+ file,
73
+ path,
74
+ len: length,
75
+ multiprocess_mode,
76
+ type_,
77
+ pid: pid.to_string()?,
78
+ })
79
+ }
80
+
81
+ /// Read the contents of the associated file into the buffer provided by
82
+ /// the caller.
83
+ pub fn read_from_file(&mut self, buf: &mut Vec<u8>) -> Result<()> {
84
+ buf.clear();
85
+ buf.try_reserve(self.len).map_err(|_| {
86
+ MmapError::legacy(
87
+ format!("Can't malloc {}, errno: {}", self.len, util::errno()),
88
+ RubyError::Io,
89
+ )
90
+ })?;
91
+
92
+ match self.file.read_to_end(buf) {
93
+ Ok(n) if n == self.len => Ok(()),
94
+ Ok(_) => Err(MmapError::io(
95
+ "read",
96
+ &self.path,
97
+ io::Error::from(io::ErrorKind::UnexpectedEof),
98
+ )),
99
+ Err(e) => Err(MmapError::io("read", &self.path, e)),
100
+ }
101
+ }
102
+ }
103
+
104
+ #[cfg(test)]
105
+ mod test {
106
+ use magnus::{eval, RArray, StaticSymbol, Symbol};
107
+ use rand::{thread_rng, Rng};
108
+ use sha2::{Digest, Sha256};
109
+
110
+ use super::*;
111
+ use crate::testhelper::TestFile;
112
+
113
+ #[test]
114
+ fn test_open_from_params() {
115
+ let _cleanup = unsafe { magnus::embed::init() };
116
+ let ruby = magnus::Ruby::get().unwrap();
117
+ crate::init(&ruby).unwrap();
118
+
119
+ let file_data = b"foobar";
120
+ let TestFile {
121
+ file: _file,
122
+ path,
123
+ dir: _dir,
124
+ } = TestFile::new(file_data);
125
+
126
+ let pid = "worker-1_0";
127
+ let args = RArray::from_value(
128
+ eval(&format!("['{}', :max, :gauge, '{pid}']", path.display())).unwrap(),
129
+ )
130
+ .unwrap();
131
+ let arg0 = args.shift().unwrap();
132
+ let arg1 = args.shift().unwrap();
133
+ let arg2 = args.shift().unwrap();
134
+ let arg3 = args.shift().unwrap();
135
+
136
+ let out = FileInfo::open_from_params(&[arg0, arg1, arg2, arg3]);
137
+ assert!(out.is_ok());
138
+
139
+ let out = out.unwrap();
140
+
141
+ assert_eq!(out.path, path);
142
+ assert_eq!(out.len, file_data.len());
143
+ assert_eq!(out.multiprocess_mode, Symbol::new("max"));
144
+ assert_eq!(out.type_, Symbol::new("gauge"));
145
+ assert_eq!(out.pid, pid);
146
+ }
147
+
148
+ #[test]
149
+ fn test_read_from_file() {
150
+ let _cleanup = unsafe { magnus::embed::init() };
151
+ let ruby = magnus::Ruby::get().unwrap();
152
+ crate::init(&ruby).unwrap();
153
+
154
+ const BUF_LEN: usize = 1 << 20; // 1MiB
155
+
156
+ // Create a buffer with random data.
157
+ let mut buf = vec![0u8; BUF_LEN];
158
+ thread_rng().fill(buf.as_mut_slice());
159
+
160
+ let TestFile {
161
+ file,
162
+ path,
163
+ dir: _dir,
164
+ } = TestFile::new(&buf);
165
+
166
+ let mut info = FileInfo {
167
+ file,
168
+ path,
169
+ len: buf.len(),
170
+ multiprocess_mode: Symbol::new("puma"),
171
+ type_: StaticSymbol::new("max"),
172
+ pid: "worker-0_0".to_string(),
173
+ };
174
+
175
+ let mut out_buf = Vec::new();
176
+ info.read_from_file(&mut out_buf).unwrap();
177
+
178
+ assert_eq!(buf.len(), out_buf.len(), "buffer lens");
179
+
180
+ let mut in_hasher = Sha256::new();
181
+ in_hasher.update(&buf);
182
+ let in_hash = in_hasher.finalize();
183
+
184
+ let mut out_hasher = Sha256::new();
185
+ out_hasher.update(&out_buf);
186
+ let out_hash = out_hasher.finalize();
187
+
188
+ assert_eq!(in_hash, out_hash, "content hashes");
189
+ }
190
+ }
@@ -0,0 +1,79 @@
1
+ use magnus::exception::*;
2
+ use magnus::prelude::*;
3
+ use magnus::value::{Fixnum, Lazy, LazyId};
4
+ use magnus::{class, define_class, exception, function, method, Ruby};
5
+ use std::mem::size_of;
6
+
7
+ use crate::mmap::MmapedFile;
8
+
9
+ pub mod error;
10
+ pub mod file_entry;
11
+ pub mod file_info;
12
+ mod macros;
13
+ pub mod map;
14
+ pub mod mmap;
15
+ pub mod parser;
16
+ pub mod raw_entry;
17
+ pub mod util;
18
+
19
+ #[cfg(test)]
20
+ mod testhelper;
21
+
22
+ type Result<T> = std::result::Result<T, crate::error::MmapError>;
23
+
24
+ const MAP_SHARED: i64 = libc::MAP_SHARED as i64;
25
+ const HEADER_SIZE: usize = 2 * size_of::<u32>();
26
+
27
+ static SYM_GAUGE: LazyId = LazyId::new("gauge");
28
+ static SYM_MIN: LazyId = LazyId::new("min");
29
+ static SYM_MAX: LazyId = LazyId::new("max");
30
+ static SYM_LIVESUM: LazyId = LazyId::new("livesum");
31
+ static SYM_PID: LazyId = LazyId::new("pid");
32
+ static SYM_SAMPLES: LazyId = LazyId::new("samples");
33
+
34
+ static PROM_EPARSING_ERROR: Lazy<ExceptionClass> = Lazy::new(|_| {
35
+ let prom_err = define_class(
36
+ "PrometheusParsingError",
37
+ exception::runtime_error().as_r_class(),
38
+ )
39
+ .expect("failed to create class `PrometheusParsingError`");
40
+ ExceptionClass::from_value(prom_err.as_value())
41
+ .expect("failed to create exception class from `PrometheusParsingError`")
42
+ });
43
+
44
+ #[magnus::init]
45
+ fn init(ruby: &Ruby) -> magnus::error::Result<()> {
46
+ // Initialize the static symbols
47
+ LazyId::force(&SYM_GAUGE, ruby);
48
+ LazyId::force(&SYM_MIN, ruby);
49
+ LazyId::force(&SYM_MAX, ruby);
50
+ LazyId::force(&SYM_LIVESUM, ruby);
51
+ LazyId::force(&SYM_PID, ruby);
52
+ LazyId::force(&SYM_SAMPLES, ruby);
53
+
54
+ // Initialize `PrometheusParsingError` class.
55
+ Lazy::force(&PROM_EPARSING_ERROR, ruby);
56
+
57
+ let klass = define_class("FastMmapedFileRs", class::object())?;
58
+ klass.undef_default_alloc_func();
59
+
60
+ // UNWRAP: We know `MAP_SHARED` fits in a `Fixnum`.
61
+ klass.const_set("MAP_SHARED", Fixnum::from_i64(MAP_SHARED).unwrap())?;
62
+
63
+ klass.define_singleton_method("to_metrics", function!(MmapedFile::to_metrics, 1))?;
64
+
65
+ // Required for subclassing to work
66
+ klass.define_alloc_func::<MmapedFile>();
67
+ klass.define_singleton_method("new", method!(MmapedFile::new, -1))?;
68
+ klass.define_method("initialize", method!(MmapedFile::initialize, 1))?;
69
+ klass.define_method("slice", method!(MmapedFile::slice, -1))?;
70
+ klass.define_method("sync", method!(MmapedFile::sync, -1))?;
71
+ klass.define_method("munmap", method!(MmapedFile::munmap, 0))?;
72
+
73
+ klass.define_method("used", method!(MmapedFile::load_used, 0))?;
74
+ klass.define_method("used=", method!(MmapedFile::save_used, 1))?;
75
+ klass.define_method("fetch_entry", method!(MmapedFile::fetch_entry, 3))?;
76
+ klass.define_method("upsert_entry", method!(MmapedFile::upsert_entry, 3))?;
77
+
78
+ Ok(())
79
+ }
@@ -0,0 +1,14 @@
1
+ #[macro_export]
2
+ macro_rules! err {
3
+ (with_errno: $err_t:expr, $($arg:expr),*) => {
4
+ {
5
+ let err = format!($($arg),*);
6
+ let strerror = strerror(errno());
7
+ Error::new($err_t, format!("{err} ({strerror})"))
8
+ }
9
+ };
10
+
11
+ ($err_t:expr, $($arg:expr),*) => {
12
+ Error::new($err_t, format!($($arg),*))
13
+ };
14
+ }