Add metrics to the framework
[ric-plt/xapp-frame-cpp.git] / src / metrics / metrics.hpp
1 // vi: ts=4 sw=4 noet:
2 /*
3 ==================================================================================
4         Copyright (c) 2020 Nokia
5         Copyright (c) 2020 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       metrics.hpp
23         Abstract:       Headers for the metrics class.
24                                 This class provides a simple interface for an xAPP to compose and
25                                 send metrics. The class allows the xAPP to name the metric and to
26                                 provide one or more key/value pairs which are sent to the metrics
27                                 gateway (munchkin) when Send() is invoked. The class hides the
28                                 true message structure that the munchkin desires from the xAPP.
29
30                                 The munchkin endpoint is assumed to be a routable target and thus
31                                 normal RMR messages (with appropriate type) are sent.
32
33         Date:           20 July 2020
34         Author:         E. Scott Daniels
35 */
36
37 #ifndef _XAPP_METRICS_HPP
38 #define _XAPP_METRICS_HPP
39
40
41 #include <iostream>
42 #include <string>
43
44 #include "msg_component.hpp"
45
46 namespace xapp {
47
48 // ------------------------------------------------------------------------
49
50 /*
51 This class is loosely tied to the json expected by the munchkin. See the munchkin
52 documentation for explicit information, but this will help to understand a bit:
53         {
54                 "reporter": "<string>",
55                 "generator" "<string>",
56                 "timestamp": double,
57                 "data": [
58                         {
59                                 "id": "<string>",
60                                 "type": "<string>",
61                                 "value": double
62                         },...
63                 ]
64         }
65 */
66
67 class Metrics {
68         private:
69                 std::shared_ptr<Message> msg;           // message to send
70
71                                                                                         // data for the payload
72                 std::string reporter;                           // this application as it's reporting the metrics
73                 std::string source;                                     // source of the measurement if not the reporter
74                 std::string     data;                                   // key/value/type tuples "pushed"  waiting for send
75
76
77                 int build_payload( xapp::Msg_component payload, int payload_len );
78
79         public:
80
81                 Metrics( std::shared_ptr<Message> msg );                                                                                // builders
82                 Metrics( std::shared_ptr<Message> msg, std::string msource );                                   // allow xapp to supply metric source
83                 Metrics( std::shared_ptr<Message> msg, std::string reporter, std::string msource ); // allow xapp to supply all
84
85                 Metrics( const Metrics& soi );                                  // copy to newly created instance
86                 Metrics& operator=( const Metrics& soi );               // copy operator
87                 Metrics( Metrics&& soi );                                               // mover
88                 Metrics& operator=( Metrics&& soi );                    // move operator
89                 ~Metrics();                                                                             // destroyer
90
91
92                 void Set_source( std::string new_source );
93                 void Set_reporter( std::string new_reporter );
94                 void Push_data( std::string key, double value );
95
96
97                 bool Send( );
98 };
99
100 } // namespace
101
102 #endif