Wednesday 28 August 2019

SAP C/4HANA Extensibility – Triggering Lambdas with Events

This blog will detail how a Lambda function can be triggered to run when an event is published into the Kyma environment. To eliminate any barriers, we will utilize the SAP Commerce mock application to send events, but the process flow would be the same if using SAP Commerce. The setup of SAP Commerce can be found in this blog.

In the home workspace of your Kyma instance choose the commerce-mock application and then choose Create Binding and bind the application to your mocks namespace. This will provide access to the commerce-mock application in your mocks namespace.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

This will also result in one Bound Application showing in the Namespaces view. Choose the mocks namespace tile to open the namespace.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Choose the Catalog, Services and then choose SAP Commerce Cloud – Events, which is labeled with commerce-mock.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Choose Add to add a service instance, which will allow us to utilize the events within this namespace.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Accept the default values and choose Create Instance.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Next choose the Lambdas menu option and then choose Add Lambda.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

For the Function Name provide the name order-created. In the code view set the function code to:

module.exports = { main: function (event, context) { 
    console.log("hello from order created");
    console.log("Received data: ", event.data);
} }

The event.data will contain the data object that the event has published.

Next, bind the function to the order.created event of the commerce mock application by first choosing the Select Function Trigger button and then choose the Event Trigger option.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Find the event order.created by either utilizing the search field or by scrolling through the list. Select order.created and then choose the Add button to add the event trigger.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Review your entries for the order-created function to verify they match the following screenshot and then choose the Create button to initiate the deployment of the lambda function.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

The deployment of the lambda function may take a minute. Refresh the page until the STATUS is green.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Open the commerce mock application, choose Remote APIs and then choose SAP Commerce Cloud – Events.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

This will navigate you to a page where you can send an event and also provides details regarding the API. In Event Topics choose order.created.v1 from the drop down list for the desired event. This will place an entry into the field below, which can be edited as desired. This is the data object that will be sent and consumed by our lambda function. Press the Send Event button to trigger the event.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Viewing the Event Flow


Next, take a look at the tools provided by Kyma to help you troubleshoot and view the system interactions.

Within the mocks namespace choose the Lambdas menu option. In the lambda you created, order-created, choose the ellipse and then choose Show Logs.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Use the search options to filter the results or scroll through the records to find the output from sending the order.created event.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Next, explore the tracing features of Kyma, which are provided by Jaeger. The intent of this section is to outline how a trace should appear for a working function. Using a known working function should help identify possible focus areas in the case of troubleshooting issues.

In the home workspace choose the Tracing menu.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Tracing is done on a service base level. Within the Find Traces pane, set connector as the chosen service and then choose the Find Traces to show the results of the event order.created being sent. Notice how the event has spanned multiple services. Any of these services could be used as the selected service in the Find Traces to show the same information. Choose the trace to obtain further information.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

Here you can see the entire flow of trace in the order it was processed, and the time utilized by each service. Each of these services can then be selected to see more detailed information.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

The event-publish-knative-service showing the event information including the event-type.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

The istio-proxy.mocks showing the call to the order-created lambda.

SAP C/4HANA Extensibility, SAP HANA Tutorials and Materials, SAP HANA Online Exam, SAP HANA Learning, SAP HANA Studio

No comments:

Post a Comment