YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/event_logger.h
Line
Count
Source (jump to first uncovered line)
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
21
#pragma once
22
23
#include <memory>
24
#include <sstream>
25
#include <string>
26
27
#include "yb/rocksdb/env.h"
28
#include "yb/rocksdb/util/log_buffer.h"
29
30
namespace rocksdb {
31
32
class JSONWriter {
33
 public:
34
194k
  JSONWriter() : state_(kExpectKey), first_element_(true), in_array_(false) {
35
194k
    stream_ << "{";
36
194k
  }
37
38
1.85M
  void AddKey(const std::string& key) {
39
1.85M
    assert(state_ == kExpectKey);
40
1.85M
    if (!first_element_) {
41
1.60M
      stream_ << ", ";
42
1.60M
    }
43
1.85M
    stream_ << "\"" << key << "\": ";
44
1.85M
    state_ = kExpectValue;
45
1.85M
    first_element_ = false;
46
1.85M
  }
47
48
345k
  void AddValue(const char* value) {
49
345k
    assert(state_ == kExpectValue || state_ == kInArray);
50
345k
    if (state_ == kInArray && !first_element_) {
51
0
      stream_ << ", ";
52
0
    }
53
345k
    stream_ << "\"" << value << "\"";
54
345k
    if (state_ != kInArray) {
55
345k
      state_ = kExpectKey;
56
345k
    }
57
345k
    first_element_ = false;
58
345k
  }
59
60
  template <typename T>
61
1.63M
  void AddValue(const T& value) {
62
1.63M
    assert(state_ == kExpectValue || state_ == kInArray);
63
1.63M
    if (state_ == kInArray && !first_element_) {
64
195k
      stream_ << ", ";
65
195k
    }
66
1.63M
    stream_ << value;
67
1.63M
    if (state_ != kInArray) {
68
1.38M
      state_ = kExpectKey;
69
1.38M
    }
70
1.63M
    first_element_ = false;
71
1.63M
  }
_ZN7rocksdb10JSONWriter8AddValueIxEEvRKT_
Line
Count
Source
61
207k
  void AddValue(const T& value) {
62
207k
    assert(state_ == kExpectValue || state_ == kInArray);
63
207k
    if (state_ == kInArray && !first_element_) {
64
0
      stream_ << ", ";
65
0
    }
66
207k
    stream_ << value;
67
207k
    if (state_ != kInArray) {
68
207k
      state_ = kExpectKey;
69
207k
    }
70
207k
    first_element_ = false;
71
207k
  }
_ZN7rocksdb10JSONWriter8AddValueIiEEvRKT_
Line
Count
Source
61
430k
  void AddValue(const T& value) {
62
430k
    assert(state_ == kExpectValue || state_ == kInArray);
63
430k
    if (state_ == kInArray && !first_element_) {
64
165k
      stream_ << ", ";
65
165k
    }
66
430k
    stream_ << value;
67
430k
    if (state_ != kInArray) {
68
230k
      state_ = kExpectKey;
69
230k
    }
70
430k
    first_element_ = false;
71
430k
  }
_ZN7rocksdb10JSONWriter8AddValueIyEEvRKT_
Line
Count
Source
61
916k
  void AddValue(const T& value) {
62
916k
    assert(state_ == kExpectValue || state_ == kInArray);
63
916k
    if (state_ == kInArray && !first_element_) {
64
30.5k
      stream_ << ", ";
65
30.5k
    }
66
916k
    stream_ << value;
67
916k
    if (state_ != kInArray) {
68
860k
      state_ = kExpectKey;
69
860k
    }
70
916k
    first_element_ = false;
71
916k
  }
_ZN7rocksdb10JSONWriter8AddValueImEEvRKT_
Line
Count
Source
61
71.4k
  void AddValue(const T& value) {
62
71.4k
    assert(state_ == kExpectValue || state_ == kInArray);
63
71.4k
    if (state_ == kInArray && !first_element_) {
64
0
      stream_ << ", ";
65
0
    }
66
71.4k
    stream_ << value;
67
71.4k
    if (state_ != kInArray) {
68
71.4k
      state_ = kExpectKey;
69
71.4k
    }
70
71.4k
    first_element_ = false;
71
71.4k
  }
_ZN7rocksdb10JSONWriter8AddValueIdEEvRKT_
Line
Count
Source
61
10.6k
  void AddValue(const T& value) {
62
10.6k
    assert(state_ == kExpectValue || state_ == kInArray);
63
10.6k
    if (state_ == kInArray && !first_element_) {
64
0
      stream_ << ", ";
65
0
    }
66
10.6k
    stream_ << value;
67
10.6k
    if (state_ != kInArray) {
68
10.6k
      state_ = kExpectKey;
69
10.6k
    }
70
10.6k
    first_element_ = false;
71
10.6k
  }
72
73
73.6k
  void StartArray() {
74
73.6k
    assert(state_ == kExpectValue);
75
73.6k
    state_ = kInArray;
76
73.6k
    in_array_ = true;
77
73.6k
    stream_ << "[";
78
73.6k
    first_element_ = true;
79
73.6k
  }
80
81
73.6k
  void EndArray() {
82
73.6k
    assert(state_ == kInArray);
83
73.6k
    state_ = kExpectKey;
84
73.6k
    in_array_ = false;
85
73.6k
    stream_ << "]";
86
73.6k
    first_element_ = false;
87
73.6k
  }
88
89
50.7k
  void StartObject() {
90
50.7k
    assert(state_ == kExpectValue);
91
50.7k
    state_ = kExpectKey;
92
50.7k
    stream_ << "{";
93
50.7k
    first_element_ = true;
94
50.7k
  }
95
96
245k
  void EndObject() {
97
245k
    assert(state_ == kExpectKey);
98
245k
    stream_ << "}";
99
245k
    first_element_ = false;
100
245k
  }
101
102
0
  void StartArrayedObject() {
103
0
    assert(state_ == kInArray && in_array_);
104
0
    state_ = kExpectValue;
105
0
    if (!first_element_) {
106
0
      stream_ << ", ";
107
0
    }
108
0
    StartObject();
109
0
  }
110
111
0
  void EndArrayedObject() {
112
0
    assert(in_array_);
113
0
    EndObject();
114
0
    state_ = kInArray;
115
0
  }
116
117
194k
  std::string Get() const { return stream_.str(); }
118
119
2.19M
  JSONWriter& operator<<(const char* val) {
120
2.19M
    if (state_ == kExpectKey) {
121
1.85M
      AddKey(val);
122
345k
    } else {
123
345k
      AddValue(val);
124
345k
    }
125
2.19M
    return *this;
126
2.19M
  }
127
128
230k
  JSONWriter& operator<<(const std::string& val) {
129
230k
    return *this << val.c_str();
130
230k
  }
131
132
  template <typename T>
133
1.63M
  JSONWriter& operator<<(const T& val) {
134
1.63M
    assert(state_ != kExpectKey);
135
1.63M
    AddValue(val);
136
1.63M
    return *this;
137
1.63M
  }
_ZN7rocksdb10JSONWriterlsIxEERS0_RKT_
Line
Count
Source
133
207k
  JSONWriter& operator<<(const T& val) {
134
207k
    assert(state_ != kExpectKey);
135
207k
    AddValue(val);
136
207k
    return *this;
137
207k
  }
_ZN7rocksdb10JSONWriterlsIiEERS0_RKT_
Line
Count
Source
133
430k
  JSONWriter& operator<<(const T& val) {
134
430k
    assert(state_ != kExpectKey);
135
430k
    AddValue(val);
136
430k
    return *this;
137
430k
  }
_ZN7rocksdb10JSONWriterlsIyEERS0_RKT_
Line
Count
Source
133
916k
  JSONWriter& operator<<(const T& val) {
134
916k
    assert(state_ != kExpectKey);
135
916k
    AddValue(val);
136
916k
    return *this;
137
916k
  }
_ZN7rocksdb10JSONWriterlsImEERS0_RKT_
Line
Count
Source
133
71.4k
  JSONWriter& operator<<(const T& val) {
134
71.4k
    assert(state_ != kExpectKey);
135
71.4k
    AddValue(val);
136
71.4k
    return *this;
137
71.4k
  }
_ZN7rocksdb10JSONWriterlsIdEERS0_RKT_
Line
Count
Source
133
10.6k
  JSONWriter& operator<<(const T& val) {
134
10.6k
    assert(state_ != kExpectKey);
135
10.6k
    AddValue(val);
136
10.6k
    return *this;
137
10.6k
  }
138
139
 private:
140
  enum JSONWriterState {
141
    kExpectKey,
142
    kExpectValue,
143
    kInArray,
144
    kInArrayedObject,
145
  };
146
  JSONWriterState state_;
147
  bool first_element_;
148
  bool in_array_;
149
  std::ostringstream stream_;
150
};
151
152
class EventLoggerStream {
153
 public:
154
  template <typename T>
155
1.38M
  EventLoggerStream& operator<<(const T& val) {
156
1.38M
    MakeStream();
157
1.38M
    *json_writer_ << val;
158
1.38M
    return *this;
159
1.38M
  }
_ZN7rocksdb17EventLoggerStreamlsIA3_cEERS0_RKT_
Line
Count
Source
155
1
  EventLoggerStream& operator<<(const T& val) {
156
1
    MakeStream();
157
1
    *json_writer_ << val;
158
1
    return *this;
159
1
  }
_ZN7rocksdb17EventLoggerStreamlsIA12_cEERS0_RKT_
Line
Count
Source
155
147k
  EventLoggerStream& operator<<(const T& val) {
156
147k
    MakeStream();
157
147k
    *json_writer_ << val;
158
147k
    return *this;
159
147k
  }
_ZN7rocksdb17EventLoggerStreamlsIxEERS0_RKT_
Line
Count
Source
155
109k
  EventLoggerStream& operator<<(const T& val) {
156
109k
    MakeStream();
157
109k
    *json_writer_ << val;
158
109k
    return *this;
159
109k
  }
_ZN7rocksdb17EventLoggerStreamlsIiEERS0_RKT_
Line
Count
Source
155
333k
  EventLoggerStream& operator<<(const T& val) {
156
333k
    MakeStream();
157
333k
    *json_writer_ << val;
158
333k
    return *this;
159
333k
  }
_ZN7rocksdb17EventLoggerStreamlsIA6_cEERS0_RKT_
Line
Count
Source
155
120k
  EventLoggerStream& operator<<(const T& val) {
156
120k
    MakeStream();
157
120k
    *json_writer_ << val;
158
120k
    return *this;
159
120k
  }
_ZN7rocksdb17EventLoggerStreamlsIA13_cEERS0_RKT_
Line
Count
Source
155
48.0k
  EventLoggerStream& operator<<(const T& val) {
156
48.0k
    MakeStream();
157
48.0k
    *json_writer_ << val;
158
48.0k
    return *this;
159
48.0k
  }
_ZN7rocksdb17EventLoggerStreamlsIA4_cEERS0_RKT_
Line
Count
Source
155
97.2k
  EventLoggerStream& operator<<(const T& val) {
156
97.2k
    MakeStream();
157
97.2k
    *json_writer_ << val;
158
97.2k
    return *this;
159
97.2k
  }
_ZN7rocksdb17EventLoggerStreamlsIA20_cEERS0_RKT_
Line
Count
Source
155
10.6k
  EventLoggerStream& operator<<(const T& val) {
156
10.6k
    MakeStream();
157
10.6k
    *json_writer_ << val;
158
10.6k
    return *this;
159
10.6k
  }
_ZN7rocksdb17EventLoggerStreamlsIA23_cEERS0_RKT_
Line
Count
Source
155
10.6k
  EventLoggerStream& operator<<(const T& val) {
156
10.6k
    MakeStream();
157
10.6k
    *json_writer_ << val;
158
10.6k
    return *this;
159
10.6k
  }
_ZN7rocksdb17EventLoggerStreamlsIyEERS0_RKT_
Line
Count
Source
155
159k
  EventLoggerStream& operator<<(const T& val) {
156
159k
    MakeStream();
157
159k
    *json_writer_ << val;
158
159k
    return *this;
159
159k
  }
_ZN7rocksdb17EventLoggerStreamlsIA17_cEERS0_RKT_
Line
Count
Source
155
29.9k
  EventLoggerStream& operator<<(const T& val) {
156
29.9k
    MakeStream();
157
29.9k
    *json_writer_ << val;
158
29.9k
    return *this;
159
29.9k
  }
_ZN7rocksdb17EventLoggerStreamlsImEERS0_RKT_
Line
Count
Source
155
71.4k
  EventLoggerStream& operator<<(const T& val) {
156
71.4k
    MakeStream();
157
71.4k
    *json_writer_ << val;
158
71.4k
    return *this;
159
71.4k
  }
_ZN7rocksdb17EventLoggerStreamlsIA18_cEERS0_RKT_
Line
Count
Source
155
40.3k
  EventLoggerStream& operator<<(const T& val) {
156
40.3k
    MakeStream();
157
40.3k
    *json_writer_ << val;
158
40.3k
    return *this;
159
40.3k
  }
_ZN7rocksdb17EventLoggerStreamlsIA19_cEERS0_RKT_
Line
Count
Source
155
32.0k
  EventLoggerStream& operator<<(const T& val) {
156
32.0k
    MakeStream();
157
32.0k
    *json_writer_ << val;
158
32.0k
    return *this;
159
32.0k
  }
_ZN7rocksdb17EventLoggerStreamlsIA22_cEERS0_RKT_
Line
Count
Source
155
48
  EventLoggerStream& operator<<(const T& val) {
156
48
    MakeStream();
157
48
    *json_writer_ << val;
158
48
    return *this;
159
48
  }
_ZN7rocksdb17EventLoggerStreamlsIA25_cEERS0_RKT_
Line
Count
Source
155
48
  EventLoggerStream& operator<<(const T& val) {
156
48
    MakeStream();
157
48
    *json_writer_ << val;
158
48
    return *this;
159
48
  }
_ZN7rocksdb17EventLoggerStreamlsIA10_cEERS0_RKT_
Line
Count
Source
155
42.5k
  EventLoggerStream& operator<<(const T& val) {
156
42.5k
    MakeStream();
157
42.5k
    *json_writer_ << val;
158
42.5k
    return *this;
159
42.5k
  }
_ZN7rocksdb17EventLoggerStreamlsINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEERS0_RKT_
Line
Count
Source
155
31.1k
  EventLoggerStream& operator<<(const T& val) {
156
31.1k
    MakeStream();
157
31.1k
    *json_writer_ << val;
158
31.1k
    return *this;
159
31.1k
  }
_ZN7rocksdb17EventLoggerStreamlsIdEERS0_RKT_
Line
Count
Source
155
10.6k
  EventLoggerStream& operator<<(const T& val) {
156
10.6k
    MakeStream();
157
10.6k
    *json_writer_ << val;
158
10.6k
    return *this;
159
10.6k
  }
_ZN7rocksdb17EventLoggerStreamlsIA16_cEERS0_RKT_
Line
Count
Source
155
10.6k
  EventLoggerStream& operator<<(const T& val) {
156
10.6k
    MakeStream();
157
10.6k
    *json_writer_ << val;
158
10.6k
    return *this;
159
10.6k
  }
_ZN7rocksdb17EventLoggerStreamlsIA15_cEERS0_RKT_
Line
Count
Source
155
25.0k
  EventLoggerStream& operator<<(const T& val) {
156
25.0k
    MakeStream();
157
25.0k
    *json_writer_ << val;
158
25.0k
    return *this;
159
25.0k
  }
_ZN7rocksdb17EventLoggerStreamlsIA14_cEERS0_RKT_
Line
Count
Source
155
50.0k
  EventLoggerStream& operator<<(const T& val) {
156
50.0k
    MakeStream();
157
50.0k
    *json_writer_ << val;
158
50.0k
    return *this;
159
50.0k
  }
160
161
73.6k
  void StartArray() { json_writer_->StartArray(); }
162
73.6k
  void EndArray() { json_writer_->EndArray(); }
163
0
  void StartObject() { json_writer_->StartObject(); }
164
0
  void EndObject() { json_writer_->EndObject(); }
165
166
  ~EventLoggerStream();
167
168
 private:
169
1.38M
  void MakeStream() {
170
1.38M
    if (!json_writer_) {
171
97.2k
      json_writer_ = new JSONWriter();
172
97.2k
      *this << "time_micros"
173
97.2k
            << std::chrono::duration_cast<std::chrono::microseconds>(
174
97.2k
                   std::chrono::system_clock::now().time_since_epoch()).count();
175
97.2k
    }
176
1.38M
  }
177
  friend class EventLogger;
178
  explicit EventLoggerStream(Logger* logger);
179
  explicit EventLoggerStream(LogBuffer* log_buffer);
180
  // exactly one is non-nullptr
181
  Logger* const logger_;
182
  LogBuffer* const log_buffer_;
183
  // ownership
184
  JSONWriter* json_writer_;
185
};
186
187
// here is an example of the output that will show up in the LOG:
188
// 2015/01/15-14:13:25.788019 1105ef000 EVENT_LOG_v1 {"time_micros":
189
// 1421360005788015, "event": "table_file_creation", "file_number": 12,
190
// "file_size": 1909699}
191
class EventLogger {
192
 public:
193
194k
  static const char* Prefix() {
194
194k
    return "EVENT_LOG_v1";
195
194k
  }
196
197
341k
  explicit EventLogger(Logger* logger) : logger_(logger) {}
198
49.2k
  EventLoggerStream Log() { return EventLoggerStream(logger_); }
199
48.0k
  EventLoggerStream LogToBuffer(LogBuffer* log_buffer) {
200
48.0k
    return EventLoggerStream(log_buffer);
201
48.0k
  }
202
  void Log(const JSONWriter& jwriter);
203
  static void Log(Logger* logger, const JSONWriter& jwriter);
204
  static void LogToBuffer(LogBuffer* log_buffer, const JSONWriter& jwriter);
205
206
 private:
207
  Logger* logger_;
208
};
209
210
}  // namespace rocksdb