api.md 4.0 KB
Newer Older
F
fantengyuan 已提交
1 2 3 4 5 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
# cat c++ client

## Quickstart

```cpp
#include <iostream>
#include <unistd.h>

#include "cppcat/client.h"

using namespace std;

unsigned long long GetTime64() {
    return static_cast<unsigned long long int>(std::chrono::system_clock::now().time_since_epoch().count() / 1000);
}

void transaction() {
    cat::Transaction t("foo", "bar");
    t.AddData("foo", "1");
    t.AddData("bar", "2");
    t.AddData("foo is a bar");
    t.SetDurationStart(GetTime64() - 1000);
    t.SetTimestamp(GetTime64() - 1000);
    t.SetDurationInMillis(150);
    t.SetStatus(cat::FAIL);
    t.Complete();
}

void event() {
    cat::Event e("foo", "bar");
    e.AddData("foo", "1");
    e.AddData("bar", "2");
    e.AddData("foo is a bar");
    e.SetStatus(cat::SUCCESS);
    e.Complete();

    cat::logEvent("foo", "bar1");
    cat::logEvent("foo", "bar2", "failed");
    cat::logEvent("foo", "bar3", "failed", "k=v");
}

void metric() {
    cat::logMetricForCount("count");
    cat::logMetricForCount("count", 3);
    cat::logMetricForDuration("duration", 100);
}

int main() {
    cat::Config c = cat::Config();
    c.enableDebugLog = true;
    c.encoderType = cat::ENCODER_TEXT;
    cat::init("cppcat", c);

    for (int i = 0; i < 100; i++) {
        transaction();
        event();
        metric();
        usleep(10000);
    }
    usleep(1000000);
    cat::destroy();
}
```

## API list

67
All the `cppcat` apis are in the `cat` namespace.
F
fantengyuan 已提交
68 69 70 71 72

### Common apis

#### cat::init

73
initialize `cppcat` by default configs.
F
fantengyuan 已提交
74 75 76 77 78 79 80

```c
void init(const string& domain);
```

Following config is used by default.

81 82 83 84 85
* `encoderType` = CAT_ENCODER_BINARY.
* `enableHeartbeat` is true.
* `enableSampling` is true.
* `enableMultiprocessing` is false.
* `enableDebugLog` is false.
F
fantengyuan 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

You can also customize config. (Like to use text encoder instead of binary encoder)

```cpp
cat::Config c = cat::Config();
c.enableDebugLog = true;
c.encoderType = cat::ENCODER_TEXT;
cat::init("cppcat", c);
```

#### cat::destory

Disable the cat client, shutdown `sender`, `monitor` and `aggregator` threads.

Also release all the resources that have been used.

```cpp
void destroy();
```

### Transaction

108
You can find more information about the properties of a transaction in [Message properties](../../README.md#message-properties)
F
fantengyuan 已提交
109 110 111

#### cat::Transaction

112
Transaction can be easily created.
F
fantengyuan 已提交
113 114 115 116 117

```cpp
cat::Transaction t("type", "name");
```

118
Due to the properties of a transaction were mostly private, we offered a list of APIs to help you to edit them.
F
fantengyuan 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

* AddData
* SetStatus
* SetDurationStart
* SetDurationInMillis
* SetTimestamp
* Complete

These can be easily used, for example:

```cpp
cat::Transaction t("foo", "bar");
t.AddData("foo", "1");
t.AddData("bar", "2");
t.AddData("foo is a bar");
t.SetDurationStart(GetTime64() - 1000);
t.SetTimestamp(GetTime64() - 1000);
t.SetDurationInMillis(150);
t.SetStatus(cat::FAIL);
t.Complete();
```

There is something you may want to know:

143
1. You can call `AddData` several times, the added data will be connected by `&`.
144 145
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 已提交
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

### Event

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

#### cat::logEvent

Log an event message.

```cpp
void logEvent(const string& type, const string& name, const string& status = SUCCESS, const string& data = "");
```

### Metric

#### logMetricForCount

Log a count metric.

```cpp
void logMetricForCount(const string& key, unsigned int count = 1);
```

The metric will be sent every 1 second.

For example, if you 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.

#### logMetricForDuration

```cpp
void logMetricForDuration(const string& key, unsigned long ms);
```

179
Like `logMetricForCount`, the metrics reported in the same second will be aggregated, the only difference is `averaged` value is used instead of `summarized` value.