StatsCollector 政策

您正在查看 Apigee Edge 文档。
前往 Apigee X 文档
信息

内容

可让您收集消息中的数据统计信息,例如产品 ID、价格、REST 操作、客户端和目标网址以及消息长度。数据可能来自 Apigee 预定义的流变量或您定义的自定义变量。

统计数据会传递到分析服务器,该服务器会分析统计数据并生成报告。您可以使用 Edge 管理界面或 Edge API 查看报告。

示例

基本示例

<StatisticsCollector name="publishPurchaseDetails">
  <Statistics>
    <Statistic name="productID" ref="product.id" type="string">999999</Statistic>
    <Statistic name="price" ref="product.price" type="string">0</Statistic>
  </Statistics>
</StatisticsCollector>

在此示例中,您将收集有关两个自定义变量(product.idproduct.price)的统计信息。在每次请求时,Statistics Collector 政策都会将这两个变量的值写入分析服务器。

此示例还为每个变量设置了一个可选的默认值,其中 product.id 的默认值为 999999,product.price 的默认值为 0。如果某个变量未定义,并且您未指定默认值,则系统不会记录该变量的任何数据。如果您指定了默认值,则当变量未定义时,系统会记录该默认值。

收集统计数据后,您可以使用 Edge 管理界面或 API 来检索统计信息。在检索统计信息时,您会分别将为这些变量收集的数据引用为 productIDprice

访问统计信息

在此示例中,您将使用 Edge 管理 API 查看名为 productID 的集合的统计数据。此请求会针对商品 ID 构建自定义报告,该报告基于每天为每个商品 ID 提交的消息数量总和。将变量 {org_name} 替换为您的组织名称,并将您在 Apigee Edge 上的账号的电子邮件地址和密码替换为 email:password

设置 timeRange 参数,以纳入数据收集的时间间隔。默认情况下,无法获得从当前日期起 6 个月之前的数据。如果您需要访问 6 个月之前的数据,请与 Apigee Edge 支持团队联系。

$ curl https://api.enterprise.apigee.com/v1/o/{org_name}/environments/test/stats/productID?"select=sum(message_count)&timeRange=1/19/2015%2000:00~6/21/2015%2000:00&timeUnit=day"
-u email:password

在响应中,名称字段包含商品 ID,值显示每天的请求数:

{
  "environments" : [ {
    "dimensions" : [ {
      "metrics" : [ {
        "name" : "sum(message_count)",
        "values" : [ {
          "timestamp" : 1353369600000,
          "value" : "4.0"
        } ]
      } ],
      "name" : "52"
    }, {
      "metrics" : [ {
        "name" : "sum(message_count)",
        "values" : [ {
          "timestamp" : 1353369600000,
          "value" : "19.0"
        } ]
      } ],
      "name" : "14"
    }, ...
  } ],
  "metaData" : {
    "samplingRate" : "100"
  }
}

提取数据以供收集

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="GetWeatherData">
    <VariablePrefix>weather</VariablePrefix>
    <XMLPayload>
       <Namespaces>
          <Namespace prefix="yweather">http://xml.weather.yahoo.com/ns/rss/1.0</Namespace>
       </Namespaces>
       <Variable name="location" type="string">
          <XPath>/rss/channel/yweather:location/@city</XPath>
       </Variable>
       <Variable name="condition" type="string">
          <XPath>/rss/channel/item/yweather:condition/@text</XPath>
       </Variable>
    </XMLPayload>
</ExtractVariables>

Statistics Collector 政策要求将收集的数据存储在变量中。 这些变量可以是 Apigee 预定义的变量,也可以是您定义的自定义变量。

在此示例中,您将使用提取变量政策从包含天气信息的 XML 载荷中提取数据。本政策的目的:

  • 提取城市名称并将其写入名为 weather.location 的变量
  • 提取当前天气状况并将其写入名为 weather.condition 的变量

然后,您可以使用 Statistics Collector 政策来收集有关变量的信息,如下所示:

<StatisticsCollector name="publishPurchaseDetails">
  <Statistics>
    <Statistic name="weatherLocation" ref="weather.location" type="string"></Statistic>
    <Statistic name="weatherCondition" ref="weather.condition" type="string"></Statistic>
  </Statistics>
</StatisticsCollector>

如需查看有关如何从 XML 载荷中提取数据以用于 Statistics Collector 政策的完整教程,请参阅使用自定义分析分析 API 消息内容


关于 Statistics Collector 政策

执行时,Statistics Collector 政策会记录一个或多个变量的当前值。这些变量可以是 Apigee 预定义的流变量,也可以是您定义的自定义变量。这些数据会写入到 Edge 分析服务器。

如需访问政策收集的统计数据,您可以使用 Edge API 或 Edge 管理界面。例如,您可以使用 Edge 管理界面创建自定义报告,以多种格式显示收集的数据。

如需查看有关使用 Statistics Collector 政策的完整教程,请参阅使用自定义分析分析 API 消息内容

政策的放置位置

您可以将 StatisticsCollector 政策放入 API 代理的请求流或响应流。但是,如果您将多个 Statistics Collector 政策放入一个代理,则由要执行的最后一个政策确定要写入分析服务器的数据,并且根据以前的任何 Statistics Collector 政策写入的数据都将丢失。

一个 API 代理可能包含多个 StatisticsCollector 政策的一种情况是:请求流或响应流中各有一个政策,而故障处理程序中包含另一个政策。如果 API 代理中出现故障,则由故障处理程序中的 StatisticsCollector 确定要收集的数据。您可以使用该统计信息收集器记录有关故障的信息,或记录您认为必要的任何其他信息。故障处理程序中的 Statistics Collector 会确定要收集的数据,无论请求/响应中的 Statistics Collector 是否已执行。

如需了解详情,请参阅处理故障

元素参考

元素参考介绍了统计信息收集器政策的元素和属性。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StatisticsCollector async="false" continueOnError="false" enabled="true" name="Statistics-Collector-1">
    <DisplayName>Statistics Collector 1</DisplayName>
    <Statistics>
        <Statistic name="statName" ref="varName" type="refDataType">defaultStatValue</Statistic>
    </Statistics>
</StatisticsCollector>

<StatisticsCollector> 属性

<StatisticsCollector async="false" continueOnError="false" enabled="true" name="Stats-1">

The following table describes attributes that are common to all policy parent elements:

Attribute Description Default Presence
name

The internal name of the policy. The value of the name attribute can contain letters, numbers, spaces, hyphens, underscores, and periods. This value cannot exceed 255 characters.

Optionally, use the <DisplayName> element to label the policy in the management UI proxy editor with a different, natural-language name.

N/A Required
continueOnError

Set to false to return an error when a policy fails. This is expected behavior for most policies.

Set to true to have flow execution continue even after a policy fails.

false Optional
enabled

Set to true to enforce the policy.

Set to false to turn off the policy. The policy will not be enforced even if it remains attached to a flow.

true Optional
async

This attribute is deprecated.

false Deprecated

<DisplayName> element

Use in addition to the name attribute to label the policy in the management UI proxy editor with a different, natural-language name.

<DisplayName>Policy Display Name</DisplayName>
Default

N/A

If you omit this element, the value of the policy's name attribute is used.

Presence Optional
Type String

<Statistics>/<Statistic> 元素

<Statistics>
    <Statistic name="statName" ref="varName" type="refDataType">defaultStatValue</Statistic>
</Statistics>
属性 说明 默认值 状态
name

用于引用为指定变量收集的数据的名称。查看分析数据时,请使用此名称来引用为 ref 属性指定的变量收集的数据。

如果请求或响应中未定义 ref 指定的变量,则 defaultStatValue 指定为该变量收集的值。如果您省略默认值,则当变量未定义时,系统不会为该变量收集任何数据。

命名限制

以下命名限制适用于自定义分析变量:

不适用 必填
ref

要为其收集统计信息的流变量。此变量可以是 Apigee 预定义的流变量,也可以是您在 API 代理中定义的自定义变量。

ref 属性通常引用由“提取变量”政策定义的自定义变量。如需了解详情,请参阅提取变量政策

不适用 必填
类型

指定由 ref 属性指定的变量的数据类型。有效值为:字符串/整数/浮点数/长整数/双精度浮点数/布尔值。

对于字符串类型的数据,在自定义报告中以维度的形式引用统计数据。对于数值数据类型(整数/浮点型/长整型/双精度型),在自定义报告中以维度指标的形式引用统计数据。如需了解详情,请参阅管理自定义报告

只有当 ref 引用预定义的 Apigee 流变量或在 ExtractVariables 政策的 XML 载荷中声明了类型时,才能省略 type 的值。

字符串 可选

错误参考信息

This section describes the error messages and flow variables that are set when this policy triggers an error. This information is important to know if you are developing fault rules for a proxy. To learn more, see What you need to know about policy errors and Handling faults.

Runtime errors

None.

Deployment errors

Error name Cause Fix
UnsupportedDatatype If the type of the variable specified by the ref attribute in the <Statistic> element of the Statistics Collector policy is unsupported, then the deployment of the API proxy fails. The supported data types are string, integer, float, long, double, and boolean.
InvalidName If the name used to reference the data collected for the specified variable defined within the <Statistic> element of the Statistics Collector policy conflicts with a system-defined variable, then the deployment of the API proxy fails. Some of the known system-defined variables are organization and environment.
DatatypeMissing If the type of the variable specified by the ref attribute in the <Statistic> element of the Statistics Collector policy is missing, then the deployment of the API proxy fails.

Fault variables

None.

架构

每种政策类型均由 XML 架构 (.xsd) 定义。GitHub 提供了政策架构作为参考。

相关主题

有关详情,请参阅: