YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
210k
  JSONWriter() : state_(kExpectKey), first_element_(true), in_array_(false) {
35
210k
    stream_ << "{";
36
210k
  }
37
38
2.00M
  void AddKey(const std::string& key) {
39
2.00M
    assert(state_ == kExpectKey);
40
2.00M
    if (!first_element_) {
41
1.73M
      stream_ << ", ";
42
1.73M
    }
43
2.00M
    stream_ << "\"" << key << "\": ";
44
2.00M
    state_ = kExpectValue;
45
2.00M
    first_element_ = false;
46
2.00M
  }
47
48
374k
  void AddValue(const char* value) {
49
374k
    assert(state_ == kExpectValue || state_ == kInArray);
50
374k
    if (state_ == kInArray && 
!first_element_0
) {
51
0
      stream_ << ", ";
52
0
    }
53
374k
    stream_ << "\"" << value << "\"";
54
374k
    if (
state_ != kInArray374k
) {
55
374k
      state_ = kExpectKey;
56
374k
    }
57
374k
    first_element_ = false;
58
374k
  }
59
60
  template <typename T>
61
1.75M
  void AddValue(const T& value) {
62
1.75M
    assert(state_ == kExpectValue || state_ == kInArray);
63
1.75M
    if (state_ == kInArray && 
!first_element_263k
) {
64
197k
      stream_ << ", ";
65
197k
    }
66
1.75M
    stream_ << value;
67
1.75M
    if (state_ != kInArray) {
68
1.49M
      state_ = kExpectKey;
69
1.49M
    }
70
1.75M
    first_element_ = false;
71
1.75M
  }
void rocksdb::JSONWriter::AddValue<long long>(long long const&)
Line
Count
Source
61
223k
  void AddValue(const T& value) {
62
223k
    assert(state_ == kExpectValue || state_ == kInArray);
63
223k
    if (state_ == kInArray && 
!first_element_0
) {
64
0
      stream_ << ", ";
65
0
    }
66
223k
    stream_ << value;
67
223k
    if (
state_ != kInArray223k
) {
68
223k
      state_ = kExpectKey;
69
223k
    }
70
223k
    first_element_ = false;
71
223k
  }
void rocksdb::JSONWriter::AddValue<int>(int const&)
Line
Count
Source
61
451k
  void AddValue(const T& value) {
62
451k
    assert(state_ == kExpectValue || state_ == kInArray);
63
451k
    if (state_ == kInArray && 
!first_element_204k
) {
64
164k
      stream_ << ", ";
65
164k
    }
66
451k
    stream_ << value;
67
451k
    if (state_ != kInArray) {
68
246k
      state_ = kExpectKey;
69
246k
    }
70
451k
    first_element_ = false;
71
451k
  }
void rocksdb::JSONWriter::AddValue<unsigned long long>(unsigned long long const&)
Line
Count
Source
61
993k
  void AddValue(const T& value) {
62
993k
    assert(state_ == kExpectValue || state_ == kInArray);
63
993k
    if (state_ == kInArray && 
!first_element_58.4k
) {
64
32.3k
      stream_ << ", ";
65
32.3k
    }
66
993k
    stream_ << value;
67
993k
    if (state_ != kInArray) {
68
934k
      state_ = kExpectKey;
69
934k
    }
70
993k
    first_element_ = false;
71
993k
  }
void rocksdb::JSONWriter::AddValue<unsigned long>(unsigned long const&)
Line
Count
Source
61
79.8k
  void AddValue(const T& value) {
62
79.8k
    assert(state_ == kExpectValue || state_ == kInArray);
63
79.8k
    if (state_ == kInArray && 
!first_element_0
) {
64
0
      stream_ << ", ";
65
0
    }
66
79.8k
    stream_ << value;
67
79.8k
    if (
state_ != kInArray79.8k
) {
68
79.8k
      state_ = kExpectKey;
69
79.8k
    }
70
79.8k
    first_element_ = false;
71
79.8k
  }
void rocksdb::JSONWriter::AddValue<double>(double const&)
Line
Count
Source
61
11.4k
  void AddValue(const T& value) {
62
11.4k
    assert(state_ == kExpectValue || state_ == kInArray);
63
11.4k
    if (state_ == kInArray && 
!first_element_0
) {
64
0
      stream_ << ", ";
65
0
    }
66
11.4k
    stream_ << value;
67
11.4k
    if (state_ != kInArray) {
68
11.4k
      state_ = kExpectKey;
69
11.4k
    }
70
11.4k
    first_element_ = false;
71
11.4k
  }
72
73
78.7k
  void StartArray() {
74
78.7k
    assert(state_ == kExpectValue);
75
0
    state_ = kInArray;
76
78.7k
    in_array_ = true;
77
78.7k
    stream_ << "[";
78
78.7k
    first_element_ = true;
79
78.7k
  }
80
81
78.7k
  void EndArray() {
82
78.7k
    assert(state_ == kInArray);
83
0
    state_ = kExpectKey;
84
78.7k
    in_array_ = false;
85
78.7k
    stream_ << "]";
86
78.7k
    first_element_ = false;
87
78.7k
  }
88
89
55.0k
  void StartObject() {
90
55.0k
    assert(state_ == kExpectValue);
91
0
    state_ = kExpectKey;
92
55.0k
    stream_ << "{";
93
55.0k
    first_element_ = true;
94
55.0k
  }
95
96
265k
  void EndObject() {
97
265k
    assert(state_ == kExpectKey);
98
0
    stream_ << "}";
99
265k
    first_element_ = false;
100
265k
  }
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
210k
  std::string Get() const { return stream_.str(); }
118
119
2.37M
  JSONWriter& operator<<(const char* val) {
120
2.37M
    if (state_ == kExpectKey) {
121
2.00M
      AddKey(val);
122
2.00M
    } else {
123
374k
      AddValue(val);
124
374k
    }
125
2.37M
    return *this;
126
2.37M
  }
127
128
248k
  JSONWriter& operator<<(const std::string& val) {
129
248k
    return *this << val.c_str();
130
248k
  }
131
132
  template <typename T>
133
1.75M
  JSONWriter& operator<<(const T& val) {
134
1.75M
    assert(state_ != kExpectKey);
135
0
    AddValue(val);
136
1.75M
    return *this;
137
1.75M
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<<<long long>(long long const&)
Line
Count
Source
133
223k
  JSONWriter& operator<<(const T& val) {
134
223k
    assert(state_ != kExpectKey);
135
0
    AddValue(val);
136
223k
    return *this;
137
223k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<<<int>(int const&)
Line
Count
Source
133
451k
  JSONWriter& operator<<(const T& val) {
134
451k
    assert(state_ != kExpectKey);
135
0
    AddValue(val);
136
451k
    return *this;
137
451k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<<<unsigned long long>(unsigned long long const&)
Line
Count
Source
133
993k
  JSONWriter& operator<<(const T& val) {
134
993k
    assert(state_ != kExpectKey);
135
0
    AddValue(val);
136
993k
    return *this;
137
993k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<<<unsigned long>(unsigned long const&)
Line
Count
Source
133
79.8k
  JSONWriter& operator<<(const T& val) {
134
79.8k
    assert(state_ != kExpectKey);
135
0
    AddValue(val);
136
79.8k
    return *this;
137
79.8k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<<<double>(double const&)
Line
Count
Source
133
11.4k
  JSONWriter& operator<<(const T& val) {
134
11.4k
    assert(state_ != kExpectKey);
135
0
    AddValue(val);
136
11.4k
    return *this;
137
11.4k
  }
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.48M
  EventLoggerStream& operator<<(const T& val) {
156
1.48M
    MakeStream();
157
1.48M
    *json_writer_ << val;
158
1.48M
    return *this;
159
1.48M
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [12]>(char const (&) [12])
Line
Count
Source
155
163k
  EventLoggerStream& operator<<(const T& val) {
156
163k
    MakeStream();
157
163k
    *json_writer_ << val;
158
163k
    return *this;
159
163k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<long long>(long long const&)
Line
Count
Source
155
118k
  EventLoggerStream& operator<<(const T& val) {
156
118k
    MakeStream();
157
118k
    *json_writer_ << val;
158
118k
    return *this;
159
118k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [4]>(char const (&) [4])
Line
Count
Source
155
106k
  EventLoggerStream& operator<<(const T& val) {
156
106k
    MakeStream();
157
106k
    *json_writer_ << val;
158
106k
    return *this;
159
106k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<int>(int const&)
Line
Count
Source
155
347k
  EventLoggerStream& operator<<(const T& val) {
156
347k
    MakeStream();
157
347k
    *json_writer_ << val;
158
347k
    return *this;
159
347k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [6]>(char const (&) [6])
Line
Count
Source
155
129k
  EventLoggerStream& operator<<(const T& val) {
156
129k
    MakeStream();
157
129k
    *json_writer_ << val;
158
129k
    return *this;
159
129k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [20]>(char const (&) [20])
Line
Count
Source
155
11.4k
  EventLoggerStream& operator<<(const T& val) {
156
11.4k
    MakeStream();
157
11.4k
    *json_writer_ << val;
158
11.4k
    return *this;
159
11.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [23]>(char const (&) [23])
Line
Count
Source
155
11.4k
  EventLoggerStream& operator<<(const T& val) {
156
11.4k
    MakeStream();
157
11.4k
    *json_writer_ << val;
158
11.4k
    return *this;
159
11.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<unsigned long long>(unsigned long long const&)
Line
Count
Source
155
172k
  EventLoggerStream& operator<<(const T& val) {
156
172k
    MakeStream();
157
172k
    *json_writer_ << val;
158
172k
    return *this;
159
172k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [13]>(char const (&) [13])
Line
Count
Source
155
52.2k
  EventLoggerStream& operator<<(const T& val) {
156
52.2k
    MakeStream();
157
52.2k
    *json_writer_ << val;
158
52.2k
    return *this;
159
52.2k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [17]>(char const (&) [17])
Line
Count
Source
155
30.8k
  EventLoggerStream& operator<<(const T& val) {
156
30.8k
    MakeStream();
157
30.8k
    *json_writer_ << val;
158
30.8k
    return *this;
159
30.8k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<unsigned long>(unsigned long const&)
Line
Count
Source
155
79.8k
  EventLoggerStream& operator<<(const T& val) {
156
79.8k
    MakeStream();
157
79.8k
    *json_writer_ << val;
158
79.8k
    return *this;
159
79.8k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [18]>(char const (&) [18])
Line
Count
Source
155
42.0k
  EventLoggerStream& operator<<(const T& val) {
156
42.0k
    MakeStream();
157
42.0k
    *json_writer_ << val;
158
42.0k
    return *this;
159
42.0k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [19]>(char const (&) [19])
Line
Count
Source
155
34.2k
  EventLoggerStream& operator<<(const T& val) {
156
34.2k
    MakeStream();
157
34.2k
    *json_writer_ << val;
158
34.2k
    return *this;
159
34.2k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [22]>(char const (&) [22])
Line
Count
Source
155
48
  EventLoggerStream& operator<<(const T& val) {
156
48
    MakeStream();
157
48
    *json_writer_ << val;
158
48
    return *this;
159
48
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [25]>(char const (&) [25])
Line
Count
Source
155
48
  EventLoggerStream& operator<<(const T& val) {
156
48
    MakeStream();
157
48
    *json_writer_ << val;
158
48
    return *this;
159
48
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [10]>(char const (&) [10])
Line
Count
Source
155
46.9k
  EventLoggerStream& operator<<(const T& val) {
156
46.9k
    MakeStream();
157
46.9k
    *json_writer_ << val;
158
46.9k
    return *this;
159
46.9k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
155
31.7k
  EventLoggerStream& operator<<(const T& val) {
156
31.7k
    MakeStream();
157
31.7k
    *json_writer_ << val;
158
31.7k
    return *this;
159
31.7k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<double>(double const&)
Line
Count
Source
155
11.4k
  EventLoggerStream& operator<<(const T& val) {
156
11.4k
    MakeStream();
157
11.4k
    *json_writer_ << val;
158
11.4k
    return *this;
159
11.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [16]>(char const (&) [16])
Line
Count
Source
155
11.4k
  EventLoggerStream& operator<<(const T& val) {
156
11.4k
    MakeStream();
157
11.4k
    *json_writer_ << val;
158
11.4k
    return *this;
159
11.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [15]>(char const (&) [15])
Line
Count
Source
155
28.5k
  EventLoggerStream& operator<<(const T& val) {
156
28.5k
    MakeStream();
157
28.5k
    *json_writer_ << val;
158
28.5k
    return *this;
159
28.5k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<<<char [14]>(char const (&) [14])
Line
Count
Source
155
57.0k
  EventLoggerStream& operator<<(const T& val) {
156
57.0k
    MakeStream();
157
57.0k
    *json_writer_ << val;
158
57.0k
    return *this;
159
57.0k
  }
160
161
78.7k
  void StartArray() { json_writer_->StartArray(); }
162
78.7k
  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.48M
  void MakeStream() {
170
1.48M
    if (!json_writer_) {
171
106k
      json_writer_ = new JSONWriter();
172
106k
      *this << "time_micros"
173
106k
            << std::chrono::duration_cast<std::chrono::microseconds>(
174
106k
                   std::chrono::system_clock::now().time_since_epoch()).count();
175
106k
    }
176
1.48M
  }
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
210k
  static const char* Prefix() {
194
210k
    return "EVENT_LOG_v1";
195
210k
  }
196
197
435k
  explicit EventLogger(Logger* logger) : logger_(logger) {}
198
53.9k
  EventLoggerStream Log() { return EventLoggerStream(logger_); }
199
52.2k
  EventLoggerStream LogToBuffer(LogBuffer* log_buffer) {
200
52.2k
    return EventLoggerStream(log_buffer);
201
52.2k
  }
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