runwhen-contrib / rw-public-codecollection

RunWhen Public Codecollection Repository - Open Source troubleshooting runbook library for Kubernetes and cloud infrastructure components.
Apache License 2.0
39 stars 5 forks source link

Libraries/RW/Prometheus - Non-Raw transforms only apply to result.values #138

Closed Hashfyre closed 5 months ago

Hashfyre commented 5 months ago

Objective: Run RW.Prometheus based codebundle against a Prometheus endpoint with default transform Codebundle / Robot Script: Infracloudio/ifc-rw-codecollection/.../runbook.robot

Test Procedure:

and, in the corresponding sli.robot, if we do not input TRANSFORM as an USER VARIABLE:

Querying Prometheus Instance And Pushing Aggregated Data
    Log      ${ENV_QUERY}
    ${rsp}=    RW.Prometheus.Query Instant
    ...    api_url=${ENV_PROMETHEUS_HOST}
    ...    query=${ENV_QUERY}
    ...    step=${STEP}
    ...    target_service=${CURL_SERVICE}
    ${data}=    Set Variable    ${rsp["data"]}
    ${metric}=    RW.Prometheus.Transform Data
    ...    data=${data}
    ...    *** method=${TRANSFORM} *** <------ commented out TRANSFORM
    ...    no_result_overwrite=${NO_RESULT_OVERWRITE}
    ...    no_result_value=${NO_RESULT_VALUE}
    RW.Core.Push Metric    ${metric}

then RW.Prometheus.Transform Data call defaults to transform: Last, as seen here:

    RW.Core.Import User Variable    TRANSFORM
    ...    type=string
    ...    enum=[Raw,Max,Average,Minimum,Sum,First,Last]
    ...    description=What transform method to apply to the column data. First and Last are position relative, so Last is the most recent value. Use Raw to skip transform. 
    ...    default=Last
    ...    example=Last

While this can be controlled by the developer of sli.robot, the current logic creates an edge case where all transforms that only shuld apply to result.values:[][], can also be mistakenly called for result.value:[], resulting in the following error:

14:17:58.613    TRACE   Arguments: [ data={'resultType': 'vector',
 'result': [{'metric': {'__name__': 'aws_rds_database_connections_average',
                        'account_id': '590183940259',
                        'container': 'yet-another-cloudwatch-exporter',
                        'dimension_DBInstanceIdentifier': 'robotshopmysql',
                        'endpoint': 'http',
                        'instance': '192.168.164.136:5000',
                        'job': 'yace',
                        'name': 'arn:aws:rds:us-west-2:590183940259:db:robotshopmysql',
                        'namespace': 'monitoring',
                        'pod': 'yace-8b8bd5598-j2xps',
                        'region': 'us-west-2',
                        'service': 'yace'},
             'value': [1706172466.004, '30.2']}]} | method='Last' | no_result_overwrite=True | no_result_value=0.0 ]    
14:17:58.614    FAIL    TypeError: 'float' object is not subscriptable

This is because all transforms other than Raw can only apply to 2D matrices of [float][float], but here it gets applied to [float].

This PR intends to fix this issue, by only allowing non-Raw transforms on result.values[float][float]