您目前查看的是 Apigee Edge 說明文件。
前往 Apigee X 說明文件。 info
結果
可收集訊息中資料的統計資料,例如產品 ID、價格、REST 動作、用戶端和目標網址,以及訊息長度。資料可來自 Apigee 預先定義的流程變數,或您定義的自訂變數。
統計資料會傳送至分析伺服器,由該伺服器分析統計資料並產生報表。您可以透過 Edge 管理 UI 或 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.id 和 product.price。每次提出要求時,統計資料收集器政策都會將這兩個變數的值寫入 Analytics 伺服器。
這個範例也會為每個變數設定選用的預設值,其中 product.id 的預設值為 999999,而 product.price 的預設值為 0。如果變數未定義,且您未指定預設值,系統就不會記錄該變數的資料。如果您指定預設值,系統會在變數未定義時記錄該值。
收集統計資料後,您可以使用 Edge 管理 UI 或 API 擷取統計資料。擷取統計資料時,您會分別參照為這些變數收集的資料,例如 productID 和 price。
存取統計資料
在本範例中,您會使用 Edge 管理 API 查看名為 productID 的集合統計資料。這項要求會根據每天為每個產品 ID 提交的訊息數量總和,為產品 ID 建立自訂報表。將變數 {org_name} 替換成貴機構名稱,並將 Apigee Edge 帳戶的電子郵件地址和密碼替換成 email:password。
設定 timeRange 參數,納入收集資料的時間間隔。根據預設,系統不會提供超過目前日期六個月前的資料。如要存取六個月前的資料,請與 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
在回應中,name 欄位包含產品 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>
「統計資料收集器」政策規定,收集到的資料必須儲存在變數中。 這些變數可以是 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 酬載中擷取資料,請參閱「使用自訂分析功能分析 API 訊息內容」。
關於統計資料收集器政策
執行時,統計資料收集器政策會記錄一或多個變數的目前值。這些變數可以是 Apigee 預先定義的流程變數,也可以是您定義的自訂變數。這項資料會寫入 Edge Analytics 伺服器。
如要存取政策收集的統計資料,請使用 Edge API 或 Edge 管理 UI。舉例來說,您可以使用 Edge 管理使用者介面建立自訂報表,以各種格式顯示收集到的資料。
如需使用 Statistics Collector 政策的完整教學課程,請參閱「使用自訂分析功能分析 API 訊息內容」。
政策的放置位置
您可以在 API Proxy 的要求或回應流程中加入 Statistics Collector 政策。不過,如果您在 Proxy 中放置多項 Statistics Collector 政策,則最後執行的政策會決定寫入 Analytics 伺服器的資料,先前任何 Statistics Collector 政策寫入的資料都會遺失。
在 API Proxy 中有多項 Statistics Collector 政策的其中一種情況,是您在要求或回應流程中有一項政策,而在錯誤處理常式中則有另一項政策。如果 API Proxy 發生錯誤,錯誤處理常式中的統計資料收集器會判斷收集的資料。您可以使用該統計資料收集器記錄故障相關資訊,或記錄您認為必要的任何其他資訊。無論要求/回應中的 Statistics Collector 是否已執行,錯誤處理常式中的 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">
下表說明所有政策父項元素的共同屬性:
| 屬性 | 說明 | 預設 | 存在必要性 |
|---|---|---|---|
name |
政策的內部名稱。 視需要使用 |
不適用 | 必填 |
continueOnError |
如果設為「 如果設為 |
false | 選用 |
enabled |
如要強制執行政策,請設為 設為 |
true | 選用 |
async |
此屬性已淘汰。 |
false | 已淘汰 |
<DisplayName>元素
除 name 屬性外,一併使用
管理 UI Proxy 編輯器,使用不同的自然語言名稱。
<DisplayName>Policy Display Name</DisplayName>
| 預設 |
不適用 如果省略這個元素,政策的 |
|---|---|
| 存在必要性 | 選用 |
| 類型 | 字串 |
<Statistics>/<Statistic> 元素
<Statistics> <Statistic name="statName" ref="varName" type="refDataType">defaultStatValue</Statistic> </Statistics>
| 屬性 | 說明 | 預設 | 存在必要性 |
|---|---|---|---|
| 名稱 |
用來參照指定變數所收集資料的名稱。查看 Analytics 資料時,請使用這個名稱參照透過 如果要求或回應中未定義 命名限制 自訂 Analytics 變數的命名限制如下:
|
N/A | 必填 |
| ref |
您要收集統計資料的流程變數。這個變數可以是 Apigee 預先定義的流程變數,也可以是您在 API Proxy 中定義的自訂變數。 ref 屬性通常會參照 Extract Variables 政策定義的自訂變數。詳情請參閱「擷取變數政策」。 |
N/A | 必填 |
| 類型 |
指定 ref 屬性所指定變數的資料類型。有效值包括: 字串/整數/浮點數/長整數/雙精度浮點數/布林值。 如果是字串類型的資料,請在自訂報表中將統計資料參照為維度。如果是數值型資料類型 (整數/浮點數/長整數/雙精度浮點數),請在自訂報表中將統計資料參照為「維度」或「指標」。詳情請參閱「管理自訂報表」。 只有在 ref 參照預先定義的 Apigee 流程變數,或類型是在「擷取變數」政策的 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. |
build |
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. |
build |
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. |
build |
Fault variables
None.
結構定義
每個政策類型都由 XML 架構 (.xsd) 定義。如需參考,請前往 GitHub 查看政策架構。
相關主題
如需詳細資訊,請參閱: