55#include < prometheus/metric_family.h>
66#include < prometheus/metric_type.h>
77#include < algorithm>
8+ #include < cmath>
89#include < cstddef>
910#include < cstdint>
1011#include < functional>
12+ #include < iomanip>
1113#include < limits>
14+ #include < locale>
1215#include < regex>
1316#include < sstream>
1417#include < string>
@@ -101,6 +104,91 @@ std::string SanitizeLabel(std::string label_key)
101104 });
102105}
103106
107+ std::string EscapeJsonString (const std::string &str)
108+ {
109+ std::string out;
110+ out.reserve (str.size () + 2 );
111+ out.push_back (' "' );
112+ for (unsigned char c : str)
113+ {
114+ switch (c)
115+ {
116+ case ' "' :
117+ out.append (" \\\" " );
118+ break ;
119+ case ' \\ ' :
120+ out.append (" \\\\ " );
121+ break ;
122+ case ' \b ' :
123+ out.append (" \\ b" );
124+ break ;
125+ case ' \f ' :
126+ out.append (" \\ f" );
127+ break ;
128+ case ' \n ' :
129+ out.append (" \\ n" );
130+ break ;
131+ case ' \r ' :
132+ out.append (" \\ r" );
133+ break ;
134+ case ' \t ' :
135+ out.append (" \\ t" );
136+ break ;
137+ default :
138+ if (c <= 0x1F )
139+ {
140+ constexpr char kHex [] = " 0123456789abcdef" ;
141+ unsigned int uc = static_cast <unsigned int >(c);
142+ char buf[6 ] = {' \\ ' , ' u' , ' 0' , ' 0' , kHex [(uc >> 4U ) & 0x0FU ], kHex [uc & 0x0FU ]};
143+ out.append (buf, 6 );
144+ }
145+ else
146+ {
147+ out.push_back (static_cast <char >(c));
148+ }
149+ break ;
150+ }
151+ }
152+ out.push_back (' "' );
153+ return out;
154+ }
155+
156+ template <typename T>
157+ std::string NumericVectorToJson (const std::vector<T> &vec)
158+ {
159+ std::string result;
160+ result.push_back (' [' );
161+ for (std::size_t i = 0 ; i < vec.size (); ++i)
162+ {
163+ if (i > 0 )
164+ {
165+ result.push_back (' ,' );
166+ }
167+ result.append (std::to_string (vec[i]));
168+ }
169+ result.push_back (' ]' );
170+ return result;
171+ }
172+
173+ std::string FormatDouble (double val)
174+ {
175+ std::ostringstream ss;
176+ ss.imbue (std::locale::classic ());
177+ ss << std::setprecision (std::numeric_limits<double >::digits10) << val;
178+ std::string str = ss.str ();
179+ std::istringstream iss (str);
180+ iss.imbue (std::locale::classic ());
181+ double roundtrip = 0.0 ;
182+ if ((iss >> roundtrip) && roundtrip == val)
183+ {
184+ return str;
185+ }
186+ ss.str (" " );
187+ ss.clear ();
188+ ss << std::setprecision (std::numeric_limits<double >::max_digits10) << val;
189+ return ss.str ();
190+ }
191+
104192} // namespace
105193
106194/* *
@@ -142,6 +230,10 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
142230 {
143231 for (const auto &metric_data : instrumentation_info.metric_data_ )
144232 {
233+ if (metric_data.point_data_attr_ .empty ())
234+ {
235+ continue ;
236+ }
145237 prometheus_client::MetricFamily metric_family;
146238 metric_family.help = metric_data.instrument_descriptor .description_ ;
147239 auto front = metric_data.point_data_attr_ .front ();
@@ -671,7 +763,7 @@ void PrometheusExporterUtils::SetMetricBasic(
671763 const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
672764 const opentelemetry::sdk::resource::Resource *resource)
673765{
674- if (labels.empty () && nullptr == resource)
766+ if (labels.empty () && nullptr == scope && nullptr == resource)
675767 {
676768 return ;
677769 }
@@ -681,19 +773,38 @@ void PrometheusExporterUtils::SetMetricBasic(
681773 // We could sort the sanitized keys again, but this seems too expensive to do
682774 // in this hot code path. Instead, we ignore out-of-order keys and emit a warning.
683775 metric.label .reserve (labels.size () + 2 );
776+
777+ struct LabelCollisionEntry
778+ {
779+ std::size_t label_index;
780+ std::string original_key;
781+ std::string value;
782+ };
783+ std::vector<LabelCollisionEntry> colliding_scope_entries;
784+
684785 std::string previous_key;
685786 for (auto const &label : labels)
686787 {
687788 auto sanitized = SanitizeLabel (label.first );
688789 int comparison = previous_key.compare (sanitized);
689790 if (metric.label .empty () || comparison < 0 ) // new key
690791 {
691- previous_key = sanitized;
692- metric.label .push_back ({sanitized, AttributeValueToString (label.second )});
792+ previous_key = sanitized;
793+ std::string val = AttributeValueToString (label.second );
794+ if (scope != nullptr && (sanitized == kScopeNameKey || sanitized == kScopeVersionKey ))
795+ {
796+ colliding_scope_entries.push_back ({metric.label .size (), label.first , val});
797+ }
798+ metric.label .push_back ({sanitized, std::move (val)});
693799 }
694800 else if (comparison == 0 ) // key collision after sanitation
695801 {
696- metric.label .back ().value += " ;" + AttributeValueToString (label.second );
802+ std::string val = AttributeValueToString (label.second );
803+ if (scope != nullptr && (sanitized == kScopeNameKey || sanitized == kScopeVersionKey ))
804+ {
805+ colliding_scope_entries.push_back ({metric.label .size () - 1 , label.first , val});
806+ }
807+ metric.label .back ().value += " ;" + val;
697808 }
698809 else // order inversion introduced by sanitation
699810 {
@@ -708,19 +819,53 @@ void PrometheusExporterUtils::SetMetricBasic(
708819 {
709820 return ;
710821 }
711- auto scope_name = scope->GetName ();
822+
823+ auto add_or_merge_scope_label = [&metric, &colliding_scope_entries](
824+ const std::string &sanitized_key,
825+ const std::string &original_key, const std::string &value) {
826+ for (const auto &entry : colliding_scope_entries)
827+ {
828+ if (metric.label [entry.label_index ].name == sanitized_key)
829+ {
830+ const std::size_t target_index = entry.label_index ;
831+ colliding_scope_entries.push_back ({target_index, original_key, value});
832+ std::vector<const LabelCollisionEntry *> matched;
833+ for (const auto &candidate : colliding_scope_entries)
834+ {
835+ if (candidate.label_index == target_index)
836+ {
837+ matched.push_back (&candidate);
838+ }
839+ }
840+ std::stable_sort (matched.begin (), matched.end (),
841+ [](const LabelCollisionEntry *a, const LabelCollisionEntry *b) {
842+ return a->original_key < b->original_key ;
843+ });
844+ std::string merged;
845+ for (std::size_t j = 0 ; j < matched.size (); ++j)
846+ {
847+ if (j > 0 )
848+ {
849+ merged.push_back (' ;' );
850+ }
851+ merged.append (matched[j]->value );
852+ }
853+ metric.label [target_index].value = std::move (merged);
854+ return ;
855+ }
856+ }
857+ metric.label .push_back ({sanitized_key, value});
858+ };
859+
860+ const auto &scope_name = scope->GetName ();
712861 if (!scope_name.empty ())
713862 {
714- metric.label .emplace_back ();
715- metric.label .back ().name = kScopeNameKey ;
716- metric.label .back ().value = std::move (scope_name);
863+ add_or_merge_scope_label (kScopeNameKey , kScopeNameKey , scope_name);
717864 }
718- auto scope_version = scope->GetVersion ();
865+ const auto & scope_version = scope->GetVersion ();
719866 if (!scope_version.empty ())
720867 {
721- metric.label .emplace_back ();
722- metric.label .back ().name = kScopeVersionKey ;
723- metric.label .back ().value = std::move (scope_version);
868+ add_or_merge_scope_label (kScopeVersionKey , kScopeVersionKey , scope_version);
724869 }
725870}
726871
@@ -756,6 +901,75 @@ std::string PrometheusExporterUtils::AttributeValueToString(
756901 {
757902 result = nostd::get<std::string>(value);
758903 }
904+ else if (nostd::holds_alternative<std::vector<bool >>(value))
905+ {
906+ const auto &vec = nostd::get<std::vector<bool >>(value);
907+ result.push_back (' [' );
908+ for (std::size_t i = 0 ; i < vec.size (); ++i)
909+ {
910+ if (i > 0 )
911+ {
912+ result.push_back (' ,' );
913+ }
914+ result.append (vec[i] ? " true" : " false" );
915+ }
916+ result.push_back (' ]' );
917+ }
918+ else if (nostd::holds_alternative<std::vector<int >>(value))
919+ {
920+ result = NumericVectorToJson (nostd::get<std::vector<int >>(value));
921+ }
922+ else if (nostd::holds_alternative<std::vector<int64_t >>(value))
923+ {
924+ result = NumericVectorToJson (nostd::get<std::vector<int64_t >>(value));
925+ }
926+ else if (nostd::holds_alternative<std::vector<unsigned int >>(value))
927+ {
928+ result = NumericVectorToJson (nostd::get<std::vector<unsigned int >>(value));
929+ }
930+ else if (nostd::holds_alternative<std::vector<uint64_t >>(value))
931+ {
932+ result = NumericVectorToJson (nostd::get<std::vector<uint64_t >>(value));
933+ }
934+ else if (nostd::holds_alternative<std::vector<double >>(value))
935+ {
936+ const auto &vec = nostd::get<std::vector<double >>(value);
937+ result.push_back (' [' );
938+ for (std::size_t i = 0 ; i < vec.size (); ++i)
939+ {
940+ if (i > 0 )
941+ {
942+ result.push_back (' ,' );
943+ }
944+ if (std::isnan (vec[i]))
945+ {
946+ result.append (" \" NaN\" " );
947+ }
948+ else if (std::isinf (vec[i]))
949+ {
950+ result.append (vec[i] < 0 ? " \" -Infinity\" " : " \" Infinity\" " );
951+ }
952+ else
953+ {
954+ result.append (FormatDouble (vec[i]));
955+ }
956+ }
957+ result.push_back (' ]' );
958+ }
959+ else if (nostd::holds_alternative<std::vector<std::string>>(value))
960+ {
961+ const auto &vec = nostd::get<std::vector<std::string>>(value);
962+ result.push_back (' [' );
963+ for (std::size_t i = 0 ; i < vec.size (); ++i)
964+ {
965+ if (i > 0 )
966+ {
967+ result.push_back (' ,' );
968+ }
969+ result.append (EscapeJsonString (vec[i]));
970+ }
971+ result.push_back (' ]' );
972+ }
759973 else
760974 {
761975 OTEL_INTERNAL_LOG_WARN (
0 commit comments