You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否有NewRelic API可直接获取SLIs/SLOs数据以检测错误预算违规?

解决方案:通过NewRelic获取SLO数值并判断违规

直接获取SLO状态与错误预算的NerdGraph查询

你可以扩展现有的NerdGraph查询,直接获取SLO的当前达成率、错误预算剩余量以及违规状态,不需要额外解析NRQL再查询。示例查询如下:

{
  actor {
    entity(guid: "{你的实体GUID}") {
      serviceLevel {
        indicators {
          name
          id
          currentSummary {
            overall {
              value # SLO达成率(如99.9%对应0.999)
              violation {
                isViolated # 是否触发违规
                remainingBudgetRatio # 剩余错误预算比例
                remainingBudgetMinutes # 剩余错误预算分钟数
              }
            }
            timeWindows {
              duration # 时间窗口,如"7_DAYS"、"30_DAYS"
              value
              violation {
                isViolated
              }
            }
          }
        }
      }
    }
  }
}

这个查询会返回核心数据:*isViolated*直接标识是否违规,*remainingBudgetRatio*和*remainingBudgetMinutes*可辅助判断错误预算消耗情况。

在Jenkins中集成的实现步骤

  • 调用NerdGraph API:在Jenkins流水线中用curl或HTTP Request插件发送查询,请求头带上NewRelic API密钥(格式为ApiKey {你的密钥})。示例curl命令:
curl -X POST \
  https://api.newrelic.com/graphql \
  -H 'Content-Type: application/json' \
  -H 'Api-Key: YOUR_NEWRELIC_API_KEY' \
  -d '{
    "query": "{ actor { entity(guid: \"YOUR_ENTITY_GUID\") { serviceLevel { indicators { name id currentSummary { overall { value violation { isViolated remainingBudgetRatio } } timeWindows { duration value violation { isViolated } } } } } } }"
  }'
  • 解析返回结果:用jq工具提取*isViolated*字段的值,示例:
# 假设响应保存到response.json
VIOLATED=$(jq -r '.data.actor.entity.serviceLevel.indicators[0].currentSummary.overall.violation.isViolated' response.json)
  • 中断CI流程:如果VIOLATED为true,直接终止流水线。示例Jenkins流水线代码片段:
stage('Check SLO Violation') {
  steps {
    script {
      def response = httpRequest(
        url: 'https://api.newrelic.com/graphql',
        httpMode: 'POST',
        headers: [
          ['name': 'Api-Key', 'value': env.NEWRELIC_API_KEY],
          ['name': 'Content-Type', 'value': 'application/json']
        ],
        requestBody: '''{
          "query": "{ actor { entity(guid: \\\"''' + env.SERVICE_ENTITY_GUID + '''\\\") { serviceLevel { indicators { currentSummary { overall { violation { isViolated } } } } } } }"
        }'''
      )
      def jsonResponse = new groovy.json.JsonSlurper().parseText(response.content)
      def isViolated = jsonResponse.data.actor.entity.serviceLevel.indicators[0].currentSummary.overall.violation.isViolated
      if (isViolated) {
        error "SLO已违规,终止CI流程"
      }
    }
  }
}

注意事项

  • 确保NewRelic API密钥拥有NrServiceLevelQuery权限,否则会返回权限错误
  • 若有多个SLI,需要遍历indicators数组,通过name或id定位到目标指标
  • 可以指定timeWindows.duration查询特定周期的SLO状态,比如检查最近7天的错误预算

内容的提问来源于stack exchange,提问作者anand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 03:26:19