@@ -171,3 +171,72 @@ func testCountRequests(t *testing.T, measure metrics.Measure, category string) {
171171 })
172172 })
173173}
174+
175+ func TestRequestDuration (t * testing.T ) {
176+ t .Run ("records duration for non-streaming request" , func (t * testing.T ) {
177+ router := mux .NewRouter ()
178+ router .Use (RequestCount (metrics .ServerRequests ))
179+ router .Handle ("/duration-test" , nullHandler ()).Methods ("GET" )
180+
181+ metricsMiddlewareTest (t , func (p metricsMiddlewareTestParams ) {
182+ expectedTags := map [string ]string {
183+ "env" : p .envName ,
184+ "method" : "GET" ,
185+ "route" : "_duration-test" ,
186+ "platformCategory" : "server" ,
187+ "userAgent" : metricsTestUserAgent ,
188+ "sdkWrapper" : "not-provided" ,
189+ }
190+
191+ req , _ := http .NewRequest ("GET" , "/duration-test" , nil )
192+ req .Header .Set ("User-Agent" , metricsTestUserAgent )
193+ req = req .WithContext (WithEnvContextInfo (req .Context (), EnvContextInfo {Env : p .env }))
194+ router .ServeHTTP (httptest .NewRecorder (), req )
195+
196+ p .exporter .AwaitData (t , time .Second , p .mockLog .Loggers , func (d st.TestMetricsData ) bool {
197+ return d .HasRowMatching ("request_duration" , expectedTags , func (row st.TestMetricsRow ) bool {
198+ return row .DistributionCount >= 1
199+ })
200+ })
201+ })
202+ })
203+
204+ t .Run ("does not record duration for streaming request" , func (t * testing.T ) {
205+ router := mux .NewRouter ()
206+ router .Use (RequestCount (metrics .ServerRequests ))
207+ // Handler that sets Content-Type: text/event-stream (like the streaming middleware does)
208+ router .Handle ("/stream-route" , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
209+ w .Header ().Set ("Content-Type" , "text/event-stream" )
210+ })).Methods ("GET" )
211+
212+ metricsMiddlewareTest (t , func (p metricsMiddlewareTestParams ) {
213+ streamTags := map [string ]string {
214+ "env" : p .envName ,
215+ "method" : "GET" ,
216+ "route" : "_stream-route" ,
217+ "platformCategory" : "server" ,
218+ "userAgent" : metricsTestUserAgent ,
219+ "sdkWrapper" : "not-provided" ,
220+ }
221+
222+ req , _ := http .NewRequest ("GET" , "/stream-route" , nil )
223+ req .Header .Set ("User-Agent" , metricsTestUserAgent )
224+ req = req .WithContext (WithEnvContextInfo (req .Context (), EnvContextInfo {Env : p .env }))
225+ router .ServeHTTP (httptest .NewRecorder (), req )
226+
227+ // The request count should still be recorded
228+ p .exporter .AwaitData (t , time .Second , p .mockLog .Loggers , func (d st.TestMetricsData ) bool {
229+ return d .HasRow ("requests" , st.TestMetricsRow {
230+ Tags : streamTags ,
231+ Count : 1 ,
232+ })
233+ })
234+
235+ // But there should be no duration data for this specific route
236+ lastData := p .exporter .GetLastData ()
237+ require .False (t , lastData .HasRowMatching ("request_duration" , streamTags , func (st.TestMetricsRow ) bool {
238+ return true
239+ }), "streaming request should not have duration recorded" )
240+ })
241+ })
242+ }
0 commit comments