1- //! Tag generation for OpenTelemetry metric payloads
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;
6+
27use std:: { cmp, rc:: Rc } ;
38
4- use super :: templates:: GeneratorError ;
5- use crate :: { Error , Generator , common:: config:: ConfRange , common:: strings:: Pool } ;
9+ use crate :: { Error , Generator , SizedGenerator , common:: config:: ConfRange , common:: strings:: Pool } ;
610use opentelemetry_proto:: tonic:: common:: v1:: { AnyValue , KeyValue , any_value} ;
711use prost:: Message ;
812
13+ /// Errors that can occur during generation
14+ #[ derive( thiserror:: Error , Debug , Clone , Copy ) ]
15+ pub enum GeneratorError {
16+ #[ error( "Generator exhausted bytes budget prematurely" ) ]
17+ SizeExhausted ,
18+ #[ error( "Failed to generate string" ) ]
19+ StringGenerate ,
20+ }
21+
22+ /// Ratio of unique tags to use in tag generation
23+ pub ( crate ) const UNIQUE_TAG_RATIO : f32 = 0.75 ;
24+
25+ /// Smallest useful `KeyValue` protobuf, determined by experimentation and enforced in tests
26+ pub ( crate ) const SMALLEST_KV_PROTOBUF : usize = 10 ;
27+
28+ /// Tag generator for OpenTelemetry attributes
929#[ derive( Debug , Clone ) ]
1030pub ( crate ) struct TagGenerator {
1131 inner : crate :: common:: tags:: Generator ,
1232}
1333
14- // smallest useful protobuf, determined by experimentation and enforced in
15- // smallest_kv_protobuf test
16- const SMALLEST_KV_PROTOBUF : usize = 10 ;
17-
1834impl TagGenerator {
1935 /// Creates a new tag generator
2036 ///
@@ -43,7 +59,8 @@ impl TagGenerator {
4359 }
4460}
4561
46- fn varint_len ( v : usize ) -> usize {
62+ /// Calculate the length of a varint encoding
63+ pub ( crate ) fn varint_len ( v : usize ) -> usize {
4764 let mut v = v;
4865 let mut n = 1 ;
4966 while v > 0x7f {
@@ -53,14 +70,15 @@ fn varint_len(v: usize) -> usize {
5370 n
5471}
5572
56- fn overhead ( v : usize ) -> usize {
73+ /// Calculate the overhead for a `KeyValue` in a repeated field
74+ pub ( crate ) fn overhead ( v : usize ) -> usize {
5775 // overhead in a repeated field is per-item, so:
5876 //
5977 // [tag-byte] [varint-length] [kv-bytes…]
6078 varint_len ( v) + 1 + v
6179}
6280
63- impl < ' a > crate :: SizedGenerator < ' a > for TagGenerator {
81+ impl < ' a > SizedGenerator < ' a > for TagGenerator {
6482 type Output = Vec < KeyValue > ;
6583 type Error = GeneratorError ;
6684
@@ -143,11 +161,11 @@ mod test {
143161 } ) ,
144162 } ;
145163
146- let encoded_size = overhead ( kv. encoded_len ( ) ) ;
164+ let sz = overhead ( kv. encoded_len ( ) ) ;
147165
148- assert ! (
149- encoded_size == SMALLEST_KV_PROTOBUF ,
150- "Minimal useful request size ({encoded_size}) should be == SMALLEST_KV_PROTOBUF ({SMALLEST_KV_PROTOBUF}) "
166+ assert_eq ! (
167+ sz , SMALLEST_KV_PROTOBUF ,
168+ "Minimal useful key/value pair should have size { SMALLEST_KV_PROTOBUF}, was {sz} "
151169 ) ;
152170 }
153171}
0 commit comments