-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathotlp_encoder.rs
More file actions
400 lines (356 loc) · 15.1 KB
/
Copy pathotlp_encoder.rs
File metadata and controls
400 lines (356 loc) · 15.1 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use crate::payload_encoder::bond_encoder::{BondDataType, BondEncodedSchema, BondWriter, FieldDef};
use crate::payload_encoder::central_blob::{CentralBlob, CentralEventEntry, CentralSchemaEntry};
use chrono::{TimeZone, Utc};
use opentelemetry_proto::tonic::common::v1::any_value::Value;
use opentelemetry_proto::tonic::logs::v1::LogRecord;
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
type SchemaCache = Arc<RwLock<HashMap<u64, (BondEncodedSchema, [u8; 16], Vec<FieldDef>)>>>;
type BatchKey = (u64, String);
type EncodedRow = (String, u8, Vec<u8>); // (event_name, level, row_buffer)
type BatchValue = (CentralSchemaEntry, Vec<EncodedRow>);
type LogBatches = HashMap<BatchKey, BatchValue>;
const FIELD_ENV_NAME: &str = "env_name";
const FIELD_ENV_VER: &str = "env_ver";
const FIELD_TIMESTAMP: &str = "timestamp";
const FIELD_ENV_TIME: &str = "env_time";
const FIELD_TRACE_ID: &str = "env_dt_traceId";
const FIELD_SPAN_ID: &str = "env_dt_spanId";
const FIELD_TRACE_FLAGS: &str = "env_dt_traceFlags";
const FIELD_NAME: &str = "name";
const FIELD_SEVERITY_NUMBER: &str = "SeverityNumber";
const FIELD_SEVERITY_TEXT: &str = "SeverityText";
const FIELD_BODY: &str = "body";
/// Encoder to write OTLP payload in bond form.
#[derive(Clone)]
pub struct OtlpEncoder {
// TODO - limit cache size or use LRU eviction, and/or add feature flag for caching
schema_cache: SchemaCache,
}
impl OtlpEncoder {
pub fn new() -> Self {
OtlpEncoder {
schema_cache: Arc::new(RwLock::new(HashMap::new())),
}
}
pub fn encode_log_batch<'a, I>(
&self,
logs: I,
metadata: &str,
) -> Vec<(u64, String, Vec<u8>, usize)>
where
I: Iterator<Item = &'a opentelemetry_proto::tonic::logs::v1::LogRecord>,
{
use std::collections::HashMap;
let mut batches: LogBatches = HashMap::new();
for log_record in logs {
// 1. Get schema
let field_specs = self.determine_fields(log_record);
let schema_id = Self::calculate_schema_id(&field_specs);
let (schema_entry, field_info) = self.get_or_create_schema(schema_id, field_specs);
// 2. Encode row
let row_buffer = self.write_row_data(log_record, &field_info);
let event_name = if log_record.event_name.is_empty() {
"Log".to_string()
} else {
log_record.event_name.clone()
};
let level = log_record.severity_number as u8;
// 3. Insert into batches - Key is (schema_id, event_name)
batches
.entry((schema_id, event_name.clone()))
.or_insert_with(|| (schema_entry, Vec::new()))
.1
.push((event_name, level, row_buffer));
}
// 4. Encode blobs (one per schema AND event_name combination)
let mut blobs = Vec::new();
for ((schema_id, batch_event_name), (schema_entry, records)) in batches {
let events: Vec<CentralEventEntry> = records
.into_iter()
.map(|(event_name, level, row_buffer)| CentralEventEntry {
schema_id,
level,
event_name,
row: row_buffer,
})
.collect();
let events_len = events.len();
let blob = CentralBlob {
version: 1,
format: 2,
metadata: metadata.to_string(),
schemas: vec![schema_entry],
events,
};
let bytes = blob.to_bytes();
blobs.push((schema_id, batch_event_name, bytes, events_len));
}
blobs
}
/// Determine which fields are present in the LogRecord
fn determine_fields(&self, log: &LogRecord) -> Vec<FieldDef> {
// Pre-allocate with estimated capacity to avoid reallocations
let estimated_capacity = 7 + 4 + log.attributes.len();
let mut fields = Vec::with_capacity(estimated_capacity);
fields.push((Cow::Borrowed(FIELD_ENV_NAME), BondDataType::BT_STRING));
fields.push((FIELD_ENV_VER.into(), BondDataType::BT_STRING));
fields.push((FIELD_TIMESTAMP.into(), BondDataType::BT_STRING));
fields.push((FIELD_ENV_TIME.into(), BondDataType::BT_STRING));
// Part A extension - Conditional fields
if !log.trace_id.is_empty() {
fields.push((FIELD_TRACE_ID.into(), BondDataType::BT_STRING));
}
if !log.span_id.is_empty() {
fields.push((FIELD_SPAN_ID.into(), BondDataType::BT_STRING));
}
if log.flags != 0 {
fields.push((FIELD_TRACE_FLAGS.into(), BondDataType::BT_UINT32));
}
// Part B - Core log fields
if !log.event_name.is_empty() {
fields.push((FIELD_NAME.into(), BondDataType::BT_STRING));
}
fields.push((FIELD_SEVERITY_NUMBER.into(), BondDataType::BT_INT32));
if !log.severity_text.is_empty() {
fields.push((FIELD_SEVERITY_TEXT.into(), BondDataType::BT_STRING));
}
if let Some(body) = &log.body {
if let Some(Value::StringValue(_)) = &body.value {
// Only included in schema when body is a string value
fields.push((FIELD_BODY.into(), BondDataType::BT_STRING));
}
//TODO - handle other body types
}
// Part C - Dynamic attributes
for attr in &log.attributes {
if let Some(val) = attr.value.as_ref().and_then(|v| v.value.as_ref()) {
let type_id = match val {
Value::StringValue(_) => BondDataType::BT_STRING,
Value::IntValue(_) => BondDataType::BT_INT64,
Value::DoubleValue(_) => BondDataType::BT_DOUBLE,
Value::BoolValue(_) => BondDataType::BT_BOOL,
_ => continue,
};
fields.push((attr.key.clone().into(), type_id));
}
}
fields.sort_by(|a, b| a.0.cmp(&b.0)); // Sort fields by name consistent schema ID generation
fields
.into_iter()
.enumerate()
.map(|(i, (name, type_id))| FieldDef {
name,
type_id,
field_id: (i + 1) as u16,
})
.collect()
}
/// Calculate schema ID from field specifications
fn calculate_schema_id(fields: &[FieldDef]) -> u64 {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
for field in fields {
field.name.hash(&mut hasher);
field.type_id.hash(&mut hasher);
}
hasher.finish()
}
/// Get or create schema with field ordering information
fn get_or_create_schema(
&self,
schema_id: u64,
field_info: Vec<FieldDef>,
) -> (CentralSchemaEntry, Vec<FieldDef>) {
// Check cache first
if let Some((schema, schema_md5, field_info)) =
self.schema_cache.read().unwrap().get(&schema_id)
{
return (
CentralSchemaEntry {
id: schema_id,
md5: *schema_md5,
schema: schema.clone(),
},
field_info.clone(),
);
}
let schema =
BondEncodedSchema::from_fields("OtlpLogRecord", "telemetry", field_info.clone()); //TODO - use actual struct name and namespace
let schema_bytes = schema.as_bytes();
let schema_md5 = md5::compute(schema_bytes).0;
// Cache the schema and field info
{
let mut cache = self.schema_cache.write().unwrap();
// TODO: Refactor to eliminate field_info duplication in cache
// The field information (name, type_id, order) is already stored in BondEncodedSchema's
// DynamicSchema.fields vector. We should:
// 1. Ensure DynamicSchema maintains fields in sorted order
// 2. Add a method to BondEncodedSchema to iterate fields for row encoding
// 3. Remove field_info from cache tuple to reduce memory usage and cloning overhead
// This would require updating write_row_data() to work with DynamicSchema fields directly
cache.insert(schema_id, (schema.clone(), schema_md5, field_info.clone()));
}
let schema_bytes = schema.as_bytes();
let schema_md5 = md5::compute(schema_bytes).0;
(
CentralSchemaEntry {
id: schema_id,
md5: schema_md5,
schema,
},
field_info,
)
}
/// Write row data directly from LogRecord
fn write_row_data(&self, log: &LogRecord, sorted_fields: &[FieldDef]) -> Vec<u8> {
let mut buffer = Vec::with_capacity(sorted_fields.len() * 50); //TODO - estimate better
for field in sorted_fields {
match field.name.as_ref() {
FIELD_ENV_NAME => BondWriter::write_string(&mut buffer, "TestEnv"), // TODO - placeholder for actual env name
FIELD_ENV_VER => BondWriter::write_string(&mut buffer, "4.0"), // TODO - placeholder for actual env version
FIELD_TIMESTAMP | FIELD_ENV_TIME => {
let dt = Self::format_timestamp(log.observed_time_unix_nano);
BondWriter::write_string(&mut buffer, &dt);
}
FIELD_TRACE_ID => {
let hex_bytes = Self::encode_id_to_hex::<32>(&log.trace_id);
let hex_str = std::str::from_utf8(&hex_bytes).unwrap();
BondWriter::write_string(&mut buffer, hex_str);
}
FIELD_SPAN_ID => {
let hex_bytes = Self::encode_id_to_hex::<16>(&log.span_id);
let hex_str = std::str::from_utf8(&hex_bytes).unwrap();
BondWriter::write_string(&mut buffer, hex_str);
}
FIELD_TRACE_FLAGS => {
BondWriter::write_numeric(&mut buffer, log.flags);
}
FIELD_NAME => {
BondWriter::write_string(&mut buffer, &log.event_name);
}
FIELD_SEVERITY_NUMBER => {
BondWriter::write_numeric(&mut buffer, log.severity_number)
}
FIELD_SEVERITY_TEXT => {
BondWriter::write_string(&mut buffer, &log.severity_text);
}
FIELD_BODY => {
// TODO - handle all types of body values - For now, we only handle string values
if let Some(body) = &log.body {
if let Some(Value::StringValue(s)) = &body.value {
BondWriter::write_string(&mut buffer, s);
}
}
}
_ => {
// Handle dynamic attributes
// TODO - optimize better - we could update determine_fields to also return a vec of bytes which has bond serialized attributes
if let Some(attr) = log.attributes.iter().find(|a| a.key == field.name) {
self.write_attribute_value(&mut buffer, attr, field.type_id);
}
}
}
}
buffer
}
fn encode_id_to_hex<const N: usize>(id: &[u8]) -> [u8; N] {
let mut hex_bytes = [0u8; N];
hex::encode_to_slice(id, &mut hex_bytes).unwrap();
hex_bytes
}
/// Format timestamp from nanoseconds
fn format_timestamp(nanos: u64) -> String {
let secs = (nanos / 1_000_000_000) as i64;
let nsec = (nanos % 1_000_000_000) as u32;
Utc.timestamp_opt(secs, nsec)
.single()
.unwrap_or_else(|| Utc.timestamp_opt(0, 0).single().unwrap())
.to_rfc3339()
}
/// Write attribute value based on its type
fn write_attribute_value(
&self,
buffer: &mut Vec<u8>,
attr: &opentelemetry_proto::tonic::common::v1::KeyValue,
expected_type: BondDataType,
) {
if let Some(val) = &attr.value {
match (&val.value, expected_type) {
(Some(Value::StringValue(s)), BondDataType::BT_STRING) => {
BondWriter::write_string(buffer, s)
}
(Some(Value::IntValue(i)), BondDataType::BT_INT64) => {
BondWriter::write_numeric(buffer, *i)
}
(Some(Value::DoubleValue(d)), BondDataType::BT_DOUBLE) => {
BondWriter::write_numeric(buffer, *d)
}
(Some(Value::BoolValue(b)), BondDataType::BT_BOOL) => {
// TODO - represent bool as BT_BOOL
BondWriter::write_bool(buffer, *b)
}
_ => {} // TODO - handle more types
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use opentelemetry_proto::tonic::common::v1::{AnyValue, KeyValue};
#[test]
fn test_encoding() {
let encoder = OtlpEncoder::new();
let mut log = LogRecord {
observed_time_unix_nano: 1_700_000_000_000_000_000,
event_name: "test_event".to_string(),
severity_number: 9,
severity_text: "INFO".to_string(),
..Default::default()
};
// Add some attributes
log.attributes.push(KeyValue {
key: "user_id".to_string(),
value: Some(AnyValue {
value: Some(Value::StringValue("user123".to_string())),
}),
});
log.attributes.push(KeyValue {
key: "request_count".to_string(),
value: Some(AnyValue {
value: Some(Value::IntValue(42)),
}),
});
let metadata = "namespace=testNamespace/eventVersion=Ver1v0";
let result = encoder.encode_log_batch([log].iter(), metadata);
assert!(!result.is_empty());
}
#[test]
fn test_schema_caching() {
let encoder = OtlpEncoder::new();
let log1 = LogRecord {
observed_time_unix_nano: 1_700_000_000_000_000_000,
severity_number: 9,
..Default::default()
};
let mut log2 = LogRecord {
observed_time_unix_nano: 1_700_000_001_000_000_000,
severity_number: 10,
..Default::default()
};
let metadata = "namespace=test";
// First encoding creates schema
let _result1 = encoder.encode_log_batch([log1].iter(), metadata);
assert_eq!(encoder.schema_cache.read().unwrap().len(), 1);
// Second encoding with same schema structure reuses schema
let _result2 = encoder.encode_log_batch([log2.clone()].iter(), metadata);
assert_eq!(encoder.schema_cache.read().unwrap().len(), 1);
// Add trace_id to create different schema
log2.trace_id = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
let _result3 = encoder.encode_log_batch([log2].iter(), metadata);
assert_eq!(encoder.schema_cache.read().unwrap().len(), 2);
}
}