본문 바로가기
공돌이 작업실/하루 1커밋

ES + open distro 에서 Alert 하기. (feat.Mustache templates)

by 미젤 2020. 3. 13.

일단 첨에 부딪힌 부분>

 

trigger condition 에서 

ctx.results[0].hits.total.value > 0

이렇게 쓰길래 똑같이 아래 Action 에서 웹훅 보내려고 message 부분에도 Mustache templates 으로다가

{{ctx.results[0].hits.total.value }} 이렇게 썼더니 왜인지 안되서 당황했더니

{{ctx.results.0.hits.total.value }} 이렇게 접근해야 했다.. 아니.. 그럼.. 좀...

 

일단 요 삽질하는데 시간을 좀 썼고,

 

 

그 다음은 엘라스틱에서 aggregation을 자주 쓰다보니 buckets 가 많아지고, 버킷 안에 데이터 갯수가 유동적이게 되는데 전부다 접근하려면 어떻게 해야하는가에 크게 막혀 있었다.

Mustache templates 부분을 아무리 봐도 사실 잘 모르겠어서 그쪽말고 계속 DSL Query를 손대고 있었는데,,,

 

1 ) #과 / 로 for문 처럼 구현이 가능..


{{#ctx.results.0.hits.hits}}
    {{_source.codeData}} 
{{/ctx.results.0.hits.hits}}

 

hits 가 3개라면, 결과는

codeData1

codeData2

codeData3

 

이렇게 나오게 된다.

 

2) 원하는 데이터에 접근하려면 위치를 정확하게 써야지만 되는줄 알았는데 아녔다는것
 {{ctx.results.0.hit.buckets.0._source.codeData}}  

 {{ctx.results.0.hit.buckets.1._source.codeData}}  

 {{ctx.results.0.hit.buckets.2._source.codeData}} 

이런식으로 순차적으로 버켓에 접근해줘야 하는줄만 알았는데

앞부분 생략해서 {{_source.codeData}} 로 바로 접근가능할줄이야...

 

내가 센스가 부족한건지 문서에 적혀있었는데 놓친건지...ㅜ.ㅜ

 

 

참조한 것

https://discuss.elastic.co/t/watcher-webhook-action-iterate-through-aggregated-results-and-show-it-in-json-format/74597/6

댓글