|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Benchmark for Azure Monitor Transformer: OTLP logs -> JSON conversion. |
| 5 | +//! |
| 6 | +//! MacOS M4 Pro baseline results (1000 records): |
| 7 | +//! Original (per-record resource/scope mapping): 1.60ms (~625K records/s) |
| 8 | +//! Hoisted (resource/scope computed once): 1.36ms (~735K records/s) +17% |
| 9 | +//! Hoisted + Direct Serialization (current): 425µs (~2.35M records/s) +275% |
| 10 | +
|
| 11 | +#![allow(unused_results)] |
| 12 | + |
| 13 | +use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; |
| 14 | +use otap_df_contrib_nodes::exporters::azure_monitor_exporter::{Config, Transformer}; |
| 15 | +use otap_df_pdata::views::otlp::bytes::logs::RawLogsData; |
| 16 | +use prost::Message; |
| 17 | +use serde_json::json; |
| 18 | +use std::collections::HashMap; |
| 19 | + |
| 20 | +use otap_df_pdata::proto::opentelemetry::common::v1::{ |
| 21 | + AnyValue, InstrumentationScope, KeyValue, any_value::Value as OtelAnyValueEnum, |
| 22 | +}; |
| 23 | +use otap_df_pdata::proto::opentelemetry::logs::v1::{LogRecord, ResourceLogs, ScopeLogs}; |
| 24 | +use otap_df_pdata::proto::opentelemetry::resource::v1::Resource; |
| 25 | + |
| 26 | +use otap_df_pdata::proto::opentelemetry::collector::logs::v1::ExportLogsServiceRequest; |
| 27 | + |
| 28 | +fn create_config() -> Config { |
| 29 | + use otap_df_contrib_nodes::exporters::azure_monitor_exporter::config::{ |
| 30 | + ApiConfig, AuthConfig, SchemaConfig, |
| 31 | + }; |
| 32 | + |
| 33 | + Config { |
| 34 | + api: ApiConfig { |
| 35 | + dcr_endpoint: "https://test.ingest.monitor.azure.com".into(), |
| 36 | + stream_name: "Custom-TestTable".into(), |
| 37 | + dcr: "dcr-test-rule-id".into(), |
| 38 | + schema: SchemaConfig { |
| 39 | + resource_mapping: HashMap::from([ |
| 40 | + ("service.name".into(), "ServiceName".into()), |
| 41 | + ("service.version".into(), "ServiceVersion".into()), |
| 42 | + ("host.name".into(), "HostName".into()), |
| 43 | + ]), |
| 44 | + scope_mapping: HashMap::from([ |
| 45 | + ("scope.name".into(), "ScopeName".into()), |
| 46 | + ("scope.version".into(), "ScopeVersion".into()), |
| 47 | + ]), |
| 48 | + log_record_mapping: HashMap::from([ |
| 49 | + ("time_unix_nano".into(), json!("TimeGenerated")), |
| 50 | + ("severity_text".into(), json!("SeverityText")), |
| 51 | + ("severity_number".into(), json!("SeverityNumber")), |
| 52 | + ("body".into(), json!("Body")), |
| 53 | + ("trace_id".into(), json!("TraceId")), |
| 54 | + ("span_id".into(), json!("SpanId")), |
| 55 | + ( |
| 56 | + "attributes".into(), |
| 57 | + json!({ |
| 58 | + "env": "Environment", |
| 59 | + "request.id": "RequestId", |
| 60 | + "user.id": "UserId" |
| 61 | + }), |
| 62 | + ), |
| 63 | + ]), |
| 64 | + }, |
| 65 | + }, |
| 66 | + auth: AuthConfig::default(), |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +fn make_log_record(i: usize) -> LogRecord { |
| 71 | + LogRecord { |
| 72 | + time_unix_nano: 1_700_000_000_000_000_000 + (i as u64) * 1_000_000, |
| 73 | + observed_time_unix_nano: 1_700_000_000_000_000_000 + (i as u64) * 1_000_000, |
| 74 | + severity_number: 9, // INFO |
| 75 | + severity_text: "INFO".into(), |
| 76 | + body: Some(AnyValue { |
| 77 | + value: Some(OtelAnyValueEnum::StringValue(format!( |
| 78 | + "Log message number {i}" |
| 79 | + ))), |
| 80 | + }), |
| 81 | + attributes: vec![ |
| 82 | + KeyValue { |
| 83 | + key: "env".into(), |
| 84 | + value: Some(AnyValue { |
| 85 | + value: Some(OtelAnyValueEnum::StringValue("production".into())), |
| 86 | + }), |
| 87 | + }, |
| 88 | + KeyValue { |
| 89 | + key: "request.id".into(), |
| 90 | + value: Some(AnyValue { |
| 91 | + value: Some(OtelAnyValueEnum::StringValue(format!("req-{i:06}"))), |
| 92 | + }), |
| 93 | + }, |
| 94 | + KeyValue { |
| 95 | + key: "user.id".into(), |
| 96 | + value: Some(AnyValue { |
| 97 | + value: Some(OtelAnyValueEnum::StringValue("user-42".into())), |
| 98 | + }), |
| 99 | + }, |
| 100 | + ], |
| 101 | + trace_id: vec![ |
| 102 | + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, |
| 103 | + 0xcd, 0xef, |
| 104 | + ], |
| 105 | + span_id: vec![0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10], |
| 106 | + flags: 1, |
| 107 | + ..Default::default() |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +fn make_request( |
| 112 | + num_resource_logs: usize, |
| 113 | + num_scope_logs: usize, |
| 114 | + records_per_scope: usize, |
| 115 | +) -> Vec<u8> { |
| 116 | + let resource_logs: Vec<ResourceLogs> = (0..num_resource_logs) |
| 117 | + .map(|_| ResourceLogs { |
| 118 | + resource: Some(Resource { |
| 119 | + attributes: vec![ |
| 120 | + KeyValue { |
| 121 | + key: "service.name".into(), |
| 122 | + value: Some(AnyValue { |
| 123 | + value: Some(OtelAnyValueEnum::StringValue("my-service".into())), |
| 124 | + }), |
| 125 | + }, |
| 126 | + KeyValue { |
| 127 | + key: "service.version".into(), |
| 128 | + value: Some(AnyValue { |
| 129 | + value: Some(OtelAnyValueEnum::StringValue("1.2.3".into())), |
| 130 | + }), |
| 131 | + }, |
| 132 | + KeyValue { |
| 133 | + key: "host.name".into(), |
| 134 | + value: Some(AnyValue { |
| 135 | + value: Some(OtelAnyValueEnum::StringValue("host-01.prod".into())), |
| 136 | + }), |
| 137 | + }, |
| 138 | + ], |
| 139 | + dropped_attributes_count: 0, |
| 140 | + entity_refs: vec![], |
| 141 | + }), |
| 142 | + scope_logs: (0..num_scope_logs) |
| 143 | + .map(|_| ScopeLogs { |
| 144 | + scope: Some(InstrumentationScope { |
| 145 | + name: "my-library".into(), |
| 146 | + version: "0.1.0".into(), |
| 147 | + attributes: vec![ |
| 148 | + KeyValue { |
| 149 | + key: "scope.name".into(), |
| 150 | + value: Some(AnyValue { |
| 151 | + value: Some(OtelAnyValueEnum::StringValue("my-library".into())), |
| 152 | + }), |
| 153 | + }, |
| 154 | + KeyValue { |
| 155 | + key: "scope.version".into(), |
| 156 | + value: Some(AnyValue { |
| 157 | + value: Some(OtelAnyValueEnum::StringValue("0.1.0".into())), |
| 158 | + }), |
| 159 | + }, |
| 160 | + ], |
| 161 | + dropped_attributes_count: 0, |
| 162 | + }), |
| 163 | + log_records: (0..records_per_scope).map(make_log_record).collect(), |
| 164 | + schema_url: String::new(), |
| 165 | + }) |
| 166 | + .collect(), |
| 167 | + schema_url: String::new(), |
| 168 | + }) |
| 169 | + .collect(); |
| 170 | + |
| 171 | + let request = ExportLogsServiceRequest { resource_logs }; |
| 172 | + request.encode_to_vec() |
| 173 | +} |
| 174 | + |
| 175 | +fn bench_transform(c: &mut Criterion) { |
| 176 | + let mut group = c.benchmark_group("transformer"); |
| 177 | + |
| 178 | + let config = create_config(); |
| 179 | + let transformer = Transformer::new(&config); |
| 180 | + |
| 181 | + // Varying record counts: 1 ResourceLogs, 1 ScopeLogs, N records |
| 182 | + for num_records in [10, 100, 1000] { |
| 183 | + let bytes = make_request(1, 1, num_records); |
| 184 | + let total_records = num_records; |
| 185 | + |
| 186 | + group.throughput(criterion::Throughput::Elements(total_records as u64)); |
| 187 | + group.bench_with_input( |
| 188 | + BenchmarkId::new("1r_1s", total_records), |
| 189 | + &bytes, |
| 190 | + |b, bytes| { |
| 191 | + b.iter(|| { |
| 192 | + let view = RawLogsData::new(bytes); |
| 193 | + let result = transformer.convert_to_log_analytics(&view); |
| 194 | + assert_eq!(result.len(), total_records); |
| 195 | + }); |
| 196 | + }, |
| 197 | + ); |
| 198 | + } |
| 199 | + |
| 200 | + // Many scopes: 1 ResourceLogs, 10 ScopeLogs, 100 records each |
| 201 | + { |
| 202 | + let bytes = make_request(1, 10, 100); |
| 203 | + let total_records = 1000; |
| 204 | + |
| 205 | + group.throughput(criterion::Throughput::Elements(total_records as u64)); |
| 206 | + group.bench_with_input( |
| 207 | + BenchmarkId::new("1r_10s", total_records), |
| 208 | + &bytes, |
| 209 | + |b, bytes| { |
| 210 | + b.iter(|| { |
| 211 | + let view = RawLogsData::new(bytes); |
| 212 | + let result = transformer.convert_to_log_analytics(&view); |
| 213 | + assert_eq!(result.len(), total_records); |
| 214 | + }); |
| 215 | + }, |
| 216 | + ); |
| 217 | + } |
| 218 | + |
| 219 | + // Many resources: 10 ResourceLogs, 1 ScopeLogs, 100 records each |
| 220 | + { |
| 221 | + let bytes = make_request(10, 1, 100); |
| 222 | + let total_records = 1000; |
| 223 | + |
| 224 | + group.throughput(criterion::Throughput::Elements(total_records as u64)); |
| 225 | + group.bench_with_input( |
| 226 | + BenchmarkId::new("10r_1s", total_records), |
| 227 | + &bytes, |
| 228 | + |b, bytes| { |
| 229 | + b.iter(|| { |
| 230 | + let view = RawLogsData::new(bytes); |
| 231 | + let result = transformer.convert_to_log_analytics(&view); |
| 232 | + assert_eq!(result.len(), total_records); |
| 233 | + }); |
| 234 | + }, |
| 235 | + ); |
| 236 | + } |
| 237 | + |
| 238 | + group.finish(); |
| 239 | +} |
| 240 | + |
| 241 | +criterion_group!(benches, bench_transform); |
| 242 | +criterion_main!(benches); |
0 commit comments