1+ // Copyright (c) Microsoft Corporation.
2+ // All rights reserved.
3+ //
4+ // This code is licensed under the MIT License.
5+ //
6+ // Permission is hereby granted, free of charge, to any person obtaining a copy
7+ // of this software and associated documentation files(the "Software"), to deal
8+ // in the Software without restriction, including without limitation the rights
9+ // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10+ // copies of the Software, and to permit persons to whom the Software is
11+ // furnished to do so, subject to the following conditions :
12+ //
13+ // The above copyright notice and this permission notice shall be included in
14+ // all copies or substantial portions of the Software.
15+ //
16+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+ // THE SOFTWARE.
123package com .microsoft .identity .common .java .broker ;
224
25+ import com .microsoft .identity .common .java .logging .Logger ;
26+
327/**
428 * Holds broker performance metrics derived from timestamps in authentication flows.
529 * Calculates processing and latency durations at construction time.
630 */
731public class BrokerPerformanceMetrics {
32+ private static final String TAG = BrokerPerformanceMetrics .class .getSimpleName ();
33+
834 /**
935 * Time spent by broker processing the request (in milliseconds).
1036 * Calculated as: brokerResponseGenerationTimestamp - brokerRequestReceivedTimestamp
1137 */
12- private final Long brokerHandlingTime ;
38+ private final long brokerHandlingTime ;
1339
1440 /**
1541 * Time for the response to reach and be processed by the client (in milliseconds).
1642 * Calculated as: currentTime - brokerResponseGenerationTimestamp
1743 */
18- private final Long responseLatency ;
44+ private final long responseLatency ;
1945
2046
2147 /**
2248 * Timestamp when the broker received the authentication request (epoch milliseconds).
2349 * Used to calculate broker handling time.
2450 */
25- private final Long brokerRequestReceivedTimestamp ;
51+ private final long brokerRequestReceivedTimestamp ;
2652
2753 /**
2854 * Timestamp when the broker generated the authentication response (epoch milliseconds).
2955 * Used to calculate both broker handling time and response latency.
3056 */
31- private final Long brokerResponseGenerationTimestamp ;
57+ private final long brokerResponseGenerationTimestamp ;
3258
3359 /**
3460 * Creates broker performance metrics with calculated durations.
3561 *
3662 * @param brokerRequestReceivedTimestamp When broker received the request (epoch millis)
3763 * @param brokerResponseGenerationTimestamp When broker generated the response (epoch millis)
3864 */
39- public BrokerPerformanceMetrics (final Long brokerRequestReceivedTimestamp ,
40- final Long brokerResponseGenerationTimestamp ) {
65+ public BrokerPerformanceMetrics (final long brokerRequestReceivedTimestamp ,
66+ final long brokerResponseGenerationTimestamp ) {
4167 this .brokerRequestReceivedTimestamp = brokerRequestReceivedTimestamp ;
4268 this .brokerResponseGenerationTimestamp = brokerResponseGenerationTimestamp ;
43- this .brokerHandlingTime = brokerResponseGenerationTimestamp - brokerRequestReceivedTimestamp ;
69+ // Validation to avoid negative durations due to clock skew or invalid timestamps
70+ if (brokerResponseGenerationTimestamp < brokerRequestReceivedTimestamp ) {
71+ Logger .warn (TAG , "Invalid timestamps: response before request" );
72+ this .brokerHandlingTime = 0 ;
73+ } else {
74+ this .brokerHandlingTime = brokerResponseGenerationTimestamp - brokerRequestReceivedTimestamp ;
75+ }
4476 this .responseLatency = System .currentTimeMillis () - brokerResponseGenerationTimestamp ;
4577 }
4678
47- public Long getBrokerHandlingTime () {
79+ public long getBrokerHandlingTime () {
4880 return brokerHandlingTime ;
4981 }
5082
51- public Long getBrokerRequestReceivedTimestamp () {
83+ public long getBrokerRequestReceivedTimestamp () {
5284 return brokerRequestReceivedTimestamp ;
5385 }
5486
55- public Long getBrokerResponseGenerationTimestamp () {
87+ public long getBrokerResponseGenerationTimestamp () {
5688 return brokerResponseGenerationTimestamp ;
5789 }
5890
59- public Long getResponseLatency () {
91+ public long getResponseLatency () {
6092 return responseLatency ;
6193 }
6294
@@ -65,6 +97,8 @@ public String toString() {
6597 return "BrokerPerformanceMetrics{" +
6698 "brokerHandlingTime=" + brokerHandlingTime +
6799 ", responseLatency=" + responseLatency +
68- ", brokerRequestReceivedTimestamp=" + brokerRequestReceivedTimestamp ;
100+ ", brokerRequestReceivedTimestamp=" + brokerRequestReceivedTimestamp +
101+ ", brokerResponseGenerationTimestamp=" + brokerResponseGenerationTimestamp +
102+ '}' ;
69103 }
70104}
0 commit comments