1- //! Tag generation for OpenTelemetry metric payloads
2- use std:: { cmp, rc:: Rc } ;
1+ //! Common utilities and types for OpenTelemetry payload generation
2+ //!
3+ //! This module contains shared code used by both metrics and logs implementations.
4+
5+ pub ( crate ) mod templates;
36
4- use super :: templates:: GeneratorError ;
5- use crate :: { Error , Generator , common:: config:: ConfRange , common:: strings:: Pool } ;
7+ use crate :: { Error , Generator , SizedGenerator , common:: config:: ConfRange , common:: strings:: Pool } ;
68use opentelemetry_proto:: tonic:: common:: v1:: { AnyValue , KeyValue , any_value} ;
79use prost:: Message ;
10+ use std:: { cmp, rc:: Rc } ;
11+
12+ /// Errors that can occur during generation
13+ #[ derive( thiserror:: Error , Debug , Clone , Copy ) ]
14+ pub enum GeneratorError {
15+ /// Generator exhausted bytes budget prematurely
16+ #[ error( "Generator exhausted bytes budget prematurely" ) ]
17+ SizeExhausted ,
18+ /// Failed to generate string
19+ #[ error( "Failed to generate string" ) ]
20+ StringGenerate ,
21+ }
22+
23+ /// Ratio of unique tags to use in tag generation
24+ pub ( crate ) const UNIQUE_TAG_RATIO : f32 = 0.75 ;
825
26+ /// Smallest useful `KeyValue` protobuf, determined by experimentation and enforced in tests
27+ pub ( crate ) const SMALLEST_KV_PROTOBUF : usize = 10 ;
28+
29+ /// Tag generator for OpenTelemetry attributes
930#[ derive( Debug , Clone ) ]
1031pub ( crate ) struct TagGenerator {
1132 inner : crate :: common:: tags:: Generator ,
1233}
1334
14- // smallest useful protobuf, determined by experimentation and enforced in
15- // smallest_kv_protobuf test
16- const SMALLEST_KV_PROTOBUF : usize = 10 ;
17-
1835impl TagGenerator {
1936 /// Creates a new tag generator
2037 ///
@@ -43,7 +60,8 @@ impl TagGenerator {
4360 }
4461}
4562
46- fn varint_len ( v : usize ) -> usize {
63+ /// Calculate the length of a varint encoding
64+ pub ( crate ) fn varint_len ( v : usize ) -> usize {
4765 let mut v = v;
4866 let mut n = 1 ;
4967 while v > 0x7f {
@@ -53,14 +71,15 @@ fn varint_len(v: usize) -> usize {
5371 n
5472}
5573
56- fn overhead ( v : usize ) -> usize {
74+ /// Calculate the overhead for a `KeyValue` in a repeated field
75+ pub ( crate ) fn overhead ( v : usize ) -> usize {
5776 // overhead in a repeated field is per-item, so:
5877 //
5978 // [tag-byte] [varint-length] [kv-bytes…]
6079 varint_len ( v) + 1 + v
6180}
6281
63- impl < ' a > crate :: SizedGenerator < ' a > for TagGenerator {
82+ impl < ' a > SizedGenerator < ' a > for TagGenerator {
6483 type Output = Vec < KeyValue > ;
6584 type Error = GeneratorError ;
6685
@@ -143,11 +162,11 @@ mod test {
143162 } ) ,
144163 } ;
145164
146- let encoded_size = overhead ( kv. encoded_len ( ) ) ;
165+ let sz = overhead ( kv. encoded_len ( ) ) ;
147166
148- assert ! (
149- encoded_size == SMALLEST_KV_PROTOBUF ,
150- "Minimal useful request size ({encoded_size}) should be == SMALLEST_KV_PROTOBUF ({SMALLEST_KV_PROTOBUF}) "
167+ assert_eq ! (
168+ sz , SMALLEST_KV_PROTOBUF ,
169+ "Minimal useful key/value pair should have size { SMALLEST_KV_PROTOBUF}, was {sz} "
151170 ) ;
152171 }
153172}
0 commit comments