All Products
Search
Document Center

2. Author your function

Last Updated: Feb 22, 2019

After the previous step and your Service is ready, you can create new functions in the service. You can use the Function Compute console or the fcli tool to submit your functions. This guide provides sample code that uses the specified programming language to the code edit field in the Function Compute console.

In this guide, incremental log entries collected by Log Service trigger executing this function. Then, the function retrieves log entries, and prints them to Function Compute. For more information about programming, see Log Service topic Development guide for ETL function.

Sample function for Python 2.7

You can use this code example as the template for retrieving all logical-log files. The accessKeyId and accessKey must be either manually specified or obtained from context and creds objects.

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import json
  4. from aliyun.log import LogClient
  5. from time import time
  6. def logClient(endpoint, creds):
  7. logger = logging.getLogger()
  8. logger.info('creds info')
  9. logger.info(creds.access_key_id)
  10. logger.info(creds.access_key_secret)
  11. logger.info(creds.security_token)
  12. accessKeyId = 'your accessKeyId'
  13. accessKey = 'your accessKeyId scr'
  14. client = LogClient(endpoint, accessKeyId, accessKey)
  15. return client
  16. def handler(event, context):
  17. logger = logging.getLogger()
  18. logger.info('start deal SLS data')
  19. logger.info(event.decode().encode())
  20. info_arr = json.loads(event.decode())
  21. fetchdata(info_arr['source'])
  22. return 'hello world'
  23. def fetchdata(event):
  24. logger = logging.getLogger()
  25. endpoint = event['endpoint']
  26. creds = context.credentials
  27. client = logClient(endpoint, creds)
  28. if client == None :
  29. logger.info("client creat failed")
  30. return False
  31. project = event['projectName']
  32. logstore = event['logstoreName']
  33. start_cursor = event['beginCursor']
  34. end_cursor = event['endCursor']
  35. loggroup_count = 10
  36. shard_id = event['shardId']
  37. while True:
  38. res = client.pull_logs(project, logstore, shard_id, start_cursor, loggroup_count, end_cursor)
  39. res.log_print()
  40. next_cursor = res.get_next_cursor()
  41. if next_cursor == start_cursor :
  42. break
  43. start_cursor = next_cursor
  44. #log_data = res.get_loggroup_json_list()
  45. return True

Request parameters

The event parameter is the Function Compute object. The format of event is as follows:

  1. {
  2. "parameter": {},
  3. "source": {
  4. "endpoint": "http://cn-shanghai-intranet.log.aliyuncs.com",
  5. "projectName": "log-com",
  6. "logstoreName": "log-en",
  7. "shardId": 0,
  8. "beginCursor": "MTUyOTQ4MDIwOTY1NTk3ODQ2Mw==",
  9. "endCursor": "MTUyOTQ4MDIwOTY1NTk3ODQ2NA=="
  10. },
  11. "jobName": "1f7043ced683de1a4e3d8d70b5a412843d817a39",
  12. "taskId": "c2691505-38da-4d1b-998a-f1d4bb8c9994",
  13. "cursorTime": 1529486425
  14. }
  • parameter: Contains the function configuration of the trigger.

  • source: The information of the log block that the triggered function reads from Log Service.

    • endpoint: The region of the Log Service project.

    • projectName: The Log Service project name.

    • logstoreName: The Logstore name.

    • shardId: The specified shard in the Logstore.

    • beginCursor: The location on the shard where the triggered function starts consuming data.

    • endCursor: The location on the shard where the triggered function stops consuming data.

  • jobName: The name of the Extract, Transform, and Load (ETL) job in Log Service. A Log Service trigger in Function Compute corresponds to an ETL job in Log Service.

  • taskId: The identifier of the execution of the specified function. This execution corresponds to an ETL job.

  • cursorTime: The unix_timestamp of the last log entry that reaches Log Service. The function reads this data when retrieving log entries.

Next step

3. Test your function