Updated on 2025-07-28 GMT+08:00

LTS Go SDK

This LTS SDK provides a series of methods for reporting logs in the Go language, enabling you to directly report logs to LTS in encoding mode.

Transport Protocol

HTTPS

Prerequisites

Instructions

  • After a permission is modified, the new permission takes effect one day later.
  • When logs are reported to LTS via the Go 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.
  • The Go 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.

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 Go SDK

  1. Obtain the LTS Go SDK package.

    go get github.com/huaweicloud/huaweicloud-lts-sdk-go

  2. Import the LTS Go SDK package.

    import github.com/huaweicloud/huaweicloud-lts-sdk-go

  3. Compile the code for reporting logs.

    You can customize the callback methods for successful and failed log reporting. The implementation method is as follows: Define the structure of ErrorHandler and the Success and Fail methods, and use them directly when sending logs.

    SendLogWithCallBack(groupId, streamId string, log *Log, callback CallBack)

    Example code:

    package main
    
    import (
    "github.com/huaweicloud/huaweicloud-lts-sdk-go"
    "fmt"
    "sync"
    "time"
    )
    
    var (
    ak        = ""
    sk        = ""
    pid       = ""
    groupId   = ""
    streamId1 = ""
    streamId2 = ""
    region    = ""
    endpoint  = ""
    )
    
    func main() {
    // Obtain the default configuration.
    producerConfig := producer.GetConfig()
    // Log reporting address
    producerConfig.Endpoint = endpoint
    // accessKeyId: AK of the Huawei Cloud account. Hard-coded or plaintext AK is risky. For security, encrypt your AK, store it in the configuration file or as an environment variable, and decrypt it during usage.
    producerConfig.AccessKeyID = os.Getenv("accessKeyId")
    // accessKeySecret: SK of the Huawei Cloud account. Hard-coded or plaintext SK is risky. For security, encrypt your SK, store it in the configuration file or as an environment variable, and decrypt it during usage.
    producerConfig.AccessKeySecret = os.Getenv("accessKeySecret")
    // Region name
    producerConfig.RegionId = region
    // Account project ID
    producerConfig.ProjectId = pid
    // Initialize the producer instance.
    producerInstance := producer.InitProducer(producerConfig)
    // Start the producer.
    producerInstance.Start()
    wg := sync.WaitGroup{}
    for i := 0; i < 10; i++ {
    wg.Add(1)
    go func() {
    for j := 0; j < 1000; j++ {
    labels := make(map[string]string)
    labels["keyA"] = "valueA"
    labels["keyB"] = "valueB"
    labels["keyC"] = "valueC"
    
    logContent := fmt.Sprintf("content for this test %d", j)
    // Generate logs and fill in log content and labels.
    log := producer.GenerateLog([]string{logContent}, labels)
    // Send logs to log groups and log streams.
    err := producerInstance.SendLog(groupId, streamId1, log)
    err = producerInstance.SendLog(groupId, streamId2, log)
    if err != nil {
    fmt.Println(err)
    }
    
    handle := ErrorHandle{}
    // Send logs using the method with CallBack. If the sending fails, the Fail method of ErrorHandler is called.
    err = producerInstance.SendLogWithCallBack("groupId", "streamId1", log, handle)
    err = producerInstance.SendLogWithCallBack("groupId", "streamId1", log, handle)
    if err != nil {
        fmt.Println(err)
    }
    // Method for reporting structured logs
    var sLog producer.StructLog
    logContent := make(map[string]string)
    logContent["keyA"] = "valueA"
    logContent["keyB"] = "valueB"
    logContent1 := make(map[string]string)
    logContent1["keyA1"] = "valueA1"
    logContent1["keyB1"] = "valueB1"
    sLog.Contents = append(sLog.Contents, logContent)
    sLog.Contents = append(sLog.Contents, logContent1)
    sLog.Time = time.Now().UnixNano() / 1000 /1000
    // Send structured logs.
    err = producerInstance.SendLogStruct("groupId2", "streamId2", &sLog)
    if nil != err {
        continue
    }
    // Send structured logs with CallBack. If the sending fails, the Fail method of ErrorHandler is called.
    var callback ErrorHandle
    err = producerInstance.SendLogStructWithCallBack("groupId2", "streamId2", &sLog, callback)
    if nil != err {
        continue
    }
    time.Sleep(1 * time.Microsecond)
    }
    
    wg.Done()
    fmt.Printf("test func finished\n")
    }()
    }
    
    wg.Wait()
    fmt.Printf("send all complete ...")
    // Disable the sending instance.
    producerInstance.Close(60 * 1000)
    time.Sleep(10 * 60 * time.Second)
    }
    
    // errorHandle can call the callback functions for successful and failed sending. This is useful for printing error logs and locating faults.
    type ErrorHandle struct{}
    
    // Callback method when the sending is successful.
    func (ErrorHandle) Success(result *producer.Result) {
        fmt.Printf("send log to lts success, success flag: %v\n", result.IsSuccessful())
    }
    
    // Callback method when the sending fails. You can print the error information in result to facilitate fault locating.
    func (ErrorHandle) Fail(result *producer.Result) {
        fmt.Printf("send log to lts error, success flag: %v requestId: %s, httpcode: %d, errorCode: %s, errorMsg: %s\n",
           result.IsSuccessful(), result.GetRequestId(), result.GetHttpCode(), result.GetErrorCode(), result.GetErrorMessage())
    }

Parameters

  • Parameters for producer config
    Table 1 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

    Domain name or IP address of the server 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 60 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.

    Table 2 Parameters of the GenerateLog method

    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 3 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

  • Parameters of the StructLog method for reporting structured logs
    Table 4 Parameters of the StructLog method

    Parameter

    Description

    Type

    Mandatory

    Time

    Log reporting time.

    int64

    No

    Contents

    Log content.

    []map[string]string

    Yes

  • Parameters of the SendLogStruct method for reporting structured logs
    Table 5 Parameters of the SendLogStruct method

    Parameter

    Description

    Type

    Mandatory

    groupId

    Log group ID.

    String

    Yes

    streamId

    Log stream ID.

    String

    Yes

    log

    Log structure.

    Struct Logstruct

    Yes

Obtaining Parameters

  • See the following region information for using Huawei Cloud hosts:
    Table 6 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.