Skip to Content
Technical Articles

Alert notifications for APIs using CPI

Hi experts,

Today I want to share with you a way to produce alert notifications for your APIs deployed in SAP API Management using CPI.

Example of alert notification sent via email:

For this you can use the same OData Service used by the Analytics view in SAP API Management called “Results”:

OData Service definition:

“https://prodapiportalapimgmtpr-<your sub-account>.hana.ondemand.com/apiportal/plugins/1.0/Results.svc/$metadata”

<?xml version="1.0"?>
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
  <edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
    <Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="workspace">
      <EntityType Name="ResultsType">
        <Key>
          <PropertyRef Name="ID"/>
        </Key>
        <Property Name="ID" Type="Edm.String" Nullable="false" MaxLength="2147483647"/>
        <Property Name="id" Type="Edm.String" MaxLength="255"/>
        <Property Name="http_status_code" Type="Edm.String"/>
        <Property Name="environment" Type="Edm.String" MaxLength="40"/>
        <Property Name="organization" Type="Edm.String" MaxLength="40"/>
        <Property Name="request_verb" Type="Edm.String" MaxLength="10"/>
        <Property Name="request_uri" Type="Edm.String" MaxLength="1000"/>
        <Property Name="proxy" Type="Edm.String" MaxLength="1000"/>
        <Property Name="proxy_base_path" Type="Edm.String" MaxLength="1000"/>
        <Property Name="x_forwarded_for_ip" Type="Edm.String" MaxLength="40"/>
        <Property Name="user_agent" Type="Edm.String" MaxLength="512"/>
        <Property Name="api_provider_url" Type="Edm.String" MaxLength="1000"/>
        <Property Name="api_provider_host" Type="Edm.String" MaxLength="256"/>
        <Property Name="api_provider_basepath" Type="Edm.String" MaxLength="1000"/>
        <Property Name="api_provider_ip" Type="Edm.String" MaxLength="40"/>
        <Property Name="client_sent_start_time" Type="Edm.DateTime"/>
        <Property Name="client_sent_end_time" Type="Edm.DateTime"/>
        <Property Name="client_recieve_start_time" Type="Edm.DateTime"/>
        <Property Name="client_recieve_end_time" Type="Edm.DateTime"/>
        <Property Name="api_provider_sent_start_time" Type="Edm.DateTime"/>
        <Property Name="api_provider_sent_end_time" Type="Edm.DateTime"/>
        <Property Name="api_provider_recieve_start_time" Type="Edm.DateTime"/>
        <Property Name="api_provider_recieve_end_time" Type="Edm.DateTime"/>
        <Property Name="month" Type="Edm.Int32"/>
        <Property Name="year" Type="Edm.Int32"/>
        <Property Name="day" Type="Edm.Int32"/>
        <Property Name="week" Type="Edm.Int32"/>
        <Property Name="hour" Type="Edm.Int32"/>
        <Property Name="minutes" Type="Edm.Int32"/>
        <Property Name="seconds" Type="Edm.Int32"/>
        <Property Name="api_provider_status_code" Type="Edm.Int32"/>
        <Property Name="product_version" Type="Edm.String" MaxLength="10"/>
        <Property Name="product_title" Type="Edm.String" MaxLength="255"/>
        <Property Name="api_version" Type="Edm.String" MaxLength="10"/>
        <Property Name="api_title" Type="Edm.String" MaxLength="255"/>
        <Property Name="provider_id" Type="Edm.String" MaxLength="255"/>
        <Property Name="application_version" Type="Edm.String" MaxLength="10"/>
        <Property Name="app_title" Type="Edm.String" MaxLength="255"/>
        <Property Name="developer_id" Type="Edm.String" MaxLength="255"/>
        <Property Name="developer_external_id" Type="Edm.String" MaxLength="128"/>
        <Property Name="product_id" Type="Edm.String" MaxLength="255"/>
        <Property Name="api_id" Type="Edm.String" MaxLength="255"/>
        <Property Name="application_id" Type="Edm.String" MaxLength="255"/>
        <Property Name="period_hour" Type="Edm.Int32"/>
        <Property Name="period_year" Type="Edm.Int32"/>
        <Property Name="period_month" Type="Edm.Int32"/>
        <Property Name="period_day" Type="Edm.Int32"/>
        <Property Name="period_minute" Type="Edm.Int32"/>
        <Property Name="period_week" Type="Edm.Int32"/>
        <Property Name="response_time" Type="Edm.Int64"/>
        <Property Name="error" Type="Edm.Int64"/>
        <Property Name="is_error" Type="Edm.Int64"/>
        <Property Name="request_processing_latency" Type="Edm.Int64"/>
        <Property Name="response_processing_latency" Type="Edm.Int64"/>
        <Property Name="request_size" Type="Edm.Int64"/>
        <Property Name="api_provider_response_time" Type="Edm.Int64"/>
        <Property Name="cache_hit" Type="Edm.Int64"/>
        <Property Name="api_provider_error" Type="Edm.Int64"/>
        <Property Name="policy_error" Type="Edm.Int64"/>
        <Property Name="min_request_size" Type="Edm.Int64"/>
        <Property Name="max_request_size" Type="Edm.Int64"/>
        <Property Name="min_response_time" Type="Edm.Int64"/>
        <Property Name="max_response_time" Type="Edm.Int64"/>
        <Property Name="min_api_provider_response_time" Type="Edm.Int64"/>
        <Property Name="max_api_provider_response_time" Type="Edm.Int64"/>
        <Property Name="min_request_processing_latency" Type="Edm.Int64"/>
        <Property Name="max_request_processing_latency" Type="Edm.Int64"/>
        <Property Name="min_response_processing_latency" Type="Edm.Int64"/>
        <Property Name="max_response_processing_latency" Type="Edm.Int64"/>
        <Property Name="average_request_size" Type="Edm.Decimal" Precision="34"/>
        <Property Name="average_response_time" Type="Edm.Decimal" Precision="34"/>
        <Property Name="average_api_provider_response_time" Type="Edm.Decimal" Precision="34"/>
        <Property Name="average_request_processing_latency" Type="Edm.Decimal" Precision="34"/>
        <Property Name="average_response_processing_latency" Type="Edm.Decimal" Precision="34"/>
        <Property Name="average_error" Type="Edm.Decimal" Precision="34"/>
        <Property Name="average_api_provider_error" Type="Edm.Decimal" Precision="34"/>
        <Property Name="average_policy_error" Type="Edm.Decimal" Precision="34"/>
        <Property Name="hits" Type="Edm.Int64"/>
        <Property Name="custom_dimensions" Type="Edm.String" MaxLength="512"/>
        <Property Name="custom_metrics" Type="Edm.String" MaxLength="512"/>
        <Property Name="cust_dim" Type="Edm.String" MaxLength="512"/>
        <Property Name="cust_msr" Type="Edm.String" MaxLength="512"/>
        <Property Name="gt" Type="Edm.String" MaxLength="512"/>
        <Property Name="lt" Type="Edm.String" MaxLength="512"/>
        <Property Name="ne" Type="Edm.String" MaxLength="512"/>
        <Property Name="developer_value" Type="Edm.String" MaxLength="255"/>
      </EntityType>
      <EntityContainer Name="APIManagementAnalytics" m:IsDefaultEntityContainer="true">
        <EntitySet Name="Results" EntityType="workspace.ResultsType"/>
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

So using this OData Service you can get the necessary data to generate the alert notifications, for example you want to get the errors in a period of time:

Request:

https://prodapiportalapimgmtpr-<sub-account>.hana.ondemand.com/apiportal/plugins/1.0/Results.svc/Results?$select=error&$filter=(period_year eq 2021 and period_month eq 1 and period_day eq 1 and period_hour eq 0 and period_minute eq 0 ) and (period_year eq 2021 and period_month eq 1 and period_day eq 10 and period_hour eq 0 and period_minute eq 0 )&$format=json

Response:

{
    "d": {
        "results": [
            {
                "__metadata": {
                    "id": "https://prodapiportalapimgmtpr-<sub-account>.hana.ondemand.com:443/apiportal/plugins/1.0/Results.svc/Results('1')",
                    "uri": "https://prodapiportalapimgmtpr-<sub-account>.hana.ondemand.com:443/apiportal/plugins/1.0/Results.svc/Results('1')",
                    "type": "workspace.ResultsType"
                },
                "error": "7"
            }
        ]
    }
}

Based in the same concept you can adjust the “$select” & “$filter” parameters to build your custom alert notification and send it from CPI:

 

Enjoy it 😉

 

Kind Regards.

Max.

1 Comment
You must be Logged on to comment or reply to a post.