-
Notifications
You must be signed in to change notification settings - Fork 160
/
molten_util.h
158 lines (136 loc) · 4.83 KB
/
molten_util.h
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
/**
* Copyright 2017 chuan-yun silkcutks <[email protected]>
*
* Licensed 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.
*/
#ifndef MOLTEN_UTIL_H
#define MOLTEN_UTIL_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdint.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include "php.h"
#include "php7_wrapper.h"
#include "molten_common.h"
#ifdef USE_ZIPKIN_HEADER
#define MOLTEN_HEADER_PREFIX "X-B3-"
#define MOLTEN_REC_HEADER_PREFIX "HTTP_X_B3_"
#else
#define MOLTEN_HEADER_PREFIX "X-W-"
#define MOLTEN_REC_HEADER_PREFIX "HTTP_X_W_"
#endif
#define MOLTEN_HEADER_PREFIX_LEN (sizeof(MOLTEN_HEADER_PREFIX) - 1)
#define MOLTEN_HEADER_TRACE_ID MOLTEN_HEADER_PREFIX"TraceId"
#define MOLTEN_HEADER_SPAN_ID MOLTEN_HEADER_PREFIX"SpanId"
#define MOLTEN_HEADER_PARENT_SPAN_ID MOLTEN_HEADER_PREFIX"ParentSpanId"
#define MOLTEN_HEADER_SAMPLED MOLTEN_HEADER_PREFIX"Sampled"
#define MOLTEN_HEADER_FLAGS MOLTEN_HEADER_PREFIX"Flags"
#define MOLTEN_REC_TRACE_ID MOLTEN_REC_HEADER_PREFIX"TRACEID"
#define MOLTEN_REC_SPAN_ID MOLTEN_REC_HEADER_PREFIX"SPANID"
#define MOLTEN_REC_PARENT_SPAN_ID MOLTEN_REC_HEADER_PREFIX"PARENTSPANID"
#define MOLTEN_REC_SAMPLED MOLTEN_REC_HEADER_PREFIX"SAMPLED"
#define MOLTEN_REC_FLAGS MOLTEN_REC_HEADER_PREFIX"FLAGS"
#define SPAN_VERSION "php-4"
#define MO_FRAME_ENTRY 1
#define MO_FRAME_EXIT 2
#define MO_USEC_PER_SEC 1000000l
static inline long mo_time_sec()
{
struct timeval tv;
gettimeofday(&tv, 0);
return (long) tv.tv_sec;
}
static inline long mo_time_m()
{
struct timeval tv;
gettimeofday(&tv, 0);
long sec = tv.tv_sec;
return (long) sec/60 ;
}
static inline long mo_time_u2s(long usec)
{
return (long) usec/(MO_USEC_PER_SEC);
}
static inline long mo_time_usec()
{
struct timeval tv;
gettimeofday(&tv, 0);
return (long) tv.tv_sec * MO_USEC_PER_SEC + (long) tv.tv_usec;
}
/* {{{ find server variables */
static int find_server_var(char *key, int key_size, void **ret)
{
/* for jit preload server */
if (PG(auto_globals_jit)) {
mo_zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1);
}
#if PHP_MAJOR_VERSION < 7
zval **server = (zval **)&PG(http_globals)[TRACK_VARS_SERVER];
return mo_zend_hash_zval_find(Z_ARRVAL_P(*server), key, key_size, ret);
#else
zval *server = &PG(http_globals)[TRACK_VARS_SERVER];
return mo_zend_hash_zval_find(Z_ARRVAL_P(server), key, key_size, ret);
#endif
}
/* }}} */
/* {{{ join origin url */
static inline zend_bool join_ori_url(smart_string *url, zend_bool trim_query_string)
{
smart_string tmp = {0};
zval *http_host;
if (find_server_var("HTTP_HOST", sizeof("HTTP_HOST"), (void **)&http_host) != SUCCESS) {
return 0;
}
zval *request_uri;
if (find_server_var("REQUEST_URI", sizeof("REQUEST_URI"), (void **)&request_uri) != SUCCESS) {
return 0;
}
if (http_host != NULL && request_uri != NULL && (MO_Z_TYPE_P(http_host) == IS_STRING) && (MO_Z_TYPE_P(request_uri) == IS_STRING)) {
smart_string_appendl(&tmp, Z_STRVAL_P(http_host), Z_STRLEN_P(http_host));
smart_string_appendl(&tmp, Z_STRVAL_P(request_uri), Z_STRLEN_P(request_uri));
if (trim_query_string) {
int i;
char *string = smart_string_str(tmp);
for(i=Z_STRLEN_P(http_host); i < smart_string_len(tmp); i++) {
if(string[i] == '?') {
smart_string_appendl(url, smart_string_str(tmp), i);
break;
}
}
if (i == smart_string_len(tmp)) {
smart_string_appendl(url, smart_string_str(tmp), smart_string_len(tmp));
}
smart_string_0(url);
} else {
smart_string_appendl(url, smart_string_str(tmp), smart_string_len(tmp));
smart_string_0(url);
}
smart_string_free(&tmp);
return 1;
} else {
return 0;
}
}
/* }}} */
uint64_t rand_uint64(void);
void b2hex(char **output, const unsigned char *input, int input_len);
void bin2hex64(char **output, const uint64_t *input);
void rand64hex(char **output);
void build_span_id_random(char **span_id, char *parent_span_id, int span_count);
void build_span_id_level(char **span_id, char *parent_span_id, int span_count);
int check_hit_ratio(long base);
smart_string repr_zval(zval *zv, int limit TSRMLS_DC);
#endif