-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlib.rs
More file actions
211 lines (197 loc) · 6.48 KB
/
lib.rs
File metadata and controls
211 lines (197 loc) · 6.48 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! The lading payloads
//!
//! This library supports payload generation for the lading project.
#![deny(clippy::all)]
#![deny(clippy::cargo)]
#![deny(clippy::pedantic)]
#![deny(clippy::print_stdout)]
#![deny(clippy::print_stderr)]
#![deny(clippy::dbg_macro)]
#![deny(clippy::unwrap_used)]
#![deny(unused_extern_crates)]
#![deny(unused_allocation)]
#![deny(unused_assignments)]
#![deny(unused_comparisons)]
#![deny(unreachable_pub)]
#![deny(missing_docs)]
#![deny(missing_copy_implementations)]
#![deny(missing_debug_implementations)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::multiple_crate_versions)]
use std::{
io::{self, Write},
path::PathBuf,
};
use rand::{Rng, distr::weighted};
use serde::{Deserialize, Serialize as SerdeSerialize};
pub mod block;
pub use apache_common::ApacheCommon;
pub use ascii::Ascii;
pub use datadog_logs::DatadogLog;
pub use dogstatsd::DogStatsD;
pub use fluent::Fluent;
pub use json::Json;
pub use opentelemetry_log::OpentelemetryLogs;
pub use opentelemetry_metric::OpentelemetryMetrics;
pub use opentelemetry_trace::OpentelemetryTraces;
pub use splunk_hec::SplunkHec;
pub use statik::Static;
pub use syslog::Syslog5424;
pub use trace_agent::TraceAgent;
pub mod apache_common;
pub mod ascii;
pub mod common;
pub mod datadog_logs;
pub mod dogstatsd;
pub mod fluent;
pub mod json;
pub mod opentelemetry_log;
pub mod opentelemetry_metric;
pub mod opentelemetry_trace;
pub mod procfs;
pub mod splunk_hec;
pub mod statik;
pub mod syslog;
pub mod trace_agent;
/// Errors related to serialization
#[derive(thiserror::Error, Debug)]
pub enum Error {
/// `MsgPack` payload could not be encoded
#[error("MsgPack payload could not be encoded: {0}")]
MsgPack(#[from] rmp_serde::encode::Error),
/// Json payload could not be encoded
#[error("Json payload could not be encoded: {0}")]
Json(#[from] serde_json::Error),
/// IO operation failed
#[error("IO operation failed: {0}")]
Io(#[from] io::Error),
/// failed to generate string
#[error("Failed to generate string")]
StringGenerate,
/// Serialization failed
#[error("Serialization failed")]
Serialize,
/// See [`weighted::Error`]
#[error(transparent)]
Weights(#[from] weighted::Error),
/// See [`unit::Error`]
#[error(transparent)]
Unit(#[from] opentelemetry_metric::unit::Error),
/// See [`prost::EncodeError`]
#[error(transparent)]
ProstEncode(#[from] prost::EncodeError),
}
/// To serialize into bytes
pub trait Serialize {
/// Write bytes into writer, subject to `max_bytes` limitations.
///
/// # Errors
///
/// Most implementations are serializing data in some way. The errors that
/// result come from serialization crackups.
fn to_bytes<W, R>(&mut self, rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
where
R: Rng + Sized,
W: Write;
}
/// Sub-configuration for `TraceAgent` format
#[derive(Debug, Deserialize, SerdeSerialize, Clone, Copy, PartialEq)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum Encoding {
/// Use JSON format
Json,
/// Use `MsgPack` binary format
#[serde(alias = "msgpack")]
MsgPack,
}
/// Configuration for `Payload`
#[derive(Debug, Deserialize, SerdeSerialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[serde(deny_unknown_fields)]
pub enum Config {
/// Generates Fluent messages
Fluent,
/// Generates syslog5424 messages
Syslog5424,
/// Generates Splunk HEC messages
SplunkHec {
/// Defines the encoding to use for the Splunk HEC messages.
encoding: splunk_hec::Encoding,
},
/// Generates Datadog Logs JSON messages
DatadogLog,
/// Generates a static, user supplied data
Static {
/// Defines the file path to read static variant data from. Content is
/// assumed to be line-oriented but no other claim is made on the file.
static_path: PathBuf,
},
/// Generates a line of printable ascii characters
Ascii,
/// Generates a json encoded line
Json,
/// Generates a Apache Common log lines
ApacheCommon,
/// Generates OpenTelemetry traces
OpentelemetryTraces,
/// Generates OpenTelemetry logs
OpentelemetryLogs,
/// Generates OpenTelemetry metrics
OpentelemetryMetrics(crate::opentelemetry_metric::Config),
/// Generates `DogStatsD`
#[serde(rename = "dogstatsd")]
DogStatsD(crate::dogstatsd::Config),
/// Generates `TraceAgent` payloads in JSON format
TraceAgent(Encoding),
}
#[derive(Debug)]
#[allow(dead_code, clippy::large_enum_variant)]
pub(crate) enum Payload {
ApacheCommon(ApacheCommon),
Ascii(Ascii),
DatadogLog(DatadogLog),
Fluent(Fluent),
Json(Json),
SplunkHec(splunk_hec::SplunkHec),
Static(Static),
Syslog(Syslog5424),
OtelTraces(OpentelemetryTraces),
OtelLogs(OpentelemetryLogs),
OtelMetrics(OpentelemetryMetrics),
DogStatsdD(DogStatsD),
TraceAgent(TraceAgent),
}
impl Serialize for Payload {
fn to_bytes<W, R>(&mut self, rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
where
W: Write,
R: Rng + Sized,
{
match self {
Payload::ApacheCommon(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::Ascii(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::DatadogLog(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::Fluent(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::Json(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::SplunkHec(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::Static(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::Syslog(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::OtelTraces(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::OtelLogs(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::OtelMetrics(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::DogStatsdD(ser) => ser.to_bytes(rng, max_bytes, writer),
Payload::TraceAgent(ser) => ser.to_bytes(rng, max_bytes, writer),
}
}
}
/// Generate instance of `I` from source of randomness `S`.
pub(crate) trait Generator<'a> {
type Output: 'a;
type Error: 'a;
fn generate<R>(&'a self, rng: &mut R) -> Result<Self::Output, Self::Error>
where
R: rand::Rng + ?Sized;
}