• SkyWalking内置MQE语法


    此文档出自SkyWalking官方git https://github.com/apache/skywalking
    docs/en/api/metrics-query-expression.md

    Metrics Query Expression(MQE) Syntax

    MQE is a string that consists of one or more expressions. Each expression could be a combination of one or more operations.
    The expression allows users to do simple query-stage calculation through V3 APIs.

    Expression =  Expression1  Expression2  Expression3 ...
    
    • 1

    The following document lists the operations supported by MQE.

    Metrics Expression

    Metrics Expression will return a collection of time-series values.

    Common Value Metrics

    Expression:

    
    
    • 1

    For example:
    If we want to query the service_sla metric, we can use the following expression:

    service_sla
    
    • 1
    Result Type

    The ExpressionResultType of the expression is TIME_SERIES_VALUES.

    Labeled Value Metrics

    For now, we only have a single anonymous label with multi label values in a labeled metric.
    To be able to use it in expressions, define _ as the anonymous label name (key).

    Expression:

    {_=',...'}
    
    • 1

    {_=',...'} is the selected label value of the metric. If is not specified, all label values of the metric will be selected.

    For example:
    If we want to query the service_percentile metric with the label values 0,1,2,3,4, we can use the following expression:

    service_percentile{_='0,1,2,3,4'}
    
    • 1

    If we want to rename the label values to P50,P75,P90,P95,P99, see Relabel Operation.

    Result Type

    The ExpressionResultType of the expression is TIME_SERIES_VALUES and with labels.

    Binary Operation

    The Binary Operation is an operation that takes two expressions and performs a calculation on their results.
    The following table lists the binary operations supported by MQE.

    Expression:

    Expression1  Expression2
    
    • 1
    OperatorDefinition
    +addition
    -subtraction
    *multiplication
    /division
    %modulo

    For example:
    If we want to transform the service_sla metric value to percent, we can use the following expression:

    service_sla / 100
    
    • 1

    Result Type

    For the result type of the expression, please refer to the following table.

    Binary Operation Rules

    The following table lists if the different result types of the input expressions could do this operation and the result type after the operation.
    The expression could be on the left or right side of the operator.
    Note: If the expressions on both sides of the operator are the TIME_SERIES_VALUES with labels, they should have the same labels for calculation.

    ExpressionExpressionYes/NoExpressionResultType
    SINGLE_VALUESINGLE_VALUEYesSINGLE_VALUE
    SINGLE_VALUETIME_SERIES_VALUESYesTIME_SERIES_VALUES
    SINGLE_VALUESORTED_LIST/RECORD_LISTYesSORTED_LIST/RECORD_LIST
    TIME_SERIES_VALUESTIME_SERIES_VALUESYesTIME_SERIES_VALUES
    TIME_SERIES_VALUESSORTED_LIST/RECORD_LISTno
    SORTED_LIST/RECORD_LISTSORTED_LIST/RECORD_LISTno

    Compare Operation

    Compare Operation takes two expressions and compares their results.
    The following table lists the compare operations supported by MQE.

    Expression:

    Expression1  Expression2
    
    • 1
    OperatorDefinition
    >greater than
    >=greater than or equal
    <less than
    <=less than or equal
    ==equal
    !=not equal

    The result of the compare operation is an int value:

    • 1: true
    • 0: false

    For example:
    Compare the service_resp_time metric value if greater than 3000, if the service_resp_time result is:

    {
      "data": {
        "execExpression": {
          "type": "TIME_SERIES_VALUES",
          "error": null,
          "results": [
            {
              "metric": {
                "labels": []
              },
              "values": [{"id": "1691658000000", "value": "2500", "traceID": null}, {"id": "1691661600000", "value": 3500, "traceID": null}]
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    we can use the following expression:

    service_resp_time > 3000
    
    • 1

    and get result:

    {
      "data": {
        "execExpression": {
          "type": "TIME_SERIES_VALUES",
          "error": null,
          "results": [
            {
              "metric": {
                "labels": []
              },
              "values": [{"id": "1691658000000", "value": "0", "traceID": null}, {"id": "1691661600000", "value": 1, "traceID": null}]
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Compare Operation Rules and Result Type

    Same as the Binary Operation Rules.

    Aggregation Operation

    Aggregation Operation takes an expression and performs aggregate calculations on its results.

    Expression:

    (Expression)
    
    • 1
    OperatorDefinitionExpressionResultType
    avgaverage the resultSINGLE_VALUE
    countcount number of the resultSINGLE_VALUE
    latestselect the latest non-null value from the resultSINGLE_VALUE
    sumsum the resultSINGLE_VALUE
    maxselect maximum from the resultSINGLE_VALUE
    minselect minimum from the resultSINGLE_VALUE

    For example:
    If we want to query the average value of the service_cpm metric, we can use the following expression:

    avg(service_cpm)
    
    • 1

    Result Type

    The different operators could impact the ExpressionResultType, please refer to the above table.

    Mathematical Operation

    Mathematical Operation takes an expression and performs mathematical calculations on its results.

    Expression:

    (Expression, parameters)
    
    • 1
    OperatorDefinitionparametersExpressionResultType
    absreturns the absolute value of the resultfollow the input expression
    ceilreturns the smallest integer value that is greater or equal to the resultfollow the input expression
    floorreturns the largest integer value that is greater or equal to the resultfollow the input expression
    roundreturns result round to specific decimal placesplaces: a positive integer specific decimal places of the resultfollow the input expression

    For example:
    If we want to query the average value of the service_cpm metric in seconds,
    and round the result to 2 decimal places, we can use the following expression:

    round(service_cpm / 60 , 2)
    
    • 1

    Result Type

    The different operators could impact the ExpressionResultType, please refer to the above table.

    TopN Operation

    TopN Operation takes an expression and performs TopN calculation on its results.

    Expression:

    top_n(, , )
    
    • 1

    top_number is the number of the top results, should be a positive integer.

    order is the order of the top results. The value of order can be asc or des.

    For example:
    If we want to query the top 10 services with the highest service_cpm metric value, we can use the following expression:

    top_n(service_instance_cpm, 10, des)
    
    • 1

    Result Type

    According to the type of the metric, the ExpressionResultType of the expression will be SORTED_LIST or RECORD_LIST.

    Relabel Operation

    Relabel Operation takes an expression and replaces the label values with new label values on its results.

    Expression:

    relabel(Expression, _=',...')
    
    • 1

    _ is the new label of the metric after the label is relabeled, the order of the new label values should be the same as the order of the label values in the input expression result.

    For example:
    If we want to query the service_percentile metric with the label values 0,1,2,3,4, and rename the label values to P50,P75,P90,P95,P99, we can use the following expression:

    relabel(service_percentile{_='0,1,2,3,4'}, _='P50,P75,P90,P95,P99')
    
    • 1

    Result Type

    Follow the input expression.

    AggregateLabels Operation

    AggregateLabels Operation takes an expression and performs an aggregate calculation on its Labeled Value Metrics results. It aggregates a group of TIME_SERIES_VALUES into a single TIME_SERIES_VALUES.

    Expression:

    aggregate_labels(Expression, parameter)
    
    • 1
    parameterDefinitionExpressionResultType
    avgcalculate avg value of a Labeled Value MetricsTIME_SERIES_VALUES
    sumcalculate sum value of a Labeled Value MetricsTIME_SERIES_VALUES
    maxselect the maximum value from a Labeled Value MetricsTIME_SERIES_VALUES
    minselect the minimum value from a Labeled Value MetricsTIME_SERIES_VALUES

    For example:
    If we want to query all Redis command total rates, we can use the following expression(total_commands_rate is a metric which recorded every command rate in labeled value):

    aggregate_labels(total_commands_rate, SUM)
    
    • 1

    Result Type

    The ExpressionResultType of the aggregateLabels operation is TIME_SERIES_VALUES.

    Logical Operation

    ViewAsSequence Operation

    ViewAsSequence operation represents the first not-null metric from the listing metrics in the given prioritized sequence(left to right). It could also be considered as a short-circuit of given metrics for the first value existing metric.

    Expression:

    view_as_seq([, , ...])
    
    • 1

    For example:
    if the first expression value is empty but the second one is not empty, it would return the result from the second expression.
    The following example would return the content of the service_cpm metric.

    view_as_seq(not_existing, service_cpm)
    
    • 1
    Result Type

    The result type is determined by the type of selected not-null metric expression.

    Expression Query Example

    Labeled Value Metrics

    service_percentile{_='0,1'}
    
    • 1

    The example result is:

    {
      "data": {
        "execExpression": {
          "type": "TIME_SERIES_VALUES",
          "error": null,
          "results": [
            {
              "metric": {
                "labels": [{"key": "_", "value": "0"}]
              },
              "values": [{"id": "1691658000000", "value": "1000", "traceID": null}, {"id": "1691661600000", "value": 2000, "traceID": null}]
            },
            {
              "metric": {
                "labels": [{"key": "_", "value": "1"}]
              },
              "values": [{"id": "1691658000000", "value": "2000", "traceID": null}, {"id": "1691661600000", "value": 3000, "traceID": null}]
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    If we want to transform the percentile value unit from ms to s the expression is:

    service_percentile{_='0,1'} / 1000
    
    • 1
    {
      "data": {
        "execExpression": {
          "type": "TIME_SERIES_VALUES",
          "error": null,
          "results": [
            {
              "metric": {
                "labels": [{"key": "_", "value": "0"}]
              },
              "values": [{"id": "1691658000000", "value": "1", "traceID": null}, {"id": "1691661600000", "value": 2, "traceID": null}]
            },
            {
              "metric": {
                "labels": [{"key": "_", "value": "1"}]
              },
              "values": [{"id": "1691658000000", "value": "2", "traceID": null}, {"id": "1691661600000", "value": 3, "traceID": null}]
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    Get the average value of each percentile, the expression is:

    avg(service_percentile{_='0,1'})
    
    • 1
    {
      "data": {
        "execExpression": {
          "type": "SINGLE_VALUE",
          "error": null,
          "results": [
            {
              "metric": {
                "labels": [{"key": "_", "value": "0"}]
              },
              "values": [{"id": null, "value": "1500", "traceID": null}]
            },
            {
              "metric": {
                "labels": [{"key": "_", "value": "1"}]
              },
              "values": [{"id": null, "value": "2500", "traceID": null}]
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    Calculate the difference between the percentile and the average value, the expression is:

    service_percentile{_='0,1'} - avg(service_percentile{_='0,1'})
    
    • 1
    {
      "data": {
        "execExpression": {
          "type": "TIME_SERIES_VALUES",
          "error": null,
          "results": [
            {
              "metric": {
                "labels": [{"key": "_", "value": "0"}]
              },
              "values": [{"id": "1691658000000", "value": "-500", "traceID": null}, {"id": "1691661600000", "value": 500, "traceID": null}]
            },
            {
              "metric": {
                "labels": [{"key": "_", "value": "1"}]
              },
              "values": [{"id": "1691658000000", "value": "-500", "traceID": null}, {"id": "1691661600000", "value": 500, "traceID": null}]
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    Calculate the difference between the service_resp_time and the service_percentile, if the service_resp_time result is:

    {
      "data": {
        "execExpression": {
          "type": "TIME_SERIES_VALUES",
          "error": null,
          "results": [
            {
              "metric": {
                "labels": []
              },
              "values": [{"id": "1691658000000", "value": "2500", "traceID": null}, {"id": "1691661600000", "value": 3500, "traceID": null}]
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    The expression is:

    service_resp_time - service_percentile{_='0,1'}
    
    • 1
    {
      "data": {
        "execExpression": {
          "type": "TIME_SERIES_VALUES",
          "error": null,
          "results": [
            {
              "metric": {
                "labels": [{"key": "_", "value": "0"}]
              },
              "values": [{"id": "1691658000000", "value": "1500", "traceID": null}, {"id": "1691661600000", "value": "1500", "traceID": null}]
            },
            {
              "metric": {
                "labels": [{"key": "_", "value": "1"}]
              },
              "values": [{"id": "1691658000000", "value": "500", "traceID": null}, {"id": "1691661600000", "value": "500", "traceID": null}]
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    (mac)Prometheus监控之Node_exporter(CPU、内存、磁盘、网络等)
    springboot
    极速Go语言入门(超全超详细)-基础篇
    Unity3D TCP网络通讯核心意涵与基本原理详解
    类加载及执行子系统的案例
    shell之函数和数组(案例分析)
    Java版 招投标系统简介 招投标系统源码 java招投标系统 招投标系统功能设计
    Android深色主题背景的实现及主题背景颜色互换
    中国与外国互免签证协定一览表(更新至2022年7月8日)
    【Unity实战】从零手戳一个库存背包系统
  • 原文地址:https://blog.csdn.net/weixin_44728369/article/details/133035770