@@ -3,7 +3,7 @@ use std::collections::HashMap;
33use prost:: Message ;
44
55use crate :: backend:: types:: { EncodedReport , Report } ;
6- use crate :: encode:: profiles:: { Function , Line , Location , Profile , Sample , ValueType } ;
6+ use crate :: encode:: profiles:: { Function , Label , Line , Location , Profile , Sample , ValueType } ;
77
88
99struct PProfBuilder {
@@ -68,7 +68,7 @@ impl PProfBuilder {
6868 }
6969}
7070
71- pub fn encode ( reports : Vec < Report > ) -> Vec < EncodedReport > {
71+ pub fn encode ( reports : Vec < Report > , sample_rate : u32 , start_time_nanos : u64 , duration_nanos : u64 ) -> Vec < EncodedReport > {
7272 let mut b = PProfBuilder {
7373 strings : HashMap :: new ( ) ,
7474 functions : HashMap :: new ( ) ,
@@ -82,8 +82,8 @@ pub fn encode(reports: Vec<Report>) -> Vec<EncodedReport> {
8282 string_table : vec ! [ ] ,
8383 drop_frames : 0 ,
8484 keep_frames : 0 ,
85- time_nanos : 0 ,
86- duration_nanos : 0 ,
85+ time_nanos : start_time_nanos as i64 ,
86+ duration_nanos : duration_nanos as i64 ,
8787 period_type : None ,
8888 period : 0 ,
8989 comment : vec ! [ ] ,
@@ -93,10 +93,16 @@ pub fn encode(reports: Vec<Report>) -> Vec<EncodedReport> {
9393 {
9494 let count = b. add_string ( & "count" . to_string ( ) ) ;
9595 let samples = b. add_string ( & "samples" . to_string ( ) ) ;
96+ let milliseconds = b. add_string ( & "milliseconds" . to_string ( ) ) ;
9697 b. profile . sample_type . push ( ValueType {
9798 r#type : samples,
9899 unit : count,
99100 } ) ;
101+ b. profile . period = 1_000 / sample_rate as i64 ;
102+ b. profile . period_type = Some ( ValueType {
103+ r#type : 0 ,
104+ unit : milliseconds,
105+ } )
100106 }
101107 for report in & reports {
102108 for ( stacktrace, value) in & report. data {
@@ -115,6 +121,25 @@ pub fn encode(reports: Vec<Report>) -> Vec<EncodedReport> {
115121 let location_id = b. add_location ( function_id) ;
116122 sample. location_id . push ( location_id as u64 ) ;
117123 }
124+ let mut labels = HashMap :: new ( ) ;
125+ for l in & stacktrace. metadata . tags {
126+ let k = b. add_string ( & l. key ) ;
127+ let v = b. add_string ( & l. value ) ;
128+ labels. insert ( k, v) ;
129+ }
130+ for l in & report. metadata . tags {
131+ let k = b. add_string ( & l. key ) ;
132+ let v = b. add_string ( & l. value ) ;
133+ labels. insert ( k, v) ;
134+ }
135+ for ( k, v) in & labels {
136+ sample. label . push ( Label {
137+ key : * k,
138+ str : * v,
139+ num : 0 ,
140+ num_unit : 0 ,
141+ } )
142+ }
118143 b. profile . sample . push ( sample) ;
119144 }
120145 }
@@ -126,4 +151,4 @@ pub fn encode(reports: Vec<Report>) -> Vec<EncodedReport> {
126151 data: b. profile. encode_to_vec( ) ,
127152 metadata: Default :: default ( ) ,
128153 } ]
129- }
154+ }
0 commit comments