@@ -309,6 +309,9 @@ func customizeOCREventNames(metrics []MetricDefinition) []MetricDefinition {
309309// example formula: "( 1000000000 * (a / b) / (c / (d * socket_count) ) ) * DURATIONTIMEINSECONDS"
310310// desired output: "( 1000000000 * ([event1] / [event2]) / ([constant1] / ([constant2] * socket_count) ) ) * 1"
311311func getExpression (perfmonMetric PerfmonMetric ) (string , error ) {
312+ if perfmonMetric .Formula == "" {
313+ return "" , fmt .Errorf ("metric '%s' has no formula defined" , perfmonMetric .MetricName )
314+ }
312315 expression := perfmonMetric .Formula
313316 replacers := make (map [string ]string )
314317 for _ , event := range perfmonMetric .Events {
@@ -329,17 +332,17 @@ func getExpression(perfmonMetric PerfmonMetric) (string, error) {
329332 for commonEvent , alias := range commonEventReplacements {
330333 expression = strings .ReplaceAll (expression , commonEvent , alias )
331334 }
332- // replace fixed counter perfmon event names with their corresponding perf event names
333- // example: "100 * ([ref-cycles:k] / [TSC])"
334- // parse out the list of events/variables from the expression
335- expressionVars := regexp . MustCompile ( `\[[^\]]+\]` )
336- // for each event/variable, check if it is in the fixedCounterEventNameTranslation map
337- for _ , match := range expressionVars .FindAllString (expression , - 1 ) {
338- for perfmonEventName , perfEventName := range fixedCounterEventNameTranslation {
339- if match == "[" + perfmonEventName + "]" {
340- expression = util . ReplaceWholeWord ( expression , perfmonEventName , perfEventName )
341- break
342- }
335+ // replace fixed counter perfmon event names with their corresponding perf
336+ // event names found in the fixedCounterEventNameTranslation map
337+ // example: "100 * ([CPU_CLK_UNHALTED.REF_TSC:k] / [TSC])"
338+ // becomes "100 * ([ref-cycles:k] / [TSC])"
339+ expressionVarPattern := regexp . MustCompile ( `\[[^\]]+\]` )
340+ for _ , match := range expressionVarPattern .FindAllString (expression , - 1 ) {
341+ // strip the brackets
342+ match = strings . Trim ( match , "[]" )
343+ // check if the match is in the translation map
344+ if perfEventName , ok := fixedCounterEventNameTranslation [ match ]; ok {
345+ expression = strings . ReplaceAll ( expression , match , perfEventName )
343346 }
344347 }
345348 return expression , nil
0 commit comments