File: http_stats.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (767 lines) | stat: -rw-r--r-- 24,428 bytes parent folder | download | duplicates (2)
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/** @file

  Stats Over HTTP as an origin intercept

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

#include <cerrno>
#include <cinttypes>

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>

#include <fstream>
#include <sstream>

#include <string>
#include <getopt.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include "ts/ts.h"
#include "ts/remap.h"

constexpr char PLUGIN[] = "http_stats";

#define VDEBUG(fmt, ...) TSDebug(PLUGIN, fmt, ##__VA_ARGS__)

#if DEBUG
#define VERROR(fmt, ...) TSDebug(PLUGIN, fmt, ##__VA_ARGS__)
#else
#define VERROR(fmt, ...) TSError("[%s] %s: " fmt, PLUGIN, __FUNCTION__, ##__VA_ARGS__)
#endif

#define VIODEBUG(vio, fmt, ...)                                                                                              \
  VDEBUG("vio=%p vio.cont=%p, vio.cont.data=%p, vio.vc=%p " fmt, (vio), TSVIOContGet(vio), TSContDataGet(TSVIOContGet(vio)), \
         TSVIOVConnGet(vio), ##__VA_ARGS__)

static int StatCountBytes     = -1;
static int StatCountResponses = -1;

static int HTTPStatsInterceptHook(TSCont contp, TSEvent event, void *edata);
static int HTTPStatsTxnHook(TSCont contp, TSEvent event, void *edata);

struct HTTPStatsFormatter {
  HTTPStatsFormatter(bool csv, bool integer_counters, bool wrap_counters)
    : csv(csv), integer_counters(integer_counters), wrap_counters(wrap_counters)
  {
  }

  uint64_t wrap_unsigned_counter(uint64_t value);
  std::string output();

  bool csv              = false;
  bool integer_counters = false;
  bool wrap_counters    = false;
  std::string buf;
};

struct HTTPStatsConfig {
  explicit HTTPStatsConfig() {}

  ~HTTPStatsConfig() { TSContDestroy(cont); }
  std::string mimeType;

  int maxAge            = 0;
  bool csv              = false;
  bool integer_counters = false;
  bool wrap_counters    = false;

  TSCont cont;
};

struct HTTPStatsRequest;

union argument_type {
  void *ptr;
  intptr_t ecode;
  TSVConn vc;
  TSVIO vio;
  TSHttpTxn txn;
  HTTPStatsRequest *trq;

  argument_type(void *_p) : ptr(_p) {}
};

// This structure represents the state of a streaming I/O request. It
// is directional (ie. either a read or a write). We need two of these
// for each TSVConn; one to push data into the TSVConn and one to pull
// data out.
struct IOChannel {
  TSVIO vio = nullptr;
  TSIOBuffer iobuf;
  TSIOBufferReader reader;

  IOChannel() : iobuf(TSIOBufferSizedCreate(TS_IOBUFFER_SIZE_INDEX_32K)), reader(TSIOBufferReaderAlloc(iobuf)) {}
  ~IOChannel()
  {
    if (this->reader) {
      TSIOBufferReaderFree(this->reader);
    }

    if (this->iobuf) {
      TSIOBufferDestroy(this->iobuf);
    }
  }

  void
  read(TSVConn vc, TSCont contp)
  {
    this->vio = TSVConnRead(vc, contp, this->iobuf, INT64_MAX);
  }

  void
  write(TSVConn vc, TSCont contp)
  {
    this->vio = TSVConnWrite(vc, contp, this->reader, INT64_MAX);
  }
};

struct HTTPStatsHttpHeader {
  TSMBuffer buffer;
  TSMLoc header;
  TSHttpParser parser;

  HTTPStatsHttpHeader()
  {
    this->buffer = TSMBufferCreate();
    this->header = TSHttpHdrCreate(this->buffer);
    this->parser = TSHttpParserCreate();
  }

  ~HTTPStatsHttpHeader()
  {
    if (this->parser) {
      TSHttpParserDestroy(this->parser);
    }

    TSHttpHdrDestroy(this->buffer, this->header);
    TSHandleMLocRelease(this->buffer, TS_NULL_MLOC, this->header);
    TSMBufferDestroy(this->buffer);
  }
};

struct HTTPStatsRequest {
  HTTPStatsRequest() {}

  off_t nbytes        = 0; // Number of bytes to generate.
  unsigned maxAge     = 0; // Max age for cache responses.
  unsigned statusCode = 200;
  IOChannel readio;
  IOChannel writeio;
  HTTPStatsHttpHeader rqheader;

  std::string mimeType;
  std::string body;

  static HTTPStatsRequest *
  createHTTPStatsRequest(HTTPStatsConfig *cfg)
  {
    HTTPStatsRequest *shr = new HTTPStatsRequest;

    HTTPStatsFormatter fmtr(cfg->csv, cfg->integer_counters, cfg->wrap_counters);
    shr->body = fmtr.output();

    shr->nbytes     = shr->body.size();
    shr->mimeType   = cfg->mimeType;
    shr->statusCode = 200;
    shr->maxAge     = cfg->maxAge;

    return shr;
  }

  ~HTTPStatsRequest() = default;
};

// Destroy a HTTPStatsRequest, including the per-txn continuation.
static void
HTTPStatsRequestDestroy(HTTPStatsRequest *trq, TSVIO vio, TSCont contp)
{
  if (vio) {
    TSVConnClose(TSVIOVConnGet(vio));
  }

  TSContDestroy(contp);
  delete trq;
}

// NOTE: This will always append a new "field_name: value"
static void
HeaderFieldDateSet(const HTTPStatsHttpHeader &http, const char *field_name, int64_t field_len, time_t value)
{
  TSMLoc field;

  TSMimeHdrFieldCreateNamed(http.buffer, http.header, field_name, field_len, &field);
  TSMimeHdrFieldValueDateSet(http.buffer, http.header, field, value);
  TSMimeHdrFieldAppend(http.buffer, http.header, field);
  TSHandleMLocRelease(http.buffer, http.header, field);
}

// NOTE: This will always append a new "field_name: value"
static void
HeaderFieldIntSet(const HTTPStatsHttpHeader &http, const char *field_name, int64_t field_len, int64_t value)
{
  TSMLoc field;

  TSMimeHdrFieldCreateNamed(http.buffer, http.header, field_name, field_len, &field);
  TSMimeHdrFieldValueInt64Set(http.buffer, http.header, field, -1, value);
  TSMimeHdrFieldAppend(http.buffer, http.header, field);
  TSHandleMLocRelease(http.buffer, http.header, field);
}

// NOTE: This will always append a new "field_name: value"
static void
HeaderFieldStringSet(const HTTPStatsHttpHeader &http, const char *field_name, int64_t field_len, const char *value)
{
  TSMLoc field;

  TSMimeHdrFieldCreateNamed(http.buffer, http.header, field_name, field_len, &field);
  TSMimeHdrFieldValueStringSet(http.buffer, http.header, field, -1, value, -1);
  TSMimeHdrFieldAppend(http.buffer, http.header, field);
  TSHandleMLocRelease(http.buffer, http.header, field);
}

static TSReturnCode
WriteResponseHeader(HTTPStatsRequest *trq, TSCont contp, TSHttpStatus status)
{
  HTTPStatsHttpHeader response;

  VDEBUG("writing response header");

  if (TSHttpHdrTypeSet(response.buffer, response.header, TS_HTTP_TYPE_RESPONSE) != TS_SUCCESS) {
    VERROR("failed to set type");
    return TS_ERROR;
  }
  if (TSHttpHdrVersionSet(response.buffer, response.header, TS_HTTP_VERSION(1, 1)) != TS_SUCCESS) {
    VERROR("failed to set HTTP version");
    return TS_ERROR;
  }
  if (TSHttpHdrStatusSet(response.buffer, response.header, status) != TS_SUCCESS) {
    VERROR("failed to set HTTP status");
    return TS_ERROR;
  }

  TSHttpHdrReasonSet(response.buffer, response.header, TSHttpHdrReasonLookup(status), -1);

  if (status == TS_HTTP_STATUS_OK) {
    // Set the Content-Length header.
    HeaderFieldIntSet(response, TS_MIME_FIELD_CONTENT_LENGTH, TS_MIME_LEN_CONTENT_LENGTH, trq->nbytes);

    // Set the Cache-Control header.
    if (trq->maxAge > 0) {
      char buf[64];

      snprintf(buf, sizeof(buf), "max-age=%u", trq->maxAge);
      HeaderFieldStringSet(response, TS_MIME_FIELD_CACHE_CONTROL, TS_MIME_LEN_CACHE_CONTROL, buf);
      HeaderFieldDateSet(response, TS_MIME_FIELD_LAST_MODIFIED, TS_MIME_LEN_LAST_MODIFIED, time(nullptr));

    } else {
      HeaderFieldStringSet(response, TS_MIME_FIELD_CACHE_CONTROL, TS_MIME_LEN_CACHE_CONTROL, "no-cache");
    }

    HeaderFieldStringSet(response, TS_MIME_FIELD_CONTENT_TYPE, TS_MIME_LEN_CONTENT_TYPE, trq->mimeType.c_str());
  }

  // Write the header to the IO buffer. Set the VIO bytes so that we can get a WRITE_COMPLETE
  // event when this is done.
  int hdrlen = TSHttpHdrLengthGet(response.buffer, response.header);

  TSHttpHdrPrint(response.buffer, response.header, trq->writeio.iobuf);
  TSVIONBytesSet(trq->writeio.vio, hdrlen);
  TSVIOReenable(trq->writeio.vio);

  TSStatIntIncrement(StatCountBytes, hdrlen);

  return TS_SUCCESS;
}

static bool
HTTPStatsParseRequest(HTTPStatsRequest *trq)
{
  const char *path;
  int pathsz;

  // Make sure this is a GET request
  path = TSHttpHdrMethodGet(trq->rqheader.buffer, trq->rqheader.header, &pathsz);
  if (path != TS_HTTP_METHOD_GET) {
    VDEBUG("%.*s method is not supported", pathsz, path);
    return false;
  }

  return true;
}

// Handle events from TSHttpTxnServerIntercept. The intercept
// starts with TS_EVENT_NET_ACCEPT, and then continues with
// TSVConn events.
static int
HTTPStatsInterceptHook(TSCont contp, TSEvent event, void *edata)
{
  VDEBUG("HTTPStatsInterceptHook: %p ", edata);

  argument_type arg(edata);

  VDEBUG("contp=%p, event=%s (%d), edata=%p", contp, TSHttpEventNameLookup(event), event, arg.ptr);

  switch (event) {
  case TS_EVENT_NET_ACCEPT: {
    // TS_EVENT_NET_ACCEPT will be delivered when the server intercept
    // is set up by the core. We just need to allocate a stats_over_http2
    // request state and start reading the VC.
    HTTPStatsRequest *trq = static_cast<HTTPStatsRequest *>(TSContDataGet(contp));

    TSStatIntIncrement(StatCountResponses, 1);
    VDEBUG("allocated server intercept stats_over_http2 trq=%p", trq);

    // This continuation was allocated in HTTPStatsTxnHook. Reset the
    // data to keep track of this generator request.
    TSContDataSet(contp, trq);

    // Start reading the request from the server intercept VC.
    trq->readio.read(arg.vc, contp);
    VIODEBUG(trq->readio.vio, "started reading stats_over_http2 request");

    return TS_EVENT_NONE;
  }

  case TS_EVENT_NET_ACCEPT_FAILED: {
    // TS_EVENT_NET_ACCEPT_FAILED will be delivered if the
    // transaction is cancelled before we start tunnelling
    // through the server intercept. One way that this can happen
    // is if the intercept is attached early, and then we serve
    // the document out of cache.

    // There's nothing to do here except nuke the continuation
    // that was allocated in HTTPStatsTxnHook().

    HTTPStatsRequest *trq = static_cast<HTTPStatsRequest *>(TSContDataGet(contp));
    delete trq;

    TSContDestroy(contp);
    return TS_EVENT_NONE;
  }

  case TS_EVENT_VCONN_READ_READY: {
    argument_type cdata           = TSContDataGet(contp);
    HTTPStatsHttpHeader &rqheader = cdata.trq->rqheader;

    VDEBUG("reading vio=%p vc=%p, trq=%p", arg.vio, TSVIOVConnGet(arg.vio), cdata.trq);

    TSIOBufferBlock blk;
    TSParseResult result = TS_PARSE_CONT;

    for (blk = TSIOBufferReaderStart(cdata.trq->readio.reader); blk; blk = TSIOBufferBlockNext(blk)) {
      const char *ptr;
      const char *end;
      int64_t nbytes;
      TSHttpStatus status = static_cast<TSHttpStatus>(cdata.trq->statusCode);

      ptr = TSIOBufferBlockReadStart(blk, cdata.trq->readio.reader, &nbytes);
      if (ptr == nullptr || nbytes == 0) {
        continue;
      }

      end    = ptr + nbytes;
      result = TSHttpHdrParseReq(rqheader.parser, rqheader.buffer, rqheader.header, &ptr, end);
      switch (result) {
      case TS_PARSE_ERROR:
        // If we got a bad request, just shut it down.
        VDEBUG("bad request on trq=%p, sending an error", cdata.trq);
        HTTPStatsRequestDestroy(cdata.trq, arg.vio, contp);
        return TS_EVENT_ERROR;

      case TS_PARSE_DONE:
        // Check the response.
        VDEBUG("parsed request on trq=%p, sending a response", cdata.trq);
        if (!HTTPStatsParseRequest(cdata.trq)) {
          status = TS_HTTP_STATUS_METHOD_NOT_ALLOWED;
        }

        // Start the vconn write.
        cdata.trq->writeio.write(TSVIOVConnGet(arg.vio), contp);
        TSVIONBytesSet(cdata.trq->writeio.vio, 0);

        if (WriteResponseHeader(cdata.trq, contp, status) != TS_SUCCESS) {
          VERROR("failure writing response");
          return TS_EVENT_ERROR;
        }

        return TS_EVENT_NONE;

      case TS_PARSE_CONT:
        break;
      }
    }

    TSReleaseAssert(result == TS_PARSE_CONT);

    // Reenable the read VIO to get more events.
    TSVIOReenable(arg.vio);
    return TS_EVENT_NONE;
  }

  case TS_EVENT_VCONN_WRITE_READY: {
    argument_type cdata = TSContDataGet(contp);

    if (cdata.trq->nbytes) {
      int64_t nbytes = cdata.trq->nbytes;

      VIODEBUG(arg.vio, "writing %" PRId64 " bytes for trq=%p", nbytes, cdata.trq);

      nbytes = TSIOBufferWrite(cdata.trq->writeio.iobuf, cdata.trq->body.c_str(), nbytes);

      cdata.trq->nbytes -= nbytes;
      TSStatIntIncrement(StatCountBytes, nbytes);

      // Update the number of bytes to write.
      TSVIONBytesSet(arg.vio, TSVIONBytesGet(arg.vio) + nbytes);
      TSVIOReenable(arg.vio);
    }

    return TS_EVENT_NONE;
  }

  case TS_EVENT_ERROR:
  case TS_EVENT_VCONN_EOS: {
    argument_type cdata = TSContDataGet(contp);

    VIODEBUG(arg.vio, "received EOS or ERROR for trq=%p", cdata.trq);
    HTTPStatsRequestDestroy(cdata.trq, arg.vio, contp);
    return event == TS_EVENT_ERROR ? TS_EVENT_ERROR : TS_EVENT_NONE;
  }

  case TS_EVENT_VCONN_READ_COMPLETE:
    // We read data forever, so we should never get a READ_COMPLETE.
    VIODEBUG(arg.vio, "unexpected TS_EVENT_VCONN_READ_COMPLETE");
    return TS_EVENT_NONE;

  case TS_EVENT_VCONN_WRITE_COMPLETE: {
    argument_type cdata = TSContDataGet(contp);

    // If we still have bytes to write, kick off a new write operation, otherwise
    // we are done and we can shut down the VC.
    if (cdata.trq->nbytes) {
      cdata.trq->writeio.write(TSVIOVConnGet(arg.vio), contp);
      TSVIONBytesSet(cdata.trq->writeio.vio, cdata.trq->nbytes);
    } else {
      VIODEBUG(arg.vio, "TS_EVENT_VCONN_WRITE_COMPLETE %" PRId64 " todo", TSVIONTodoGet(arg.vio));
      HTTPStatsRequestDestroy(cdata.trq, arg.vio, contp);
    }

    return TS_EVENT_NONE;
  }

  case TS_EVENT_TIMEOUT: {
    return TS_EVENT_NONE;
  }

  case TS_EVENT_VCONN_INACTIVITY_TIMEOUT:
    VERROR("unexpected event %s (%d) edata=%p", TSHttpEventNameLookup(event), event, arg.ptr);
    return TS_EVENT_ERROR;

  default:
    VERROR("unexpected event %s (%d) edata=%p", TSHttpEventNameLookup(event), event, arg.ptr);
    return TS_EVENT_ERROR;
  }
}

static void
HTTPStatsSetupIntercept(HTTPStatsConfig *cfg, TSHttpTxn txn)
{
  HTTPStatsRequest *req = HTTPStatsRequest::createHTTPStatsRequest(cfg);
  if (req == nullptr) {
    VERROR("could not create request!");
    return;
  }

  TSCont cnt = TSContCreate(HTTPStatsInterceptHook, TSMutexCreate());
  TSContDataSet(cnt, req);

  TSHttpTxnServerIntercept(cnt, txn);

  return;
}

// Handle events that occur on the TSHttpTxn.
static int
HTTPStatsTxnHook(TSCont contp, TSEvent event, void *edata)
{
  argument_type arg(edata);

  VDEBUG("contp=%p, event=%s (%d), edata=%p", contp, TSHttpEventNameLookup(event), event, edata);

  switch (event) {
  case TS_EVENT_HTTP_CACHE_LOOKUP_COMPLETE: {
    int method_length, status;
    TSMBuffer bufp;
    TSMLoc hdr_loc;
    const char *method;

    if (TSHttpTxnCacheLookupStatusGet(arg.txn, &status) != TS_SUCCESS) {
      VERROR("failed to get client request handle");
      goto done;
    }

    if (TSHttpTxnClientReqGet(arg.txn, &bufp, &hdr_loc) != TS_SUCCESS) {
      VERROR("Couldn't retrieve client request header");
      goto done;
    }

    method = TSHttpHdrMethodGet(bufp, hdr_loc, &method_length);
    if (nullptr == method) {
      VERROR("Couldn't retrieve client request method");
      goto done;
    }

    if (status != TS_CACHE_LOOKUP_HIT_FRESH || method != TS_HTTP_METHOD_GET) {
      HTTPStatsSetupIntercept(static_cast<HTTPStatsConfig *>(TSContDataGet(contp)), arg.txn);
    }

    break;
  }

  default:
    VERROR("unexpected event %s (%d)", TSHttpEventNameLookup(event), event);
    break;
  }

done:
  TSHttpTxnReenable(arg.txn, TS_EVENT_HTTP_CONTINUE);
  return TS_EVENT_NONE;
}

TSReturnCode
TSRemapInit(TSRemapInterface * /* api_info */, char * /* errbuf */, int /* errbuf_size */)
{
  if (TSStatFindName("stats_over_http2.response_bytes", &StatCountBytes) == TS_ERROR) {
    StatCountBytes =
      TSStatCreate("stats_over_http2.response_bytes", TS_RECORDDATATYPE_COUNTER, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_SUM);
  }

  if (TSStatFindName("stats_over_http2.response_count", &StatCountResponses) == TS_ERROR) {
    StatCountResponses =
      TSStatCreate("stats_over_http2.response_count", TS_RECORDDATATYPE_COUNTER, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_COUNT);
  }
  return TS_SUCCESS;
}

TSRemapStatus
TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
{
  const TSHttpStatus txnstat = TSHttpTxnStatusGet(rh);
  if (txnstat != TS_HTTP_STATUS_NONE && txnstat != TS_HTTP_STATUS_OK) {
    VDEBUG("transaction status_code=%d already set; skipping processing", static_cast<int>(txnstat));
    return TSREMAP_NO_REMAP;
  }

  HTTPStatsConfig *cfg = static_cast<HTTPStatsConfig *>(ih);

  if (!cfg) {
    VERROR("No remap context available, check code / config");
    TSHttpTxnStatusSet(rh, TS_HTTP_STATUS_INTERNAL_SERVER_ERROR);
    return TSREMAP_NO_REMAP;
  }

  if (!cfg->maxAge) {
    TSHttpTxnConfigIntSet(rh, TS_CONFIG_HTTP_CACHE_HTTP, 0);
    HTTPStatsSetupIntercept(static_cast<HTTPStatsConfig *>(ih), rh);
  } else {
    TSHttpTxnHookAdd(rh, TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK, static_cast<HTTPStatsConfig *>(ih)->cont);
  }

  return TSREMAP_NO_REMAP; // This plugin never rewrites anything.
}

TSReturnCode
TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf ATS_UNUSED */, int /* errbuf_size ATS_UNUSED */)
{
  static const struct option longopt[] = {{"csv", no_argument, nullptr, 'c'},
                                          {"integer-counters", no_argument, NULL, 'i'},
                                          {"wrap-counters", no_argument, NULL, 'w'},
                                          {"max-age", required_argument, nullptr, 'a'},
                                          {nullptr, no_argument, nullptr, '\0'}};

  HTTPStatsConfig *cfg = new HTTPStatsConfig;

  // argv contains the "to" and "from" URLs. Skip the first so that the
  // second one poses as the program name.
  --argc;
  ++argv;
  optind = 0;

  while (true) {
    int opt = getopt_long(argc, (char *const *)argv, "ciw", longopt, nullptr);

    switch (opt) {
    case 'i':
      cfg->integer_counters = true;
      break;
    case 'w':
      cfg->wrap_counters = true;
      break;
    case 'c':
      cfg->csv = true;
      break;
    case 'a':
      cfg->maxAge = atoi(optarg);
      break;
    }
    if (opt == -1) {
      break;
    }
  }

  if (cfg->csv) {
    cfg->mimeType = "text/csv";
  } else {
    cfg->mimeType = "application/json";
  }

  // Finally, create the continuation to use for this remap rule, tracking the config as cont data.
  cfg->cont = TSContCreate(HTTPStatsTxnHook, nullptr);
  TSContDataSet(cfg->cont, cfg);

  *ih = static_cast<void *>(cfg);

  return TS_SUCCESS;
}

void
TSRemapDeleteInstance(void *ih)
{
  HTTPStatsConfig *cfg = static_cast<HTTPStatsConfig *>(ih);
  delete cfg;
}

// This wraps uint64_t values to the int64_t range to fit into a Java long. Java 8 has an unsigned long which
// can interoperate with a full uint64_t, but it's unlikely that much of the ecosystem supports that yet.
uint64_t
HTTPStatsFormatter::wrap_unsigned_counter(uint64_t value)
{
  if (wrap_counters) {
    return (value > INT64_MAX) ? value % INT64_MAX : value;
  }
  return value;
};

#define APPEND(b, a) b += a
#define APPEND_STAT_JSON(c, a, fmt, v)                                           \
  do {                                                                           \
    char b[256];                                                                 \
    if (snprintf(b, sizeof(b), "\"%s\": \"" fmt "\",\n", a, v) < (int)sizeof(b)) \
      APPEND(c->buf, b);                                                         \
  } while (0)
#define APPEND_STAT_JSON_NUMERIC(f, a, fmt, v)                                       \
  do {                                                                               \
    char b[256];                                                                     \
    if (f->integer_counters) {                                                       \
      if (snprintf(b, sizeof(b), "\"%s\": " fmt ",\n", a, v) < (int)sizeof(b)) {     \
        APPEND(f->buf, b);                                                           \
      }                                                                              \
    } else {                                                                         \
      if (snprintf(b, sizeof(b), "\"%s\": \"" fmt "\",\n", a, v) < (int)sizeof(b)) { \
        APPEND(f->buf, b);                                                           \
      }                                                                              \
    }                                                                                \
  } while (0)

#define APPEND_STAT_CSV(f, a, fmt, v)                                  \
  do {                                                                 \
    char b[256];                                                       \
    if (snprintf(b, sizeof(b), "%s," fmt "\n", a, v) < (int)sizeof(b)) \
      APPEND(f->buf, b);                                               \
  } while (0)
#define APPEND_STAT_CSV_NUMERIC(f, a, fmt, v)                            \
  do {                                                                   \
    char b[256];                                                         \
    if (snprintf(b, sizeof(b), "%s," fmt "\n", a, v) < (int)sizeof(b)) { \
      APPEND(f->buf, b);                                                 \
    }                                                                    \
  } while (0)

static void
json_out_stat(TSRecordType rec_type, void *edata, int registered, const char *name, TSRecordDataType data_type, TSRecordData *datum)
{
  HTTPStatsFormatter *fmtr = static_cast<HTTPStatsFormatter *>(edata);

  switch (data_type) {
  case TS_RECORDDATATYPE_COUNTER:
    APPEND_STAT_JSON_NUMERIC(fmtr, name, "%" PRIu64, fmtr->wrap_unsigned_counter(datum->rec_counter));
    break;
  case TS_RECORDDATATYPE_INT:
    APPEND_STAT_JSON_NUMERIC(fmtr, name, "%" PRIu64, fmtr->wrap_unsigned_counter(datum->rec_int));
    break;
  case TS_RECORDDATATYPE_FLOAT:
    APPEND_STAT_JSON_NUMERIC(fmtr, name, "%f", datum->rec_float);
    break;
  case TS_RECORDDATATYPE_STRING:
    APPEND_STAT_JSON(fmtr, name, "%s", datum->rec_string);
    break;
  default:
    TSDebug(PLUGIN, "unknown type for %s: %d", name, data_type);
    break;
  }
}

static void
csv_out_stat(TSRecordType rec_type, void *edata, int registered, const char *name, TSRecordDataType data_type, TSRecordData *datum)
{
  HTTPStatsFormatter *fmtr = static_cast<HTTPStatsFormatter *>(edata);

  switch (data_type) {
  case TS_RECORDDATATYPE_COUNTER:
    APPEND_STAT_CSV_NUMERIC(fmtr, name, "%" PRIu64, fmtr->wrap_unsigned_counter(datum->rec_counter));
    break;
  case TS_RECORDDATATYPE_INT:
    APPEND_STAT_CSV_NUMERIC(fmtr, name, "%" PRIu64, fmtr->wrap_unsigned_counter(datum->rec_int));
    break;
  case TS_RECORDDATATYPE_FLOAT:
    APPEND_STAT_CSV_NUMERIC(fmtr, name, "%f", datum->rec_float);
    break;
  case TS_RECORDDATATYPE_STRING:
    APPEND_STAT_CSV(fmtr, name, "%s", datum->rec_string);
    break;
  default:
    TSDebug(PLUGIN, "unknown type for %s: %d", name, data_type);
    break;
  }
}

std::string
HTTPStatsFormatter::output()
{
  if (csv) {
    TSRecordDump((TSRecordType)(TS_RECORDTYPE_PLUGIN | TS_RECORDTYPE_NODE | TS_RECORDTYPE_PROCESS), csv_out_stat, this);
    APPEND_STAT_CSV(this, "version", "%s", TSTrafficServerVersionGet());
  } else {
    APPEND(buf, "{ \"global\": {\n");

    TSRecordDump((TSRecordType)(TS_RECORDTYPE_PLUGIN | TS_RECORDTYPE_NODE | TS_RECORDTYPE_PROCESS), json_out_stat, this);

    APPEND(buf, "\"server\": \"");
    APPEND(buf, TSTrafficServerVersionGet());
    APPEND(buf, "\"\n");
    APPEND(buf, "  }\n}\n");
  }
  return buf;
}