aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/eventlog/dumper/evlogdump.cpp
blob: 003b412265be6dcd5cc43e9bea58cb5b14b075df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "common.h"
#include "evlogdump.h"

#include <library/cpp/eventlog/evdecoder.h>
#include <library/cpp/eventlog/iterator.h>
#include <library/cpp/eventlog/logparser.h>

#include <library/cpp/getopt/last_getopt.h>

#ifndef NO_SVN_DEPEND
#include <library/cpp/svnversion/svnversion.h>
#endif

#include <util/datetime/base.h>
#include <util/generic/ptr.h>
#include <util/stream/file.h>
#include <util/string/cast.h>
#include <util/string/type.h>

using namespace NEventLog;
namespace nlg = NLastGetopt;

IFrameFilterRef CreateDurationFrameFilter(const TString& params) {
    TStringBuf d(params);
    TStringBuf l, r;
    d.Split(':', l, r);
    TDuration min = l.empty() ? TDuration::Zero() : FromString<TDuration>(l);
    TDuration max = r.empty() ? TDuration::Max() : FromString<TDuration>(r);

    if (min > TDuration::Zero() || max < TDuration::Max()) {
        return new TDurationFrameFilter(min.GetValue(), max.GetValue());
    }

    return nullptr;
}

IFrameFilterRef CreateFrameIdFilter(const ui32 frameId) {
    return new TFrameIdFrameFilter(frameId);
}

IFrameFilterRef CreateContainsEventFrameFilter(const TString& args, const IEventFactory* factory) {
    return new TContainsEventFrameFilter(args, factory);
}

void ListAllEvents(IEventFactory* factory, IOutputStream* out) {
    Y_ENSURE(out);

    for (TEventClass eventClass = factory->EventClassBegin(); eventClass < factory->EventClassEnd(); eventClass++) {
        THolder<TEvent> event(factory->CreateLogEvent(eventClass));

        const TStringBuf& name = event->GetName();
        if (name) {
            (*out) << name << "\n";
        }
    }
}

void ListEventFieldsByEventId(const TEventClass eventClass, IEventFactory* factory, IOutputStream* out) {
    THolder<TEvent> event(factory->CreateLogEvent(eventClass));
    const NProtoBuf::Message* message = event->GetProto();
    const google::protobuf::Descriptor* descriptor = message->GetDescriptor();

    (*out) << descriptor->DebugString();
}

void ListEventFields(const TString& eventName, IEventFactory* factory, IOutputStream* out) {
    Y_ENSURE(out);

    TEventClass eventClass;

    try {
        eventClass = factory->ClassByName(eventName);
    } catch (yexception& e) {
        if (!TryFromString<TEventClass>(eventName, eventClass)) {
            (*out) << "Cannot dervie event class from event name: " << eventName << Endl;
            return;
        }
    }

     ListEventFieldsByEventId(eventClass, factory, out);
}

int PrintHelpEvents(const TString& helpEvents, IEventFactory* factory) {
    if (helpEvents == "all") {
        ListAllEvents(factory, &Cout);
    } else if (IsNumber(helpEvents)) {
        ListEventFieldsByEventId(IntFromString<ui32, 10>(helpEvents), factory, &Cout);
    } else {
        ListEventFields(helpEvents, factory, &Cout);
    }

    return 0;
}

int IterateEventLog(IEventFactory* fac, IEventProcessor* proc, int argc, const char** argv) {
    class TProxy: public ITunableEventProcessor {
    public:
        TProxy(IEventProcessor* proc)
            : Processor_(proc)
        {
        }

        void AddOptions(NLastGetopt::TOpts&) override {
        }

        void SetOptions(const TEvent::TOutputOptions& options) override {
            Processor_->SetOptions(options);
        }

        void ProcessEvent(const TEvent* ev) override {
            Processor_->ProcessEvent(ev);
        }

        bool CheckedProcessEvent(const TEvent* ev) override {
            return Processor_->CheckedProcessEvent(ev);
        }

    private:
        IEventProcessor* Processor_ = nullptr;
    };

    TProxy proxy(proc);
    return IterateEventLog(fac, &proxy, argc, argv);
}

int IterateEventLog(IEventFactory* fac, ITunableEventProcessor* proc, int argc, const char** argv) {
    nlg::TOpts opts;
    opts.AddHelpOption('?');
    opts.AddHelpOption('h');
    opts.SetTitle(
        "Search EventLog dumper\n\n"
        "Examples:\n"
        "evlogdump -s 1228484839332219 -e 1228484840332219 event_log\n"
        "evlogdump -s 2008-12-12T12:00:00+03 -e 2008-12-12T12:05:00+03 event_log\n"
        "evlogdump -s 12:40 -e 12:55 event_log\n"
        "evlogdump -i 301,302,303 event_log\n"
        "evlogdump -i SubSourceResponse,SubSourceOk,SubSourceError event_log\n"
        "evlogdump -d 40ms:60ms event_log\n"
        "evlogdump -c ReqId:ReqId:1563185655856304-30407004790538173600035-vla1-0671-p1\n"
        "\n"
        "Fine manual: https://nda.ya.ru/3Tpo3L\n"
        "\n"
        "If in trouble using this (or bugs encountered),\n"
        "please don't hesitate to ask middlesearch/basesearch dev team\n"
        "(vmordovin@, mvel@, etc)\n");

    TString start;
    opts.AddLongOption(
            's', "start-time",
            "Start time (Unix time in microseconds, ISO8601, or HH:MM[:SS] in the last 24 hours).\n"
            "Long frames can produce inaccurate cropping and event loss in dump (see SEARCH-5576)")
        .Optional()
        .StoreResult(&start);

    TString end;
    opts.AddLongOption(
            'e', "end-time",
            "End time (Unix time in microseconds, ISO8601, or HH:MM[:SS] in the last 24 hours)")
        .Optional()
        .StoreResult(&end);

    TOptions o;

    opts.AddLongOption(
            'm', "max-request-duration",
            "Max duration of a request in the log, in us")
        .Optional()
        .StoreResult(&o.MaxRequestDuration);

    bool oneFrame = false;
    opts.AddLongOption(
            'f', "one-frame",
            "Treat input file as single frame dump (e.g. from gdb)")
        .NoArgument()
        .Optional()
        .StoreValue(&oneFrame, true);

    opts.AddLongOption(
            'v', "version",
            "Print program version")
        .NoArgument()
        .Optional()
        .Handler(&nlg::PrintVersionAndExit);

    TString includeEventList;
    opts.AddLongOption(
            'i', "event-list",
            "Comma-separated list of included event class IDs or names "
            "(e.g. 265,287 or MainTaskError,ContextCreated)")
        .Optional()
        .StoreResult(&includeEventList)
        .StoreValue(&o.EnableEvents, true);

    TString durationFilterStr;
    opts.AddLongOption(
            'd', "duration",
            "DurationMin[:DurationMax] (values must contain a unit, valid examples are: 50us, 50ms, 50s)\n"
            "(show frames with duration greater or equal DurationMin [and less or equal than DurationMax])")
        .Optional()
        .StoreResult(&durationFilterStr);

    const ui32 INVALID_FRAME_ID = ui32(-1);
    ui32 frameIdFilter = INVALID_FRAME_ID;
    opts.AddLongOption(
            'n', "frame-id",
            "Filter frame with given id\n")
        .Optional()
        .StoreResult(&frameIdFilter);

    TString excludeEventList;
    opts.AddLongOption(
            'x', "exclude-list",
            "Comma-separated list of excluded event class IDs or names (see also --event-list option)")
        .Optional()
        .StoreResult(&excludeEventList)
        .StoreValue(&o.EnableEvents, false);

    opts.AddLongOption(
            'o', "frame-order",
            "Order events by time only inside a frame (faster)")
        .NoArgument()
        .Optional()
        .StoreValue(&o.ForceWeakOrdering, true);

    opts.AddLongOption(
            'O', "global-order",
            "Globally but lossy (may lose some frames) order events by time only (slower)")
        .NoArgument()
        .Optional()
        .StoreValue(&o.ForceStrongOrdering, true);

    opts.AddLongOption(
        "lossless-strong-order",
        "Globally order events by time only (super slow and memory heavy)")
        .NoArgument()
        .Optional()
        .StoreValue(&o.ForceLosslessStrongOrdering, true)
        .StoreValue(&o.ForceStrongOrdering, true);

    opts.AddLongOption(
            'S', "stream",
            "Force stream mode (for log files with invalid ending)")
        .NoArgument()
        .Optional()
        .StoreValue(&o.ForceStreamMode, true);

    opts.AddLongOption(
            't', "tail",
            "Open file like `tail -f` does")
        .NoArgument()
        .Optional()
        .StoreValue(&o.TailFMode, true);

    bool unescaped = false;
    opts.AddLongOption(
            'u', "unescaped",
            "Don't escape \\t, \\n chars in message fields")
        .NoArgument()
        .StoreValue(&unescaped, true);

    bool json = false;
    opts.AddLongOption(
            'j', "json",
            "Print messages as JSON values")
        .NoArgument()
        .StoreValue(&json, true);

    TEvent::TOutputOptions outputOptions;
    opts.AddLongOption(
            'r', "human-readable",
            "Print some fields (e.g. timestamp) in human-readable format, add time offsets")
        .NoArgument()
        .StoreValue(&outputOptions.HumanReadable, true);

    //Supports only fields of type string
    TString containsEvent;
    opts.AddLongOption(
            'c', "contains-event",
            "Only print frames that contain events whose particular field's value matches given value.\n"
            "Match group should be provided in format: EventName:FieldName:ValueToMatch\n"
            "If more than one match group is provided, they should be separated by / delimiter.\n"
            "Nested fields are supported through \'.\' in FieldName.\n"
            "Symbols \'/\' and \':\' in EventName, FieldName or ValueToMatch must be escaped.\n"
            "NOTE: bool values are 1 and 0.")
        .Optional()
        .StoreResult(&containsEvent);

    TString helpEvents;
    opts.AddLongOption("help-events")
        .RequiredArgument("EVENT, EVENT_ID or string \"all\"")
        .Optional()
        .StoreResult(&helpEvents);

    proc->AddOptions(opts);

    opts.SetFreeArgsMin(0);
    opts.SetFreeArgsMax(1);
    opts.SetFreeArgTitle(0, "<eventlog>", "Event log file");

    try {
        const nlg::TOptsParseResult optsRes(&opts, argc, argv);

        if (helpEvents) {
            return PrintHelpEvents(helpEvents, fac);
        }

        TVector<TString> freeArgs = optsRes.GetFreeArgs();
        if (freeArgs) {
            o.FileName = freeArgs[0];
        }

        ui32 conditionsHappened = 0;

        if (frameIdFilter != INVALID_FRAME_ID) {
            ++conditionsHappened;
        }
        if (containsEvent) {
            ++conditionsHappened;
        }
        if (durationFilterStr) {
            ++conditionsHappened;
        }

        if (conditionsHappened > 1) {
            throw nlg::TUsageException() << "You can only use no more than one of frame id, duration or contains event frame filters.";
        }

        if (frameIdFilter != INVALID_FRAME_ID) {
            o.FrameFilter = CreateFrameIdFilter(frameIdFilter);
        } else if (durationFilterStr) {
            o.FrameFilter = CreateDurationFrameFilter(durationFilterStr);
        } else if (containsEvent) {
            o.FrameFilter = CreateContainsEventFrameFilter(containsEvent, fac);
        }

        // handle some inconsistencies
        if (o.ForceWeakOrdering && o.ForceStrongOrdering) {
            throw nlg::TUsageException() << "You can use either strong (-O) or weak (-o) ordering. ";
        }
        if (includeEventList && excludeEventList) {
            throw nlg::TUsageException() << "You can use either include (-i) or exclude (-x) events list. ";
        }
        if (unescaped && json) {
            throw nlg::TUsageException() << "You can use either unescaped (-u) or json (-j) output format. ";
        }

        proc->CheckOptions();
    } catch (...) {
        Cerr << "Usage error: " << CurrentExceptionMessage() << Endl;
        return 1;
    }

    o.EvList = o.EnableEvents ? includeEventList : excludeEventList;
    if (json) {
        outputOptions.OutputFormat = TEvent::TOutputFormat::Json;
    } else if (unescaped) {
        outputOptions.OutputFormat = TEvent::TOutputFormat::TabSeparatedRaw;
    } else {
        outputOptions.OutputFormat = TEvent::TOutputFormat::TabSeparated;
    }

    IEventProcessor* eventProcessor = NEvClass::Processor();
    // FIXME(mvel): A little of hell here: `proc` and `eventProcessor` are `IEventProcessor`s
    // So we need to set options for BOTH!

    eventProcessor->SetOptions(outputOptions);
    proc->SetOptions(outputOptions);
    proc->SetEventProcessor(eventProcessor);

    if (oneFrame) {
        THolder<IInputStream> fileInput;

        // this is for coredumps analysis, usage:
        // gdb: dump binary memory framefile Data_ Data_ + Pos_
        // evlogdump -f framefile
        IInputStream* usedInput = nullptr;

        if (o.FileName.size()) {
            fileInput.Reset(new TUnbufferedFileInput(o.FileName));
            usedInput = fileInput.Get();
        } else {
            usedInput = &Cin;
        }

        try {
            for (;;) {
                proc->ProcessEvent(DecodeEvent(*usedInput, true, 0, nullptr, fac).Get());
            }
        } catch (...) {
        }

        return 0;
    }

    o.StartTime = ParseTime(start, MIN_START_TIME);
    o.EndTime = ParseTime(end, MAX_END_TIME);

    try {
        THolder<IIterator> it = CreateIterator(o, fac);

        while (const auto ev = it->Next()) {
            if (!proc->CheckedProcessEvent(ev.Get())) {
                break;
            }
        }

        return 0;
    } catch (...) {
        Cout.Flush();
        Cerr << "Error occured: " << CurrentExceptionMessage() << Endl;
    }

    return 1;
}