1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2017-2020 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #include "internal.h"
19 
20 using namespace lcb::trace;
21 
lcbtrace_new(lcb_INSTANCE *instance, uint64_t flags)22 LIBCOUCHBASE_API lcbtrace_TRACER *lcbtrace_new(lcb_INSTANCE *instance, uint64_t flags)
23 {
24     switch (flags) {
25         case LCBTRACE_F_THRESHOLD:
26             if (nullptr == instance) {
27                 return nullptr;
28             }
29             return (new ThresholdLoggingTracer(instance))->wrap();
30         case LCBTRACE_F_EXTERNAL: {
31             lcbtrace_TRACER *retval = new lcbtrace_TRACER;
32             retval->version = 0;
33             retval->flags = LCBTRACE_F_EXTERNAL;
34             retval->cookie = nullptr;
35             retval->destructor = nullptr;
36             retval->v.v0.report = nullptr;
37             retval->v.v1.start_span = nullptr;
38             retval->v.v1.end_span = nullptr;
39             retval->v.v1.destroy_span = nullptr;
40             retval->v.v1.add_tag_string = nullptr;
41             retval->v.v1.add_tag_uint64 = nullptr;
42             return retval;
43         }
44         default:
45             return nullptr;
46     }
47 }
48 
lcbtrace_destroy(lcbtrace_TRACER *tracer)49 LIBCOUCHBASE_API void lcbtrace_destroy(lcbtrace_TRACER *tracer)
50 {
51     if (tracer && tracer->destructor) {
52         tracer->destructor(tracer);
53     }
54 }
55 
56 LIBCOUCHBASE_API
lcbtrace_span_start(lcbtrace_TRACER *tracer, const char *opname, uint64_t start, lcbtrace_REF *ref)57 lcbtrace_SPAN *lcbtrace_span_start(lcbtrace_TRACER *tracer, const char *opname, uint64_t start, lcbtrace_REF *ref)
58 {
59     lcbtrace_REF_TYPE type = LCBTRACE_REF_NONE;
60     lcbtrace_SPAN *other = nullptr;
61     if (ref) {
62         type = ref->type;
63         other = ref->span;
64     }
65     return new Span(tracer, opname, start, type, other, nullptr);
66 }
67 
68 LIBCOUCHBASE_API
lcbtrace_span_wrap(lcbtrace_TRACER *tracer, const char *opname, uint64_t start, void *external_span, lcbtrace_SPAN **lcbspan)69 lcb_STATUS lcbtrace_span_wrap(lcbtrace_TRACER *tracer, const char *opname, uint64_t start, void *external_span,
70                               lcbtrace_SPAN **lcbspan)
71 {
72     if (nullptr == *lcbspan && nullptr != external_span && nullptr != tracer && tracer->version == 1) {
73         *lcbspan = new Span(tracer, opname, start, LCBTRACE_REF_NONE, nullptr, external_span);
74         return LCB_SUCCESS;
75     }
76     return LCB_ERR_INVALID_ARGUMENT;
77 }
78 
79 LIBCOUCHBASE_API
lcb_get_tracer(lcb_INSTANCE *instance)80 lcbtrace_TRACER *lcb_get_tracer(lcb_INSTANCE *instance)
81 {
82     return (instance && instance->settings) ? instance->settings->tracer : nullptr;
83 }
84 
85 LIBCOUCHBASE_API
lcb_set_tracer(lcb_INSTANCE *instance, lcbtrace_TRACER *tracer)86 void lcb_set_tracer(lcb_INSTANCE *instance, lcbtrace_TRACER *tracer)
87 {
88     // TODO: this leaks the original one in the instance, _if_ it is
89     // the default ThresholdLoggingTracer
90     if (instance && instance->settings) {
91         instance->settings->tracer = tracer;
92     }
93 }
94 
lcbtrace_span_add_host_and_port(lcbtrace_SPAN *span, lcbio_CONNINFO *info)95 void lcbtrace_span_add_host_and_port(lcbtrace_SPAN *span, lcbio_CONNINFO *info)
96 {
97     if (span) {
98         lcbtrace_span_add_tag_str_nocopy(span, LCBTRACE_TAG_LOCAL_ADDRESS, info->ep_local.host);
99         lcbtrace_span_add_tag_str_nocopy(span, LCBTRACE_TAG_LOCAL_PORT, info->ep_local.port);
100         lcbtrace_span_add_tag_str_nocopy(span, LCBTRACE_TAG_PEER_ADDRESS, info->ep_remote.host);
101         lcbtrace_span_add_tag_str_nocopy(span, LCBTRACE_TAG_PEER_PORT, info->ep_remote.port);
102     }
103 }
104 
dur_level_to_string(lcb_DURABILITY_LEVEL dur_level)105 const char *dur_level_to_string(lcb_DURABILITY_LEVEL dur_level)
106 {
107     switch (dur_level) {
108         case LCB_DURABILITYLEVEL_NONE:
109             return "none";
110         case LCB_DURABILITYLEVEL_MAJORITY:
111             return "majority";
112         case LCB_DURABILITYLEVEL_MAJORITY_AND_PERSIST_TO_ACTIVE:
113             return "majority_and_persist_to_active";
114         case LCB_DURABILITYLEVEL_PERSIST_TO_MAJORITY:
115             return "persist_to_majority";
116         default:
117             return "unknown";
118     }
119 }
120