api.md 6.9 KB
Newer Older
T
terence.fan 已提交
1
# Cat Client for C
F
fantengyuan 已提交
2 3 4 5

## Quickstart

```c
6
#include "client.h"
F
fantengyuan 已提交
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

void test() {
    /**
     * A message tree will be created if the current transaction stack is empty.
     */
    CatTransaction *t1 = newTransaction("foo", "bar1");

    /**
     * Metric can be logged anywhere and won't be recorded in the message tree.
     */
    logMetricForCount("metric-count", 1);
    logMetricForDuration("metric-duration", 200);

    /**
     * Log a completed transaction with a specified duration.
     */
    newCompletedTransactionWithDuration("foo", "bar2-completed", 1000);

    /**
     * Transaction can be nested.
     * The new transaction will be pushed into the stack.
     */
    CatTransaction *t3 = newTransaction("foo", "bar3");
    t3->setStatus(t3, CAT_SUCCESS);
    /**
32 33
     * Once you complete the transaction.
     * The transaction will be popped from the stack and the duration will be calculated.
F
fantengyuan 已提交
34 35 36 37 38 39 40 41 42 43 44
     */
    t3->complete(t3);

    char buf[10];
    for (int k = 0; k < 3; k++) {
        /**
         * Create a transaction with a specified duration.
         */
        CatTransaction *t4 = newTransactionWithDuration("foo", "bar4-with-duration", 1000);
        snprintf(buf, 9, "bar%d", k);
        /**
45
         * Log an event, it will be added to the children list of the current transaction.
F
fantengyuan 已提交
46 47 48 49 50 51 52 53
         */
        logEvent("foo", buf, CAT_SUCCESS, NULL);
        t4->setStatus(t4, CAT_SUCCESS);
        t4->complete(t4);
    }

    t1->setStatus(t1, CAT_SUCCESS);
    /**
54 55 56
     * Complete the transaction and poped it from the stack.
     * When the last element of the stack is popped.
     * The message tree will be encoded and sent to server.
F
fantengyuan 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
     */
    t1->complete(t1);
}

int main(int argc, char **argv) {
    CatClientConfig config = DEFAULT_CCAT_CONFIG;
    config.enableHeartbeat = 0;
    config.enableDebugLog = 1;
    catClientInitWithConfig("ccat", &config);
    test();
    Sleep(3000);
    catClientDestroy();
    return 0;
}
```

## API list

### Common apis

#### catClientInit

79
initialize `ccat` by default configs.
F
fantengyuan 已提交
80 81 82 83 84 85 86 87 88 89 90

```c
int catClientInit(const char *appkey);
```

This is equivalent to the following codes.

```c
return catClientInitWithConfig(appkey, &DEFAULT_CCAT_CONFIG);
```

91 92 93 94 95 96 97
With following configs:

* `encoderType` = CAT_ENCODER_BINARY.
* `enableHeartbeat` is true.
* `enableSampling` is true.
* `enableMultiprocessing` is false.
* `enableDebugLog` is false.
F
fantengyuan 已提交
98 99 100

#### catClientInitWithConfig

101
You may want to customize the `ccat` in some cases.
F
fantengyuan 已提交
102 103 104 105 106 107 108 109 110 111

```c
CatClientConfig config = DEFAULT_CCAT_CONFIG;
config.enableHeartbeat = 0;
config.enableDebugLog = 1;
catClientInitWithConfig("ccat", &config);
```

#### catClientDestroy

112
Disable the `ccat`, shutdown `sender`, `monitor` and `aggregator` threads.
F
fantengyuan 已提交
113

T
terence.fan 已提交
114
And then release all the resources that have been used.
F
fantengyuan 已提交
115 116 117 118 119 120 121

```c
int catClientDestroy();
```

#### isCatEnabled

122
Represent if `ccat` has been initialized.
F
fantengyuan 已提交
123 124 125 126 127 128 129

```c
int isCatEnabled();
```

### Transaction

130
You can find more information about the properties of a transaction in [Message properties](../../README.md#message-properties)
F
fantengyuan 已提交
131 132 133 134 135 136 137 138 139

#### newTransaction

Create a transaction.

```c
CatTransaction *newTransaction(const char *type, const char *name);
```

140
We hide the properties of a transaction (due to safety reasons), but we offered a list of APIs to help you to edit them.
F
fantengyuan 已提交
141 142 143 144 145 146 147 148 149 150

* addData
* addKV
* setStatus
* setTimestamp
* complete
* addChild
* setDurationInMillis
* setDurationStart

151
These can be easily used, for example:
F
fantengyuan 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

```c
CatTransaction *t = newTransaction("Test1", "A");
t->setStatus(t, CAT_SUCCESS);
t->setTimestamp(t, GetTime64() - 5000);
t->setDurationStart(t, GetTime64() - 5000);
t->setDurationInMillis(t, 4200);
t->addData(t, "data");
t->addKV(t, "k1", "v1");
t->addKV(t, "k2", "v2");
t->complete(t);
```

There is something you may want to know:

167
1. You can call `addData` and `addKV` several times, the added data will be connected by `&`.
168 169
2. It's meaningless to specify `duration` and `durationStart` in the same transaction, although we do it in the example :)
3. Never forget to complete the transaction! Or you will get corrupted message trees and memory leaks!
F
fantengyuan 已提交
170 171 172 173 174

#### newTransactionWithDuration

Create a transaction with a specified duration in milliseconds.

175
Due to `duration` has been specified, it won't be recalculated after the transaction has been completed.
F
fantengyuan 已提交
176

177
> Note that the transaction has not been completed! So it is necessary to complete it manually.
F
fantengyuan 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190

```c
CatTransaction *newTransactionWithDuration(const char *type, const char *name, unsigned long long duration);
```

This is equivalent to the following codes:

```
CatTransaction *t = newTransaction("type", "name");
t->setDurationInMillis(t4, duration);
return t;
```

191
> Once again, don't forget to complete the transaction!
F
fantengyuan 已提交
192 193 194

#### newCompletedTransactionWithDuration

195
Log a transaction with a specified duration in milliseconds and complete it.
F
fantengyuan 已提交
196

197
Due to the transaction has been auto-completed, the `timestamp` will be turned back.
F
fantengyuan 已提交
198 199 200

> Note that the specified duration should be less than 60,000 milliseconds.
>
201
> Or we won't turn back the timestamp.
F
fantengyuan 已提交
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

```c
int duration = 1000;
newCompletedTransactionWithDuration("type", "name", duration);
```

This is equivalent to the following codes:

```c
// return current timestamp in milliseconds.
unsigned long GetTime64();

CatTransaction *t = newTransaction("type", "name");
t->setDurationInMillis(t, duration);
if (duration < 60000) {
    t->setTimestamp(t, GetTime64() - duration);
}
t->complete(t);
return;
```

### Event

The event is just a simplified transaction, which has no duration.

#### logEvent

Log an event message.

```c
void logEvent(const char *type, const char *name, const char *status, const char *data);
```

#### logError

Log an error message.

```c
void logError(const char *msg, const char *errStr);
```

This is equivalent to the following codes:

```c
logEvent("Exception", msg, CAT_ERROR, errStr);
```

#### newEvent

Create an event.

Avoid of using this API unless you really have to do so.

Using logEvent / logError is a better idea.

```c
CatEvent *newEvent(const char *type, const char *name);
```

### Metric

#### logMetricForCount

```c
void logMetricForCount(const char *name, int quantity);
```

The metric will be sent every 1 second.

T
terence.fan 已提交
271
For example, if you have called this API 3 times in one second (can be in different threads, we use a concurrent hash map to cache the value), only the aggregated value (summarized) will be reported to the server.
F
fantengyuan 已提交
272 273 274 275 276 277 278

#### logMetricForDuration

```c
void logMetricForDuration(const char *name, unsigned long long duration);
```

T
terence.fan 已提交
279
Like `logMetricForCount`, the metrics that have been logged in the same second will be aggregated, the only difference is `averaged` value is used instead of `summarized` value.
F
fantengyuan 已提交
280 281 282 283 284 285 286

### Heartbeat

#### newHeartBeat

Create a heartbeat.

287
> Heartbeat is reported by ccat automatically,
F
fantengyuan 已提交
288 289 290 291
> so you don't have to use this API in most cases,
> unless you want to overwrite our heartbeat message.
>
> Don't forget to disable our built-in heartbeat if you do so.