forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
weilong.pwl
committed
Aug 15, 2023
1 parent
8c55f03
commit cdbf3df
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
sls "github.com/aliyun/aliyun-log-go-sdk" | ||
consumerLibrary "github.com/aliyun/aliyun-log-go-sdk/consumer" | ||
"github.com/go-kit/kit/log/level" | ||
) | ||
|
||
// README : | ||
// This is a very simple example of pulling data from your logstore and printing it for consumption, including pre-handling for logs. | ||
|
||
func main() { | ||
option := consumerLibrary.LogHubConfig{ | ||
Endpoint: "", | ||
AccessKeyID: "", | ||
AccessKeySecret: "", | ||
Project: "", | ||
Logstore: "", | ||
ConsumerGroupName: "", | ||
ConsumerName: "", | ||
// This options is used for initialization, will be ignored once consumer group is created and each shard has been started to be consumed. | ||
// Could be "begin", "end", "specific time format in time stamp", it's log receiving time. | ||
CursorPosition: consumerLibrary.BEGIN_CURSOR, | ||
// Query is for log pre-handling before return to client, more info refer to https://help.aliyun.com/zh/sls/user-guide/rule-based-consumption | ||
Query: "* | where cast(body_bytes_sent as bigint) > 14000", | ||
} | ||
|
||
consumerWorker := consumerLibrary.InitConsumerWorkerWithCheckpointTracker(option, process) | ||
ch := make(chan os.Signal, 1) | ||
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) | ||
consumerWorker.Start() | ||
if _, ok := <-ch; ok { | ||
level.Info(consumerWorker.Logger).Log("msg", "get stop signal, start to stop consumer worker", "consumer worker name", option.ConsumerName) | ||
consumerWorker.StopAndWait() | ||
} | ||
} | ||
|
||
// Fill in your consumption logic here, and be careful not to change the parameters of the function and the return value, | ||
// otherwise you will report errors. | ||
func process(shardId int, logGroupList *sls.LogGroupList, checkpointTracker consumerLibrary.CheckPointTracker) (string, error) { | ||
fmt.Println(shardId, logGroupList) | ||
checkpointTracker.SaveCheckPoint(false) | ||
return "", nil | ||
} |