LTS C++ SDK
This LTS SDK provides a series of methods for reporting logs in the C++ language, enabling you to directly report logs to LTS in encoding mode.
Transport Protocol
HTTPS
Prerequisites
- You have obtained the name of the region where LTS is deployed.
- You have obtained the AK/SK of your HUAWEI ID.
- You have obtained a project ID of your Huawei Cloud account. For details, see API Credentials.
- You have obtained the IDs of the target log group and log stream in LTS.
Instructions
After a permission is modified, the new permission takes effect one day later. When logs are reported to LTS via the SDK, the time difference between the log reporting time you set and the current time must not exceed two days. Otherwise, the reported logs will be deleted by LTS.
Precautions
Hard-coded or plaintext AK and SK are risky. For security, encrypt your AK and SK, store them in the configuration file or as environment variables, and decrypt them during usage.
Installing the C++ SDK
- Obtain the LTS C++ SDK code.
git clone https://gitee.com/lordstar-habile/huaweicloud-lts-cpp-sdk.git
- Import the LTS C++ header file.
#include <sdk/Config.h> #include <sdk/Producer.h> #include <client/logger.h>
- Compile the code for reporting logs.
You can customize the callback methods for successful and failed log reporting. The implementation method is as follows: Create a CallBack object, implement the Success and Fail methods, and use them directly when sending logs.
typedef void (*callback)(Result*); class CallBack { public: callback Success; callback Fail; }; int Producer::SendLog(std::string groupId, std::string streamId, std::vector<Log*> log, CallBack* cb);
Example:
#include <sdk/Config.h> #include <sdk/Producer.h> #include <client/logger.h> Config* GetConfig() { Config* cfg = new Config(); cfg->Endpoint = "endpoint"; cfg->AccessKeyID = "ak"; cfg->AccessKeySecret = "sk"; cfg->RegionId = "region"; cfg->ProjectId = "pid"; cfg->validateProducerConfig(); return cfg; } Log* generateLog() { std::vector<std::string> logContentArr = {"content for this test no.1"}; std::map<std::string, std::string> labels = {{"keyA","valueA"}}; return GenerateLog(logContentArr, labels); } void userCallBack(Result* result) { if (result == nullptr) { return; } const char* isSuccess = "false"; if (result->successful) { isSuccess = "true"; } LogPrint(LOG_LEVEL_WARN, "userCallBack [%s]", isSuccess); } int main() { Config *cfg = GetConfig(); Producer *producerInstance = InitProducer(cfg); producerInstance->Start(); CallBack *cb = new CallBack(); cb->Success = userCallBack; cb->Fail = userCallBack; Log *log = generateLog(); producerInstance->SendLog("groupId", "streamId", std::vector<Log *>{log}, cb); SleepMs(60 * 60 * 1000); producerInstance->Close(); delete cb; delete cfg; }
Parameters
- Parameters for producer config
Parameter
Description
Type
Mandatory
Default Value
ProjectId
Project ID of the Huawei Cloud account.
String
Yes
-
AccessKeyId
AK of the Huawei Cloud account.
String
Yes
-
AccessKeySecret
SK of the Huawei Cloud account.
String
Yes
-
RegionName
Region where LTS is deployed.
String
Yes
-
Endpoint
Destination to which logs are reported.
String
Yes
-
TotalSizeInBytes
Maximum size of logs that can be cached in a producer instance.
int
No
104857600 (indicates 100 MB.)
MaxBlockSec
Maximum blocking time that the caller will wait on the send method when the producer's available space is insufficient. The default value is 6 seconds. The recommended value is 0 seconds.
- If maxBlockMs is set to a value greater than or equal to 0, the blocking time is enforced. If the memory space is still insufficient after the blocking time expires, an error will be reported and the logs will be discarded.
- If maxBlockMs is set to -1, the blocking continues until the logs are successfully sent to LTS and the logs will not be discarded.
long
No
60s
MaxIoWorkers
Size of the task pool for executing log sending tasks.
int
No
10 (default)
MaxBatchSize
Once the size of logs cached in a ProducerBatch reaches or exceeds the batchSizeThresholdInBytes value, this batch of logs will be sent to LTS.
int
No
524288 (indicates 0.5 MB.)
MaxBatchCount
Once the number of logs cached in a ProducerBatch reaches or exceeds the batchCountThreshold value, this batch of logs will be sent to LTS.
int
No
4096
LingerMs
Duration from the time when a ProducerBatch is created to the time when the logs can be sent.
int
No
2s
Retries
Number of allowed retry attempts for a ProducerBatch when the first attempt to send the logs cached in the ProducerBatch fails. The recommended value is 3. If the value of retries is less than or equal to 0, the ProducerBatch will be added to the failure queue after the first sending attempt fails.
int
No
10
BaseRetryBackoffMs
Backoff time for the first retry attempt.
long
No
0.1s
MaxRetryBackoffMs
Maximum backoff time for retry attempts.
long
No
50s
- Parameters of the GenerateLog method for generating logs The number of logs reported at a time is less than 4,096, and the size is less than 512 KB.
Parameter
Description
Type
Mandatory
contents
Content of a batch of logs.
[]string
Yes
labels
Log label, in map format.
map[string]string
Yes
- Parameters of the SendLog method for reporting logs
Table 1 Parameters of the SendLog method Parameter
Description
Type
Mandatory
groupId
Log group ID.
long
Yes
streamId
Log stream ID.
String
Yes
log
Log structure.
struct log
Yes
Obtaining Parameters
- See the following region information for using Huawei Cloud hosts:
Table 2 Regions Region Name
Region
Endpoint
CN North-Beijing4
cn-north-4
https://lts-access.cn-north-4.myhuaweicloud.com:8102
CN East-Shanghai1
cn-east-3
https://lts-access.cn-east-3.myhuaweicloud.com:8102
CN South-Guangzhou
cn-south-1
https://lts-access.cn-south-1.myhuaweicloud.com:8102
AP-Singapore
ap-southeast-3
https://lts-access.ap-southeast-3.myhuaweicloud.com:8102
The SDK supports cross-cloud and local log reporting only in regions CN North-Beijing4, CN East-Shanghai1, and CN South-Guangzhou. It uses endpoint port 443.
- To obtain a log group's ID, choose Log Management in the navigation pane of the LTS console and hover over the target log group's name.
- To obtain a log stream's ID, click
next to the corresponding log group and hover over the target log stream's name.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot