blob: 7b3f2654282df4888d4d0a2852786f99d3a84d60 [file] [log] [blame]
Miroslav Zagorac70230c62020-12-09 16:54:31 +01001/***
2 * Copyright 2020 HAProxy Technologies
3 *
4 * This file is part of the HAProxy OpenTracing filter.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#ifndef _OPENTRACING_SCOPE_H_
21#define _OPENTRACING_SCOPE_H_
22
23#define FLT_OT_SCOPE_SPAN_FINISH_REQ "*req*"
24#define FLT_OT_SCOPE_SPAN_FINISH_RES "*res*"
25#define FLT_OT_SCOPE_SPAN_FINISH_ALL "*"
26
27#define FLT_OT_RT_CTX(a) ((struct flt_ot_runtime_context *)(a))
28
29#define FLT_OT_DBG_SCOPE_SPAN(f,a) \
30 FLT_OT_DBG(3, "%s%p:{ '%s' %zu %u %hhu %p %d %p %p }", \
31 (f), (a), FLT_OT_STR_HDR_ARGS(a, id), (a)->smp_opt_dir, \
32 (a)->flag_finish, (a)->span, (a)->ref_type, (a)->ref_span, (a)->ref_ctx)
33
34#define FLT_OT_DBG_SCOPE_CONTEXT(f,a) \
35 FLT_OT_DBG(3, "%s%p:{ '%s' %zu %u %hhu %p }", \
36 (f), (a), FLT_OT_STR_HDR_ARGS(a, id), (a)->smp_opt_dir, \
37 (a)->flag_finish, (a)->context)
38
39#define FLT_OT_DBG_SCOPE_DATA(f,a) \
40 FLT_OT_DBG(3, "%s%p:{ %p %d %p %p %d }", \
41 (f), (a), (a)->tags, (a)->num_tags, (a)->baggage, (a)->log_fields, (a)->num_log_fields)
42
43#define FLT_OT_DBG_RUNTIME_CONTEXT(f,a) \
44 FLT_OT_DBG(3, "%s%p:{ %p %p { %016" PRIx64 " %016" PRIx64 " '%s' } %hhu %hhu 0x%02hhx 0x%08x %s %s }", \
45 (f), (a), (a)->stream, (a)->filter, (a)->uuid.u64[0], (a)->uuid.u64[1], (a)->uuid.s, (a)->flag_harderr, \
46 (a)->flag_disabled, (a)->logging, (a)->analyzers, flt_ot_list_debug(&((a)->spans)), \
47 flt_ot_list_debug(&((a)->contexts)))
48
49#define FLT_OT_CONST_STR_HDR(a) \
50 struct { \
51 const char *a; \
52 size_t a##_len; \
53 }
54
55
56struct flt_ot_scope_data {
57 struct otc_tag tags[FLT_OT_MAXTAGS]; /* Defined tags. */
58 int num_tags; /* The number of tags used. */
59 struct otc_text_map *baggage; /* Defined baggage. */
60 struct otc_log_field log_fields[OTC_MAXLOGFIELDS]; /* Defined logs. */
61 int num_log_fields; /* The number of log fields used. */
62};
63
64/* flt_ot_runtime_context->spans */
65struct flt_ot_scope_span {
66 FLT_OT_CONST_STR_HDR(id); /* The span operation name/len. */
67 uint smp_opt_dir; /* SMP_OPT_DIR_RE(Q|S) */
68 bool flag_finish; /* Whether the span is marked for completion. */
69 struct otc_span *span; /* The current span. */
70 otc_span_reference_type_t ref_type; /* Span reference type. */
71 struct otc_span *ref_span; /* Span to which the current span refers. */
72 struct otc_span_context *ref_ctx; /* Span context to which the current span refers. */
73 struct list list; /* Used to chain this structure. */
74};
75
76/* flt_ot_runtime_context->contexts */
77struct flt_ot_scope_context {
78 FLT_OT_CONST_STR_HDR(id); /* The span context name/len. */
79 uint smp_opt_dir; /* SMP_OPT_DIR_RE(Q|S) */
80 bool flag_finish; /* Whether the span context is marked for completion. */
81 struct otc_span_context *context; /* The current span context. */
82 struct list list; /* Used to chain this structure. */
83};
84
85struct flt_ot_uuid {
86 union {
87 uint64_t u64[2];
88 uint8_t u8[16];
89 struct {
90 uint32_t time_low;
91 uint16_t time_mid;
92 uint16_t time_hi_and_version;
93 uint16_t clock_seq;
94 uint64_t node : 48;
95 } __attribute__((packed));
96 };
97 char s[40];
98};
99
100/* The runtime filter context attached to a stream. */
101struct flt_ot_runtime_context {
102 struct stream *stream; /* The stream to which the filter is attached. */
103 struct filter *filter; /* The OpenTracing filter. */
104 struct flt_ot_uuid uuid; /* Randomly generated UUID. */
105 bool flag_harderr; /* [0 1] */
106 bool flag_disabled; /* [0 1] */
107 uint8_t logging; /* [0 1 3] */
108 uint analyzers; /* Executed channel analyzers. */
109 struct list spans; /* The scope spans. */
110 struct list contexts; /* The scope contexts. */
111};
112
113
114#ifndef DEBUG_OT
115# define flt_ot_pools_info() while (0)
116#else
117void flt_ot_pools_info(void);
118#endif
119struct flt_ot_runtime_context *flt_ot_runtime_context_init(struct stream *s, struct filter *f, char **err);
120void flt_ot_runtime_context_free(struct filter *f);
121
122struct flt_ot_scope_span *flt_ot_scope_span_init(struct flt_ot_runtime_context *rt_ctx, const char *id, size_t id_len, otc_span_reference_type_t ref_type, const char *ref_id, size_t ref_id_len, uint dir, char **err);
123void flt_ot_scope_span_free(struct flt_ot_scope_span **ptr);
124struct flt_ot_scope_context *flt_ot_scope_context_init(struct flt_ot_runtime_context *rt_ctx, struct otc_tracer *tracer, const char *id, size_t id_len, const struct otc_text_map *text_map, uint dir, char **err);
125void flt_ot_scope_context_free(struct flt_ot_scope_context **ptr);
126void flt_ot_scope_data_free(struct flt_ot_scope_data *ptr);
127
128int flt_ot_scope_finish_mark(const struct flt_ot_runtime_context *rt_ctx, const char *id, size_t id_len);
129void flt_ot_scope_finish_marked(const struct flt_ot_runtime_context *rt_ctx, const struct timespec *ts_finish);
130void flt_ot_scope_free_unused(struct flt_ot_runtime_context *rt_ctx, struct channel *chn);
131
132#endif /* _OPENTRACING_SCOPE_H_ */
133
134/*
135 * Local variables:
136 * c-indent-level: 8
137 * c-basic-offset: 8
138 * End:
139 *
140 * vi: noexpandtab shiftwidth=8 tabstop=8
141 */