1dc71348aSSergey Avseyev/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2dc71348aSSergey Avseyev/*
3cfedb1f4SSergey Avseyev *     Copyright 2017-2020 Couchbase, Inc.
4dc71348aSSergey Avseyev *
5dc71348aSSergey Avseyev *   Licensed under the Apache License, Version 2.0 (the "License");
6dc71348aSSergey Avseyev *   you may not use this file except in compliance with the License.
7dc71348aSSergey Avseyev *   You may obtain a copy of the License at
8dc71348aSSergey Avseyev *
9dc71348aSSergey Avseyev *       http://www.apache.org/licenses/LICENSE-2.0
10dc71348aSSergey Avseyev *
11dc71348aSSergey Avseyev *   Unless required by applicable law or agreed to in writing, software
12dc71348aSSergey Avseyev *   distributed under the License is distributed on an "AS IS" BASIS,
13dc71348aSSergey Avseyev *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dc71348aSSergey Avseyev *   See the License for the specific language governing permissions and
15dc71348aSSergey Avseyev *   limitations under the License.
16dc71348aSSergey Avseyev */
17dc71348aSSergey Avseyev
18dc71348aSSergey Avseyev#include "internal.h"
19dc71348aSSergey Avseyev
20dc71348aSSergey Avseyevusing namespace lcb::trace;
21dc71348aSSergey Avseyev
226b7f4186SDavid KellyLIBCOUCHBASE_API lcbtrace_TRACER *lcbtrace_new(lcb_INSTANCE *instance, uint64_t flags)
236b7f4186SDavid Kelly{
246b7f4186SDavid Kelly    switch (flags) {
256b7f4186SDavid Kelly        case LCBTRACE_F_THRESHOLD:
266b7f4186SDavid Kelly            if (nullptr == instance) {
276b7f4186SDavid Kelly                return nullptr;
286b7f4186SDavid Kelly            }
296b7f4186SDavid Kelly            return (new ThresholdLoggingTracer(instance))->wrap();
306b7f4186SDavid Kelly        case LCBTRACE_F_EXTERNAL: {
316b7f4186SDavid Kelly            lcbtrace_TRACER *retval = new lcbtrace_TRACER;
32398d6a5cSSergey Avseyev            retval->version = 0;
336b7f4186SDavid Kelly            retval->flags = LCBTRACE_F_EXTERNAL;
34398d6a5cSSergey Avseyev            retval->cookie = nullptr;
35398d6a5cSSergey Avseyev            retval->destructor = nullptr;
36398d6a5cSSergey Avseyev            retval->v.v0.report = nullptr;
37398d6a5cSSergey Avseyev            retval->v.v1.start_span = nullptr;
38398d6a5cSSergey Avseyev            retval->v.v1.end_span = nullptr;
39398d6a5cSSergey Avseyev            retval->v.v1.destroy_span = nullptr;
40398d6a5cSSergey Avseyev            retval->v.v1.add_tag_string = nullptr;
41398d6a5cSSergey Avseyev            retval->v.v1.add_tag_uint64 = nullptr;
426b7f4186SDavid Kelly            return retval;
436b7f4186SDavid Kelly        }
446b7f4186SDavid Kelly        default:
456b7f4186SDavid Kelly            return nullptr;
466b7f4186SDavid Kelly    }
476b7f4186SDavid Kelly}
486b7f4186SDavid Kelly
49dc71348aSSergey AvseyevLIBCOUCHBASE_API void lcbtrace_destroy(lcbtrace_TRACER *tracer)
50dc71348aSSergey Avseyev{
51dc71348aSSergey Avseyev    if (tracer && tracer->destructor) {
52dc71348aSSergey Avseyev        tracer->destructor(tracer);
53dc71348aSSergey Avseyev    }
54dc71348aSSergey Avseyev}
55dc71348aSSergey Avseyev
56dc71348aSSergey AvseyevLIBCOUCHBASE_API
57dc71348aSSergey Avseyevlcbtrace_SPAN *lcbtrace_span_start(lcbtrace_TRACER *tracer, const char *opname, uint64_t start, lcbtrace_REF *ref)
58dc71348aSSergey Avseyev{
59dc71348aSSergey Avseyev    lcbtrace_REF_TYPE type = LCBTRACE_REF_NONE;
60ec0aafceSSergey Avseyev    lcbtrace_SPAN *other = nullptr;
61dc71348aSSergey Avseyev    if (ref) {
62dc71348aSSergey Avseyev        type = ref->type;
63dc71348aSSergey Avseyev        other = ref->span;
64dc71348aSSergey Avseyev    }
656b7f4186SDavid Kelly    return new Span(tracer, opname, start, type, other, nullptr);
666b7f4186SDavid Kelly}
676b7f4186SDavid Kelly
686b7f4186SDavid KellyLIBCOUCHBASE_API
696b7f4186SDavid Kellylcb_STATUS lcbtrace_span_wrap(lcbtrace_TRACER *tracer, const char *opname, uint64_t start, void *external_span,
706b7f4186SDavid Kelly                              lcbtrace_SPAN **lcbspan)
716b7f4186SDavid Kelly{
726b7f4186SDavid Kelly    if (nullptr == *lcbspan && nullptr != external_span && nullptr != tracer && tracer->version == 1) {
736b7f4186SDavid Kelly        *lcbspan = new Span(tracer, opname, start, LCBTRACE_REF_NONE, nullptr, external_span);
746b7f4186SDavid Kelly        return LCB_SUCCESS;
756b7f4186SDavid Kelly    }
766b7f4186SDavid Kelly    return LCB_ERR_INVALID_ARGUMENT;
77dc71348aSSergey Avseyev}
78dc71348aSSergey Avseyev
79dc71348aSSergey AvseyevLIBCOUCHBASE_API
8007521d60SSergey Avseyevlcbtrace_TRACER *lcb_get_tracer(lcb_INSTANCE *instance)
81dc71348aSSergey Avseyev{
82ec0aafceSSergey Avseyev    return (instance && instance->settings) ? instance->settings->tracer : nullptr;
83dc71348aSSergey Avseyev}
84dc71348aSSergey Avseyev
85dc71348aSSergey AvseyevLIBCOUCHBASE_API
8607521d60SSergey Avseyevvoid lcb_set_tracer(lcb_INSTANCE *instance, lcbtrace_TRACER *tracer)
87dc71348aSSergey Avseyev{
886b7f4186SDavid Kelly    // TODO: this leaks the original one in the instance, _if_ it is
896b7f4186SDavid Kelly    // the default ThresholdLoggingTracer
90dc71348aSSergey Avseyev    if (instance && instance->settings) {
91dc71348aSSergey Avseyev        instance->settings->tracer = tracer;
92dc71348aSSergey Avseyev    }
93dc71348aSSergey Avseyev}
946b7f4186SDavid Kelly
956b7f4186SDavid Kellyvoid lcbtrace_span_add_host_and_port(lcbtrace_SPAN *span, lcbio_CONNINFO *info)
966b7f4186SDavid Kelly{
976b7f4186SDavid Kelly    if (span) {
98d4fcbc15SSergey Avseyev        lcbtrace_span_add_tag_str_nocopy(span, LCBTRACE_TAG_LOCAL_ADDRESS, info->ep_local.host);
99d4fcbc15SSergey Avseyev        lcbtrace_span_add_tag_str_nocopy(span, LCBTRACE_TAG_LOCAL_PORT, info->ep_local.port);
1006b7f4186SDavid Kelly        lcbtrace_span_add_tag_str_nocopy(span, LCBTRACE_TAG_PEER_ADDRESS, info->ep_remote.host);
1016b7f4186SDavid Kelly        lcbtrace_span_add_tag_str_nocopy(span, LCBTRACE_TAG_PEER_PORT, info->ep_remote.port);
1026b7f4186SDavid Kelly    }
1036b7f4186SDavid Kelly}
1046b7f4186SDavid Kelly
1056b7f4186SDavid Kellyconst char *dur_level_to_string(lcb_DURABILITY_LEVEL dur_level)
1066b7f4186SDavid Kelly{
1076b7f4186SDavid Kelly    switch (dur_level) {
1086b7f4186SDavid Kelly        case LCB_DURABILITYLEVEL_NONE:
1096b7f4186SDavid Kelly            return "none";
1106b7f4186SDavid Kelly        case LCB_DURABILITYLEVEL_MAJORITY:
1116b7f4186SDavid Kelly            return "majority";
1136b7f4186SDavid Kelly            return "majority_and_persist_to_active";
1146b7f4186SDavid Kelly        case LCB_DURABILITYLEVEL_PERSIST_TO_MAJORITY:
1156b7f4186SDavid Kelly            return "persist_to_majority";
1166b7f4186SDavid Kelly        default:
1176b7f4186SDavid Kelly            return "unknown";
1186b7f4186SDavid Kelly    }
1196b7f4186SDavid Kelly}