Marian Nociar
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    <style> b { color: #94e5ff } g { color: #777777 } </style> # Watcher alerts [< Back](../../home.md) ## Query DSL :information_source: official documentation: [Elasticsearch Guide](https://www.elastic.co/guide/en/elasticsearch/reference/7.16/index.html) **D**omain **S**pecific **L**anguage based on JSON to define queries, consisting of two types of clauses: - **Leaf query clauses** - look for particular value in a particular field (<b>match</b>, <b>term</b>, <b>range</b>, ...) - **Compound query clauses** - wrap other leaf or compound queries to combine multiple queries ([bool](#bool-query), <b>dis_max</b>) ### Most useful Leaf query clauses :information_source: You can experiment or validate your own queries on [Elastic Console](https://154b1b4905dc4c8eb06aa13fb7f2329e.eu-central-1.aws.cloud.es.io:9243/app/dev_tools#/console) - **term** - Returns documents that contain an exact term in a provided field. Suitable for precise values such as a price, IDs, usernames... :heavy_exclamation_mark: Avoid using a term query for <b>text</b> fields. ``` GET _search { "query": { "term": {"integrationId": 16091} } } ``` - **match** - Returns documents that match a provided text, number, date or boolean value. ``` GET integrations-services-prod-2022.02.17/_search { "query": { "match": {"message": "webhook"} } } ``` :information_source: returns all records where the message will contain the word *webhook*, (e.g. "undefining webhook", "webhook signature mismatch!", ...) - **range** - Returns documents that contain terms within a provided range. \- Optional parameters: **gt**, **gte**, **lt**, **lte**, ``` GET integrations-services-prod-2022.02.17/_search { "query": { "range": { "@timestamp": { "gte": "now-2h", "lt": "now-1h" } } } } ``` - **query string** - Returns documents based on a provided query string, using a parser with a strict syntax. This query uses a syntax to parse and split the provided query string based on operators, such as AND or NOT. **Field names**: - **<g>label:error</g>** - lable field contains error - **<g>message:error AND NOT httpBase</g>** - message field contains error but not httpBase - **<g>response.\\*:(InvalidValue OR RecordInvalid)</g>** - any of the fields response.data, response.result, response.code, ... contains InvalidValue or RecordInvalid **Wildcards**: - **<g>?</g>** - to replace single character - **<g>*</g>** - to replace zero or more characters ``` GET integrations-services-prod-2022.02.17/_search { "query": { "query_string": { "query": "response.\\*:(*Value AND Record*)" } } } ``` ## Compound queries ### bool query A query that matches documents matching boolean combinations of other queries. It is built using one or more boolean clauses, each clause with a typed occurrence. The occurrence types are: - **must** - query must appear in matching document and contribute to the score (not relevant for us). It behave like logical AND operation. e.g. Find all errors, where message contains "createOrUpdateContact" and integration type is "hubspot" *KQL*: `message: "createOrUpdateContact" and integrationType: "hubspot"` ``` GET integrations-services-prod-2022.02.17/_search { "query": { "bool": { "must": [ { "match": { "message": "createOrUpdateContact" }}, { "match": { "integrationType": "hubspot" }} ] } } } ``` - **filter** - Similar to <u>must</u>. Query must appear in matching document, but will not contribute to the score. e.g. Previous example using a filter. ``` GET integrations-services-prod-2022.02.17/_search { "query": { "bool": { "filter": [ { "match": { "message": "createOrUpdateContact" } }, { "match": { "integrationType": "hubspot" } } ] } } } ``` - **should** - Query should appear in the matching document. It behaveves like logical OR operation. ``` GET integrations-services-prod-2022.02.17/_search { "query": { "bool": { "should": [ { "match": { "integrationType": "gont" } }, { "match": { "integrationType": "tray" } } ], "minimum_should_match": 1 } } } ``` - **must_not** - Query must not appear in the matching documents. *KQL*: `message: "createOrUpdateContact" and integrationType: "hubspot" and not @timestamp <= "now-10m"` ``` GET integrations-services-prod-2022.02.17/_search { "query": { "bool": { "must": [ { "match": { "message": "createOrUpdateContact" }}, {"match": { "integrationType": "hubspot" }} ], "must_not": [ {"range": { "@timestamp": { "lte": "now-10m" } }} ] } } } ``` # Watcher You can use Watcher to watch for changes or anomalies in your data and perform the necessary actions in response. :information_source: You find Watcher at this [link](https://154b1b4905dc4c8eb06aa13fb7f2329e.eu-central-1.aws.cloud.es.io:9243/app/management/insightsAndAlerting/watcher/watches), or alternatively go to: **elastic (Kibana)** -> Side Navigation -> **Management** -> Stack Management -> **Alerts and Insights** -> Watcher :information_source: Type <u>integrations</u> to search bar to display only the list of integration watchers ## What are the goals - have an overview of the state of integrations - know about the problem in advance (before the customer reports it) ## What to avoid - we don't want to receive :100: notifications in :one: hour about the call export failed ## How it works A watch is constructed from four simple building blocks: - **Schedule** - A schedule for running a query and checking the condition. - **Query** - The query to run as input to the condition. - **Condition** - A condition that determines whether or not to execute the actions. Simple condition or scripting are available. - **Actions** - One or more actions, such as sending email, pushing data to 3rd party systems. In our case, it's sending notifications to slack channel. ## Createing new advanced watch Define name in the format "TBD" The basic structure will be automatically generated. ``` { "trigger": { "schedule": { "interval": "30m" } }, "input": { "search": { "request": { "body": { "size": 0, "query": { "match_all": {} } }, "indices": [ "*" ] } } }, "condition": { "compare": { "ctx.payload.hits.total": { "gte": 10 } } }, "actions": { "my-logging-action": { "logging": { "text": "There are {{ctx.payload.hits.total}} documents in your index. Threshold is 10." } } } } ``` ### Trigger Controls how often a watch is triggered. There are several alternatives. We can use hour schedule, daily, weekly, monthly, cron schedule or interval schedule. More info [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.16/trigger-schedule.html). In example above is watcher periodically triggered every 30 minutes. ### Input Load data into the watch payload, based on a defined query. There are four input types. We will be mainly interested in: - **<g>search</g>**: load the results of a search into the execution context (showen in basic structure above). - **<g>chain</g>**: use a series of inputs to load data into the execution context. ``` "input": { "chain": { "inputs": [ { "first": { "search": { "request": { "indices": [ "integrations*prod*" ], "rest_total_hits_as_int": true, "body": { "query": { "bool": { "must": [ { "query_string": { "query": """message: "Export failed" AND model: "Cdr" """ } }, { "range": { "@timestamp": { "gte": "now-1h" } } } ] } }, "_source": [ "message" ] } } } } }, { "second": { "search": { "request": { "indices": [ "integrations*prod*" ], "rest_total_hits_as_int": true, "body": { "query": { "bool": { "must": [ { "query_string": { "query": """message: "Export failed" AND model: "Cdr" """ } }, { "range": { "@timestamp": { "gte": "now-2h", "lt": "now-1h" } } } ] } }, "_source": [ "message" ] } } } } } ] } } ``` - **first**, **last** are the names we will refer to in the condition - **rest_total_hits_as_int** if true, response return hits.total as integer. If false, it return hits.total as object. Default is false. Check response difference in [Elastic Console](https://154b1b4905dc4c8eb06aa13fb7f2329e.eu-central-1.aws.cloud.es.io:9243/app/dev_tools#/console) - **indices** search for query in the *integrations\*prod\** index and loads the response into the watch payload. - **body** contains the query definition we discussed at the beginning [Query DSL]( #query-dsl) - **_source** need to check difference between with and without ### Conditions - **compare**: perform simple comparisons against values in the watch payload ``` "condition": { "compare": { "ctx.payload.first.hits.total": { "gte": "{{ctx.payload.second.hits.total}}" } } } ``` - **script**: use a script to determine whether or not to execute the watch actions. ``` "condition": { "script": { "source": "return ((ctx.payload.first.hits.total + 0.01) / ctx.payload.second.hits. total) > 1.10", "lang": "painless" } } ``` Default script language is [*painless*](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-painless.html). Painless has a simple syntax. Support arithmetic, relational, logical operations like in common languages. ### Action We are using webhook action to send notifications to slack chanels. **prod-allie-test** chanel is used for testing purposes. - `webhook.path: "services/TBMUGUCDP/B02RVK737SQ/UbqvtNLhf1Ti6lL7IMK9m19i"` **dev-reporting-integrations** chanel for production integrations reports - `webhook.path: "services/TBMUGUCDP/B02RVK737SQ/LQj5FHw1ZLTfhxBg5HJmYSBh"` ``` "actions": { "my_webhook": { "webhook": { "scheme": "https", "host": "hooks.slack.com", "port": 443, "method": "post", "path": "services/TBMUGUCDP/B02RVK737SQ/UbqvtNLhf1Ti6lL7IMK9m19i", "params": {}, "headers": { "Content-Type": "application/json" }, "body": """{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "*Integrations LUKAS TEST ALERT compare last two hours* - {{ctx.payload.first.hits.total}} > {{ctx.payload.second.hits.total}} " }, "accessory": { "type": "button", "text": { "type": "plain_text", "text": "Error Logs", "emoji": true }, "value": "error_logs", "url": "https://154b1b4905dc4c8eb06aa13fb7f2329e.eu-central-1.aws.cloud.es.io:9243/app/kibana#/discover/dec1d130-7919-11ec-8a40-d1559359c704?_g=(refreshInterval%3A(pause%3A!t%2Cvalue%3A0)%2Ctime%3A(from%3Anow-1h%2Cto%3Anow))", "action_id": "button-action" } } ] }""" } } } ``` ### Simulation After setting up the watcher, we can simulate it. - Click on the pencil icon :pencil2: and then select <u>Simulate tab</u>. - Select checkbox <u>*Ignore condition*</u> - Change Action mode for a slack webhook to <u>*force_execute*</u> - Fire the <u>*Simulate watch*</u> button ##### TODO - ? aggregations and array_compare

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully