如何使用基本 Javascript(不是 NodeJS)中的 App Insights TrackEvent 将自定义复杂的非字符串属性发送到 Telemetry 到 Azure 门户?
我通过以下设置片段初始化了 Application Insights JavaScript SDK:
<script type='text/javascript'>
var appInsights=window.appInsights||function(config)
{
function r(config){ t[config] = function(){ var i = arguments; t.queue.push(function(){ t[config].apply(t, i)})} }
var t = { config:config},u=document,e=window,o='script',s=u.createElement(o),i,f;for(s.src=config.url||'//az416426.vo.msecnd.net/scripts/a/ai.0.js',u.getElementsByTagName(o)[0].parentNode.appendChild(s),t.cookie=u.cookie,t.queue=[],i=['Event','Exception','Metric','PageView','Trace','Ajax'];i.length;)r('track'+i.pop());return r('setAuthenticatedUserContext'),r('clearAuthenticatedUserContext'),config.disableExceptionTracking||(i='onerror',r('_'+i),f=e[i],e[i]=function(config, r, u, e, o) { var s = f && f(config, r, u, e, o); return s !== !0 && t['_' + i](config, r, u, e, o),s}),t
}({
instrumentationKey: "@Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey"
});
window.appInsights=appInsights;
appInsights.trackPageView();
</script>
我从这里https://github.com/microsoft/ApplicationInsights-JS和这里尝试了示例https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics #properties但没有成功:
appInsights.trackEvent({
name: 'EventName',
properties: { // accepts any type
prop1: 'string',
prop2: 123.45,
prop3: {
nested: 'objects are okay too'
}
}
});
在发送到 Azure 的 Ajax 跟踪请求中,发送的有效负载具有以下形式:
ver: 2
name:
name: 'EventName'
properties:
prop1: 'string'
prop2: 123.45
prop3:
nested: 'objects are okay too'
在 Azure Portal App Insights 中,我得到了这个:
CUSTOM EVENT
Event name [object Object]
我还在控制台中收到 Javascript 警告:
Logging.ts:206 AI: CannotSerializeObjectNonSerializable
message:"Attempting to serialize an object which does not implement ISerializable"
props:"{name:name}"
我只能通过name
单独指定属性并且仅指定字符串类型的属性和仅一级嵌套属性来发送。
成功的测试:
appInsights.trackEvent("EventName1", { properties: 'something' });
appInsights.trackEvent("EventName2", { prop1: 'something', prop2: 'prop2' });
不成功的测试:
appInsights.trackEvent("EventName3", { prop1: 'prop1', nestedProp2: {prop2: 'prop2'} });
appInsights.trackEvent('EventName4', { properties: { dataToSend: 'something' }, measurements: {prop1: 'prop1', prop2: 'prop2'}});