azure 日志分析用户定义函数返回'可调用表达式的正文不能为空'

dwbf0jvd  于 2023-05-18  发布在  其他
关注(0)|答案(2)|浏览(156)

在Log Analytics中定义和执行保存的函数时,如下所示,我可以看到结果:

let cfd = () {
    let TimeSpanGranularity = 5m;
    let MinimumEventsInTimeSpan = 500;
    let ErrorRatioThreshold = 0.9;
    let TimeSpanTraces = allTraces
    | where timestamp > ago(TimeSpanGranularity)
    | parse message with * ") " EventText
    | parse EventText with EventName ":" *
    | extend Event=coalesce(EventName,EventText,message)
    | where severityLevel >= 1;
    let ShouldFireData = TimeSpanTraces
    | summarize ErrorOrAboveCount=todecimal(sumif(itemCount, severityLevel>=3)), TotalEventCount=todecimal(sum(itemCount))
    | extend ErrorRatioThreshold
    | extend ErrorRatio=100.0*ErrorOrAboveCount/TotalEventCount
    | project FireAlert = ErrorRatio>=ErrorRatioThreshold and TotalEventCount>=MinimumEventsInTimeSpan;
    let ShouldFire = toscalar(ShouldFireData);
    TimeSpanTraces
    | where ShouldFire == true and severityLevel >=3;
};
cfd();

但是,在保存函数并尝试调用保存的函数后,如下所示:

cfd()

我看到以下错误:
“可调用表达式的正文不能为空”
我花了太多的时间试图让它工作。你知道我做错了什么吗?

u4vypkhs

u4vypkhs1#

该函数似乎保存为:

let cfd = () {
    let TimeSpanGranularity = 5m;
    let MinimumEventsInTimeSpan = 500;
    let ErrorRatioThreshold = 0.9;
    let TimeSpanTraces = allTraces
    | where timestamp > ago(TimeSpanGranularity)
    | parse message with * ") " EventText
    | parse EventText with EventName ":" *
    | extend Event=coalesce(EventName,EventText,message)
    | where severityLevel >= 1;
    let ShouldFireData = TimeSpanTraces
    | summarize ErrorOrAboveCount=todecimal(sumif(itemCount, severityLevel>=3)), TotalEventCount=todecimal(sum(itemCount))
    | extend ErrorRatioThreshold
    | extend ErrorRatio=100.0*ErrorOrAboveCount/TotalEventCount
    | project FireAlert = ErrorRatio>=ErrorRatioThreshold and TotalEventCount>=MinimumEventsInTimeSpan;
    let ShouldFire = toscalar(ShouldFireData);
    TimeSpanTraces
    | where ShouldFire == true and severityLevel >=3;
};

而不是

let cfd = () {
    ...
};
cfd();

如果查询的这个特定部分而不是整个查询在UI中突出显示,则可能会发生这种情况。
你能再试一次吗?请注意,在这种情况下,保存的函数体可以简单地为:

let TimeSpanGranularity = 5m;
let MinimumEventsInTimeSpan = 500;
let ErrorRatioThreshold = 0.9;
let TimeSpanTraces = allTraces
| where timestamp > ago(TimeSpanGranularity)
| parse message with * ") " EventText
| parse EventText with EventName ":" *
| extend Event=coalesce(EventName,EventText,message)
| where severityLevel >= 1;
let ShouldFireData = TimeSpanTraces
| summarize ErrorOrAboveCount=todecimal(sumif(itemCount, severityLevel>=3)), TotalEventCount=todecimal(sum(itemCount))
| extend ErrorRatioThreshold
| extend ErrorRatio=100.0*ErrorOrAboveCount/TotalEventCount
| project FireAlert = ErrorRatio>=ErrorRatioThreshold and TotalEventCount>=MinimumEventsInTimeSpan;
let ShouldFire = toscalar(ShouldFireData);
TimeSpanTraces
| where ShouldFire == true and severityLevel >=3;
o4tp2gmn

o4tp2gmn2#

我也碰到了这个。另外,如果您想添加一个参数,您应该提供一个实际的输入。这对我来说是违反直觉的,因为函数定义通常不需要文字。

// Query that appends a question mark to a provided name parameter
let nameWithQuestionMark = (name:string) {
  print strcat(name, " is it you?")
};
let name = "John";
nameWithQuestionMark(name)

然后使用参数保存查询:

当你像这样调用一个工作簿后:

nameWithQuestionMark('databach');

相关问题