Sending a Custom Event
This section describes how to send a custom event.
The custom events generated by custom event sources are sent to EG. The EG service filters and converts the custom events based on the filter rule, and triggers the event target (a function in FunctionGraph).
Prerequisites
- You have completed the operations in Enabling EG and Authorizing Permissions.
- You have obtained the permission to access FunctionGraph.
Step 3: Create an Event Target (Create a Function)
- Log in to the FunctionGraph console.
- Choose Functions > Function List in the navigation pane.
- Click Create Function.
- Set function parameters, as shown in Figure 3. For details about the function parameters, see .
- Function Type: Select Event Function.
- Region: Select the region as required.
- Function Name: Enter test.
- Agency: Select Use no agency.
- Runtime: Select Python 2.7.
- Click Create.
- On the Code tab page of the function details page, enter the following code and click Deploy.
# -*- coding:utf-8 -*- import json def handler (event, context): print(json.dumps(event)) return { "statusCode": 200, "isBase64Encoded": False, "body": json.dumps(event), "headers": { "Content-Type": "application/json" } }
Step 4: Create an Event Subscription
Subscriptions bind event sources, channels, and targets. Events of sources are routed to targets based on specified rules.
- Log in to the EG console.
- In the navigation pane, choose Event Subscriptions.
- Click Create Event Subscription.
- Click next to the default subscription name.
- Enter CustomEvent in Subscription Name, and click OK.
- Configure an event source.
- Click Event Source, and set event source parameters as shown in Figure 4.
- Provider: Select Custom.
- Channel type: Select Existing for Type.
- Channel configuration: Select the channel created in Step 1 from Channel.
- Event source type: Select Existing for Type.
- Event source configuration: Select egsdk-source (created in Step 2) from Event Source.
- Filter Rule: Retain the default value and record the value of values, for example, egsdk-source in Figure 4.
- Click OK.
- Click Event Source, and set event source parameters as shown in Figure 4.
- Configure an event target.
- Click Save.
Step 5: Send a Custom Event
- Configure a custom event. For details, see CloudEvents SDK.
Modify the following parameters in the sample code for publishing an event:
- NAME: IAM username.
- PASSWORD: IAM user password.
- DOMAIN_NAME: Account name.
- IAM_ENDPOINT: IAM endpoint. For details, see "Regions and Endpoints".
- PROJECT_ID: Project ID. For details about how to obtain a project ID, see API Credentials
- CHANNEL_ID: Change the value to the channel ID recorded in Step 1.
- ENDPOINT: EG endpoint.
- SOURCE: Event source name. Change it to the value of values in Filter recorded in Step 4.
- TIME: Time when the event is generated.
- DATA: Event content that complies with the CloudEvents 1.0 specifications. Modify this parameter based on service requirements.
- Run the main function to publish the event.
Step 6: View Results
- Log in to the FunctionGraph console.
- Choose Functions > Function List in the navigation pane.
- Click the test function to go to the function details page.
- On the Metrics tab page, view the number of invocations and running duration.
Figure 6 Viewing metrics
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.