1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef SERVICES_NETWORK_TRUST_TOKENS_TRUST_TOKEN_OPERATION_METRICS_RECORDER_H_
6 #define SERVICES_NETWORK_TRUST_TOKENS_TRUST_TOKEN_OPERATION_METRICS_RECORDER_H_
7 
8 #include "base/time/time.h"
9 #include "services/network/public/mojom/trust_tokens.mojom.h"
10 
11 namespace network {
12 
13 namespace internal {
14 
15 // The following templates, which allow constructing names for the various Trust
16 // Tokens timing histograms, are exposed for testing.
17 extern const char kTrustTokenServerTimeHistogramNameBase[];
18 extern const char kTrustTokenTotalTimeHistogramNameBase[];
19 extern const char kTrustTokenFinalizeTimeHistogramNameBase[];
20 extern const char kTrustTokenBeginTimeHistogramNameBase[];
21 
22 }  // namespace internal
23 
24 // A TrustTokenOperationMetricsRecorder records timing and success metrics for a
25 // single Trust Tokens operation. To use, call BeginBegin at the time the Begin
26 // (outbound) part of the operation starts and FinishBegin at the time the Begin
27 // part finishes; if the Begin part was successful, call BeginFinalize and
28 // FinishFinalize analogously during the Finalize (inbound) part of the
29 // operation.
30 class TrustTokenOperationMetricsRecorder final {
31  public:
32   TrustTokenOperationMetricsRecorder() = default;
33   ~TrustTokenOperationMetricsRecorder() = default;
34 
35   TrustTokenOperationMetricsRecorder(
36       const TrustTokenOperationMetricsRecorder&) = delete;
37   TrustTokenOperationMetricsRecorder& operator=(
38       const TrustTokenOperationMetricsRecorder&) = delete;
39 
40   void BeginBegin(mojom::TrustTokenOperationType type);
41   void FinishBegin(mojom::TrustTokenOperationStatus status);
42 
43   void BeginFinalize();
44   void FinishFinalize(mojom::TrustTokenOperationStatus status);
45 
46  private:
47   mojom::TrustTokenOperationType type_;
48 
49   // Start and end times for the Begin part of the operation:
50   base::TimeTicks begin_start_;
51   base::TimeTicks begin_end_;
52 
53   // Start time for the Finalize part of the operation:
54   base::TimeTicks finalize_start_;
55 };
56 
57 // HistogramTrustTokenOperationNetError logs a //net error code corresponding to
58 // a Trust Tokens operation. This is a temporary measure for helping understand
59 // why "Failed to fetch" errors occur quite often in live testing: see
60 // https://crbug.com/1128174.
61 void HistogramTrustTokenOperationNetError(
62     network::mojom::TrustTokenOperationType type,
63     network::mojom::TrustTokenOperationStatus status,
64     int net_error);
65 
66 }  // namespace network
67 
68 #endif  // SERVICES_NETWORK_TRUST_TOKENS_TRUST_TOKEN_OPERATION_METRICS_RECORDER_H_
69