client.c 7.2 KB
Newer Older
1
#include "client.h"
F
fantengyuan 已提交
2 3 4

#include <lib/cat_clog.h>
#include <lib/cat_time_util.h>
5
#include <ccat/version.h>
F
fantengyuan 已提交
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

#include "client_config.h"
#include "context.h"
#include "message.h"
#include "message_aggregator.h"
#include "message_aggregator_metric.h"
#include "message_id.h"
#include "message_manager.h"
#include "message_sender.h"
#include "monitor.h"
#include "server_connection_manager.h"

#ifdef WIN32
#ifdef CAT_MEMLEAK_DETECT
#include "vld.h"
#endif
#elif defined(__linux)
#include <signal.h>
#endif

static volatile int g_cat_init = 0;

volatile sds g_single_process_pid_str = NULL;

extern volatile int g_cat_enabledFlag;

extern CatMessage g_cat_nullMsg;
extern CatTransaction g_cat_nullTrans;

CatClientConfig DEFAULT_CCAT_CONFIG = {
        CAT_ENCODER_BINARY,
        1,  // enable heartbeat
        1,  // enable sampling
        0,  // disable multiprocessing
        0,  // disable debug log
};

int catClientInit(const char* appkey) {
    return catClientInitWithConfig(appkey, &DEFAULT_CCAT_CONFIG);
}

int catClientInitWithConfig(const char *appkey, CatClientConfig* config) {
    if (g_cat_init) {
        return 0;
    }
    g_cat_init = 1;

    signal(SIGPIPE, SIG_IGN);

    initCatClientConfig(config);

    if (loadCatClientConfig(DEFAULT_XML_FILE) < 0) {
        g_cat_init = 0;
        g_cat_enabledFlag = 0;
        INNER_LOG(CLOG_ERROR, "Failed to initialize cat: Error occurred while parsing client config.");
        return 0;
    }
    g_config.appkey = catsdsnew(appkey);

    initMessageManager(appkey, g_config.selfHost);
    initMessageIdHelper();

    if (!initCatServerConnManager()) {
        g_cat_init = 0;
        g_cat_enabledFlag = 0;
        INNER_LOG(CLOG_ERROR, "Failed to initialize cat: Error occurred while getting router from cat-server.");
        return 0;
    }

    initCatAggregatorThread();
    initCatSenderThread();
    initCatMonitorThread();

    g_cat_enabledFlag = 1;
    INNER_LOG(CLOG_INFO, "Cat has been successfully initialized with appkey: %s", appkey);

    return 1;
}

int catClientDestroy() {
    g_cat_enabledFlag = 0;
    g_cat_init = 0;

    clearCatMonitor();
    catMessageManagerDestroy();
    clearCatAggregatorThread();
    clearCatSenderThread();
    clearCatServerConnManager();
    destroyMessageIdHelper();
    clearCatClientConfig();
    return 1;
}

99 100 101 102
const char* catVersion() {
    return CCAT_VERSION;
}

F
fantengyuan 已提交
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
void logError(const char *msg, const char *errStr) {
    getContextMessageTree()->canDiscard = 0;
    logEvent("Exception", msg, CAT_ERROR, errStr);
}

void logEvent(const char *type, const char *name, const char *status, const char *data) {
    if (!isCatEnabled()) {
        return;
    }
    CatEvent *event = newEvent(type, name);
    catChecktPtr(event);
    if (event == NULL) {
        return;
    }
    if (data != NULL) {
        event->addData(event, data);
    }
    event->setStatus(event, status);
    event->complete(event);
}

void _logMetric(const char *name, const char *status, const char *value)
{
    CatMetric *metric = newMetric("", name);
    catChecktPtr(metric);

    if (value != NULL) {
        metric->addData(metric, value);
    }
    metric->setStatus(metric, status);
    metric->complete(metric);
}

void logMetricForCount(const char *name, int quantity) {
    if (!isCatEnabled()) {
        return;
    }

    if (g_config.enableSampling) {
        addCountMetricToAggregator(name, quantity);
        return;
    }

    if (quantity == 1) {
        _logMetric(name, "C", "1");
    } else {
        sds val = catsdsfromlonglong(quantity);
        catChecktPtr(val);
        _logMetric(name, "C", val);
        catsdsfree(val);
    }
}

void logMetricForDuration(const char *name, unsigned long long duration) {
    if (!isCatEnabled()) {
        return;
    }

    if (g_config.enableSampling) {
        addDurationMetricToAggregator(name, duration);
        return;
    }

    sds val = catsdsfromlonglong(duration);
    catChecktPtr(val);
    _logMetric(name, "T", val);
    catsdsfree(val);
}

CatEvent *newEvent(const char *type, const char *name) {
    if (!isCatEnabled()) {
        return &g_cat_nullMsg;
    }
    getCatContext();
    CatEvent *event = createCatEvent(type, name);
    catChecktPtr(event);
    return event;
}

CatMetric *newMetric(const char *type, const char *name) {
    if (!isCatEnabled()) {
        return &g_cat_nullMsg;
    }
    getCatContext();
    CatMetric *metric = createCatMetric(type, name);
    catChecktPtr(metric);
    return metric;
}

CatHeartBeat *newHeartBeat(const char *type, const char *name) {
    if (!isCatEnabled()) {
        return &g_cat_nullMsg;
    }
//    getCatContext();
    getContextMessageTree()->canDiscard = 0;

    CatHeartBeat *hb = createCatHeartBeat(type, name);
    catChecktPtr(hb);
    return hb;
}

CatTransaction *newTransaction(const char *type, const char *name) {
    if (!isCatEnabled()) {
        return &g_cat_nullTrans;
    }
    getCatContext();
    CatTransaction *trans = createCatTransaction(type, name);
    catChecktPtr(trans);
    if (trans == NULL) {
        return NULL;
    }
    catMessageManagerStartTrans(trans);
    return trans;
}

CatTransaction *newTransactionWithDuration(const char *type, const char *name, unsigned long long duration) {
    CatTransaction* trans = newTransaction(type, name);
    trans->setDurationInMillis(trans, duration);
    if (duration < 60 * 1000) {
        trans->setTimestamp(trans, GetTime64() - duration);
    }
    return trans;
}

void newCompletedTransactionWithDuration(const char *type, const char *name, unsigned long long duration) {
    CatTransaction* trans = newTransactionWithDuration(type, name, duration);
    trans->complete(trans);
}

char *createMessageId() {
    if (!isCatEnabled()) {
        return NULL;
    }
    return getNextMessageId();
}

char *createRemoteServerMessageId(const char *appkey) {
    if (!isCatEnabled()) {
        return NULL;
    }
    return getNextMessageIdByAppkey(appkey);
}

char *getThreadLocalMessageTreeId() {
    if (!isCatEnabled()) {
        return NULL;
    }
    return getContextMessageTree()->messageId;
}

char *getThreadLocalMessageTreeRootId() {
    if (!isCatEnabled()) {
        return NULL;
    }
    return getContextMessageTree()->rootMessageId;
}

char *getThreadLocalMessageTreeParentId() {
    if (!isCatEnabled()) {
        return NULL;
    }
    return getContextMessageTree()->parentMessageId;
}

void setThreadLocalMessageTreeId(char *messageId) {
    if (!isCatEnabled()) {
        return;
    }
    CatMessageTree *pTree = getContextMessageTree();
    if (pTree->messageId != NULL) {
        catsdsfree(pTree->messageId);
        pTree->messageId = NULL;
    }
    pTree->messageId = catsdsnew(messageId);
}

void setThreadLocalMessageTreeRootId(char *messageId) {
    if (!isCatEnabled()) {
        return;
    }
    CatMessageTree *pTree = getContextMessageTree();
    if (pTree->rootMessageId!= NULL) {
        catsdsfree(pTree->rootMessageId);
        pTree->rootMessageId = NULL;
    }
    pTree->rootMessageId = catsdsnew(messageId);
}

void setThreadLocalMessageTreeParentId(char *messageId) {
    if (!isCatEnabled()) {
        return;
    }
    CatMessageTree *pTree = getContextMessageTree();
    if (pTree->parentMessageId!= NULL) {
        catsdsfree(pTree->parentMessageId);
        pTree->parentMessageId = NULL;
    }
    pTree->parentMessageId = catsdsnew(messageId);
}