본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Web 메트릭

원문 보기 위키 갱신

외부 서비스에 HTTP 요청을 보내 측정값을 얻을 수 있어요.

출처: 문서

본문

외부 서비스에 HTTP 요청을 보내 측정값을 얻을 수 있어요. 이 예시는 어떤 URL에 HTTP GET 요청을 보내요. 웹훅 응답은 JSON 내용을 반환해야 해요. 선택 사항인 jsonPath 표현식의 결과는 result 변수에 할당되고, 이 변수는 successCondition과 failureCondition 표현식에서 참조할 수 있어요. 생략하면 응답 본문 전체가 result 변수로 사용돼요.

metrics:
  - name: webmetric
    successCondition: result == true
    provider:
      web:
        url: "http://my-server.com/api/v1/measurement?service={{ args.service-name }}"
        timeoutSeconds: 20 # defaults to 10 seconds
        headers:
          - key: Authorization
            value: "Bearer {{ args.api-token }}"
        jsonPath: "{$.data.ok}"

다음 예시에서 페이로드가 주어졌을 때, data.ok 필드가 true이고 data.successPercent가 0.90보다 크면 측정은 성공이 돼요.

{
  "data": {
    "ok": true,
    "successPercent": 0.95
  }
}
metrics:
  - name: webmetric
    successCondition: "result.ok && result.successPercent >= 0.90"
    provider:
      web:
        url: "http://my-server.com/api/v1/measurement?service={{ args.service-name }}"
        headers:
          - key: Authorization
            value: "Bearer {{ args.api-token }}"
        jsonPath: "{$.data}"

참고: 결과가 문자열이라면 asInt와 asFloat라는 두 편의 함수를 제공해요. 이 함수들은 결과 값을 숫자 타입으로 변환해서 수학적 비교 연산자(예: >, <, >=, <=)를 사용할 수 있게 해줘요.

Optional web methods

method와 body 또는 jsonBody 필드를 지정하면 POST나 PUT 요청을 사용할 수 있어요.

metrics:
  - name: webmetric
    successCondition: result == true
    provider:
      web:
        method: POST # valid values are GET|POST|PUT, defaults to GET
        url: "http://my-server.com/api/v1/measurement?service={{ args.service-name }}"
        timeoutSeconds: 20 # defaults to 10 seconds
        headers:
          - key: Authorization
            value: "Bearer {{ args.api-token }}"
          - key: Content-Type # if body is a json, it is recommended to set the Content-Type
            value: "application/json"
        body: "string value"
        jsonPath: "{$.data.ok}"

!!! tip JSON으로 보내려면 jsonBody를 사용하면 되고 Content-Type이 자동으로 json으로 설정돼요. GET 요청에 body나 jsonBody 필드를 설정하면 오류가 발생해요. body와 jsonBody 중 하나만 설정해야 하며, 둘 다 설정하면 오류가 발생해요.

metrics:
  - name: webmetric
    successCondition: result == true
    provider:
      web:
        method: POST # valid values are GET|POST|PUT, defaults to GET
        url: "http://my-server.com/api/v1/measurement?service={{ args.service-name }}"
        timeoutSeconds: 20 # defaults to 10 seconds
        headers:
          - key: Authorization
            value: "Bearer {{ args.api-token }}"
          - key: Content-Type # if body is a json, it is recommended to set the Content-Type
            value: "application/json"
        jsonBody: # If using jsonBody Content-Type header will be automatically set to json
          key1: value_1
          key2:
            nestedObj: nested value
            key3: "{{ args.service-name }}"
        jsonPath: "{$.data.ok}"

Skip TLS verification

insecure: true 옵션을 설정하면 웹 호스트의 TLS 인증을 건너뛸 수 있어요.

metrics:
  - name: webmetric
    successCondition: "result.ok && result.successPercent >= 0.90"
    provider:
      web:
        url: "https://my-server.com/api/v1/measurement?service={{ args.service-name }}"
        insecure: true
        headers:
          - key: Authorization
            value: "Bearer {{ args.api-token }}"
        jsonPath: "{$.data}"

Authorization

OAuth2 사용하기

다음 값들을 사용해서 OAuth2 클라이언트 자격 증명 흐름을 설정할 수 있어요:

apiVersion: argoproj.io/v1alpha1
kind: AnalysisTemplate
metadata:
  name: success-rate
spec:
  args:
  - name: service-name
  # from secret
  - name: oauthSecret  # This is the OAuth2 shared secret
    valueFrom:
      secretKeyRef:
        name: oauth-secret
        key: secret
  metrics:
  - name: webmetric
    successCondition: result == true
    provider:
      web:
        url: "http://my-server.com/api/v1/measurement?service={{ args.service-name }}"
        timeoutSeconds: 20 # defaults to 10 seconds
        authentication:
          oauth2:
            tokenUrl: https://my-oauth2-provider/token
            clientId: my-client-id
            clientSecret: "{{ args.oauthSecret }}"
            scopes: [
              "my-oauth2-scope"
            ]
        headers:
          - key: Content-Type # if body is a json, it is recommended to set the Content-Type
            value: "application/json"
        jsonPath: "{$.data.ok}"

이 경우 Authentication 헤더를 따로 제공할 필요가 없어요. AnalysisRun은 먼저 그 정보로 액세스 토큰을 얻고, 메트릭 공급자 호출에 Authorization: *** 헤더로 제공해요.

더 알아보기 (Learn more)