1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 #include <memory>
34 #include <tuple>
35 #include <vector>
36 
37 #include "mongo/base/disallow_copying.h"
38 
39 namespace mongo {
40 
41 class BSONObj;
42 class BSONObjBuilder;
43 class Date_t;
44 class Client;
45 class OperationContext;
46 
47 /**
48  * BSON Collector interface
49  *
50  * Provides an interface to collect BSONObjs from system providers
51  */
52 class FTDCCollectorInterface {
53     MONGO_DISALLOW_COPYING(FTDCCollectorInterface);
54 
55 public:
56     virtual ~FTDCCollectorInterface() = default;
57 
58     /**
59      * Name of the collector
60      *
61      * Used to stamp before and after dates to measure time to collect.
62      */
63     virtual std::string name() const = 0;
64 
65     /**
66      * Collect a sample.
67      *
68      * If a collector fails to collect data, it should update builder with the result of the
69      * failure.
70      */
71     virtual void collect(OperationContext* opCtx, BSONObjBuilder& builder) = 0;
72 
73 protected:
74     FTDCCollectorInterface() = default;
75 };
76 
77 /**
78  * Manages the set of BSON collectors
79  *
80  * Not Thread-Safe. Locking is owner's responsibility.
81  */
82 class FTDCCollectorCollection {
83     MONGO_DISALLOW_COPYING(FTDCCollectorCollection);
84 
85 public:
86     FTDCCollectorCollection() = default;
87 
88     /**
89      * Add a metric collector to the collection.
90      * Must be called before collect. Cannot be called after collect is called.
91      */
92     void add(std::unique_ptr<FTDCCollectorInterface> collector);
93 
94     /**
95      * Collect a sample from all collectors. Called after all adding is complete.
96      * Returns a tuple of a sample, and the time at which collecting started.
97      *
98      * Sample schema:
99      * {
100      *    "start" : Date_t,    <- Time at which all collecting started
101      *    "name" : {           <- name is from name() in FTDCCollectorInterface
102      *       "start" : Date_t, <- Time at which name() collection started
103      *       "data" : { ... }  <- data comes from collect() in FTDCCollectorInterface
104      *       "end" : Date_t,   <- Time at which name() collection ended
105      *    },
106      *    ...
107      *    "end" : Date_t,      <- Time at which all collecting ended
108      * }
109      */
110     std::tuple<BSONObj, Date_t> collect(Client* client);
111 
112 private:
113     // collection of collectors
114     std::vector<std::unique_ptr<FTDCCollectorInterface>> _collectors;
115 };
116 
117 }  // namespace mongo
118