1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the
8  * License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #pragma once
21 
22 #include <random>
23 #include <ctime>
24 #include "ts/ts.h"
25 #include "ts/remap.h"
26 
27 #define PLUGIN_NAME "money_trace"
28 
29 #define LOG_DEBUG(fmt, ...)                                                                  \
30   do {                                                                                       \
31     TSDebug(PLUGIN_NAME, "[%s:%d] %s(): " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
32   } while (0)
33 
34 #define LOG_ERROR(fmt, ...)                                                     \
35   do {                                                                          \
36     TSError("[%s:%d] %s(): " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
37   } while (0)
38 
39 #define MIME_FIELD_MONEY_TRACE "X-MoneyTrace"
40 #define MIME_LEN_MONEY_TRACE 12
41 
42 struct MT {
43   std::minstd_rand0 generator;
44 
MTMT45   MT() { generator.seed(time(nullptr)); }
46   long
spanIdMT47   spanId()
48   {
49     long v = generator();
50     return (v * v);
51   }
52   const char *moneyTraceHdr(const char *mt_request_hdr);
53 };
54 
55 struct txndata {
56   char *client_request_mt_header;
57   char *new_span_mt_header;
58 };
59 
60 static struct txndata *allocTransactionData();
61 static void freeTransactionData(struct txndata *txn_data);
62 static void mt_cache_lookup_check(TSCont contp, TSHttpTxn txnp, struct txndata *txn_data);
63 static void mt_check_request_header(TSHttpTxn txnp);
64 static void mt_send_client_response(TSHttpTxn txnp, struct txndata *txn_data);
65 static void mt_send_server_request(TSHttpTxn txnp, struct txndata *txn_data);
66 static int transaction_handler(TSCont contp, TSEvent event, void *edata);
67