다음을 통해 공유


series_rate_fl()

이 함수 series_rate_fl() 는 초당 메트릭 증가의 평균 속도를 계산하는 UDF(사용자 정의 함수)입니다. 해당 논리는 PromQL rate() 함수를 따릅니다. Prometheus 모니터링 시스템에서 클러스터에 수집하고 series_metric_fl()에서 검색하는 시계열 카운터 메트릭에 사용해야 합니다.

구문

T | invoke series_rate_fl([ n_bins [, fix_reset ]])

T 는 series_metric_fl()에서 반환된 테이블입니다. 해당 스키마에는 .가 포함됩니다 (timestamp:dynamic, name:string, labels:string, value:dynamic).

구문 규칙에 대해 자세히 알아봅니다.

매개 변수

이름 Type 필수 설명
n_bins int 속도 계산을 위해 추출된 메트릭 값 사이의 간격을 지정할 bin 수입니다. 함수는 현재 샘플과 이전 n_bins 간의 차이를 계산하고 해당 타임스탬프의 차이(초)로 나눕니다. 기본값은 하나의 bin입니다. 기본 설정은 PromQL 인스턴스 속도 함수인 irate()를 계산합니다.
fix_reset bool 카운터 재설정을 확인하고 PromQL rate() 함수처럼 수정할지 여부를 제어합니다. 기본값은 true입니다. 재설정을 false 확인할 필요가 없는 경우 중복 분석을 저장하도록 설정합니다.

함수 정의

다음과 같이 해당 코드를 쿼리 정의 함수로 포함하거나 데이터베이스에 저장된 함수로 만들어 함수를 정의할 수 있습니다.

다음 let 문을 사용하여 함수를 정의합니다. 사용 권한이 필요 없습니다.

Important

let 문자체적으로 실행할 수 없습니다. 그 뒤에 테이블 형식 식 문이 있어야 합니다. 작업 예제 series_rate_fl()를 실행하려면 예제를 참조 하세요.

let series_rate_fl=(tbl:(timestamp:dynamic, value:dynamic), n_bins:int=1, fix_reset:bool=true)
{
    tbl
    | where fix_reset                                                   //  Prometheus counters can only go up
    | mv-apply value to typeof(double) on   
    ( extend correction = iff(value < prev(value), prev(value), 0.0)    // if the value decreases we assume it was reset to 0, so add last value
    | extend cum_correction = row_cumsum(correction)
    | extend corrected_value = value + cum_correction
    | summarize value = make_list(corrected_value))
    | union (tbl | where not(fix_reset))
    | extend timestampS = array_shift_right(timestamp, n_bins), valueS = array_shift_right(value, n_bins)
    | extend dt = series_subtract(timestamp, timestampS)
    | extend dt = series_divide(dt, 1e7)                              //  converts from ticks to seconds
    | extend dv = series_subtract(value, valueS)
    | extend rate = series_divide(dv, dt)
    | project-away dt, dv, timestampS, value, valueS
};
// Write your query to use the function here.

예제

다음 예제에서는 호출 연산자를 사용하여 함수를 실행합니다.

메트릭 증가의 평균 비율 계산

쿼리 정의 함수를 사용하려면 포함된 함수 정의 후에 호출합니다.

let series_rate_fl=(tbl:(timestamp:dynamic, value:dynamic), n_bins:int=1, fix_reset:bool=true)
{
    tbl
    | where fix_reset                                                   //  Prometheus counters can only go up
    | mv-apply value to typeof(double) on   
    ( extend correction = iff(value < prev(value), prev(value), 0.0)    // if the value decreases we assume it was reset to 0, so add last value
    | extend cum_correction = row_cumsum(correction)
    | extend corrected_value = value + cum_correction
    | summarize value = make_list(corrected_value))
    | union (tbl | where not(fix_reset))
    | extend timestampS = array_shift_right(timestamp, n_bins), valueS = array_shift_right(value, n_bins)
    | extend dt = series_subtract(timestamp, timestampS)
    | extend dt = series_divide(dt, 1e7)                              //  converts from ticks to seconds
    | extend dv = series_subtract(value, valueS)
    | extend rate = series_divide(dv, dt)
    | project-away dt, dv, timestampS, value, valueS
};
//
demo_prometheus
| invoke series_metric_fl('TimeStamp', 'Name', 'Labels', 'Val', 'writes', offset=now()-datetime(2020-12-08 00:00))
| invoke series_rate_fl(2)
| render timechart with(series=labels)

출력

모든 디스크에 대한 디스크 쓰기 메트릭의 초당 속도를 보여 주는 그래프입니다.

두 호스트의 주 디스크를 선택합니다.

다음 예제에서는 두 호스트의 주 디스크를 선택하고 함수가 이미 설치되어 있다고 가정합니다. 이 예제에서는 입력 테이블을 첫 번째 매개 변수로 지정하는 대체 직접 호출 구문을 사용합니다.

쿼리 정의 함수를 사용하려면 포함된 함수 정의 후에 호출합니다.

let series_rate_fl=(tbl:(timestamp:dynamic, value:dynamic), n_bins:int=1, fix_reset:bool=true)
{
    tbl
    | where fix_reset                                                   //  Prometheus counters can only go up
    | mv-apply value to typeof(double) on   
    ( extend correction = iff(value < prev(value), prev(value), 0.0)    // if the value decreases we assume it was reset to 0, so add last value
    | extend cum_correction = row_cumsum(correction)
    | extend corrected_value = value + cum_correction
    | summarize value = make_list(corrected_value))
    | union (tbl | where not(fix_reset))
    | extend timestampS = array_shift_right(timestamp, n_bins), valueS = array_shift_right(value, n_bins)
    | extend dt = series_subtract(timestamp, timestampS)
    | extend dt = series_divide(dt, 1e7)                              //  converts from ticks to seconds
    | extend dv = series_subtract(value, valueS)
    | extend rate = series_divide(dv, dt)
    | project-away dt, dv, timestampS, value, valueS
};
//
series_rate_fl(series_metric_fl(demo_prometheus, 'TimeStamp', 'Name', 'Labels', 'Val', 'writes', '"disk":"sda1"', lookback=2h, offset=now()-datetime(2020-12-08 00:00)), n_bins=10)
| render timechart with(series=labels)

출력

지난 2시간 동안 10개의 bin 간격으로 주 디스크 쓰기 메트릭의 초당 속도를 보여 주는 그래프입니다.