from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from volcengine.tls.TLSService import TLSService
from volcengine.tls.consumer.consumer import TLSConsumer, LogProcessor
from volcengine.tls.consumer.consumer_model import ConsumerConfig
from volcengine.tls.log_pb2 import LogGroupList
# Implement a class that inherits LogProcessor. Implement a process method to handle LogGroupList generated after log data is consumed each time as needed.
# The following sample code prints each log entry.
class MyLogProcessor(LogProcessor):
def process(self, topic_id: str, shard_id: int, log_group_list: LogGroupList):
print(topic_id + " --- " + str(shard_id))
for log_group in log_group_list.log_groups:
for log in log_group.logs:
print("*** Count = {} ***".format(count))
for content in log.contents:
print("{}: {}".format(content.key, content.value))
if __name__ == "__main__":
# When you initialize the client, we recommend that you use environment variables to dynamically get identity authentication information such as Volcano Engine AccessKey. Otherwise, hard-coded AccessKey information leads to data security risks.
# If you use Security Token Service (STS), use a temporary AccessKey ID and AccessKey secret, and set VOLCENGINE_TOKEN. If you do not use STS, leave VOLCENGINE_TOKEN empty.
endpoint = os.environ["VOLCENGINE_ENDPOINT"]
region = os.environ["VOLCENGINE_REGION"]
access_key_id = os.environ["VOLCENGINE_ACCESS_KEY_ID"]
access_key_secret = os.environ["VOLCENGINE_ACCESS_KEY_SECRET"]
# Instantiate the TLS client.
tls_service = TLSService(endpoint, access_key_id, access_key_secret, region)
# Set the required parameters of the consumer group. The ConsumerConfig constructor presets some default parameters, which can be changed as needed.
consumer_config = ConsumerConfig(project_id="your-project-id",,
consumer_group_name="python-consumer-group",
consumer_name="python-consumer",
topic_id_list=["your-topic-id"])
tls_consumer = TLSConsumer(consumer_config, tls_service, MyLogProcessor())
# Call tls_consumer.start() to consume log data continuously.
# Call tls_consumer.stop() to end consumption by the consumer group.