Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Watcher] Add Index, HipChat, PagerDuty, and Jira Action types on the client #30043

Merged
merged 9 commits into from
Feb 11, 2019

Conversation

sebelga
Copy link
Contributor

@sebelga sebelga commented Feb 5, 2019

This PR adds the missing action types on the Watcher UI client. There recently was an issue (#29787) related to the Webhook action missing and it came to the light that there were other action types missing.

  • Index action
  • HipChat action
  • PagerDuty action
  • Jira action

How to test

  • Start by creating a watcher with a webhook action in the dev console
PUT _watcher/watch/my-watch
{
  "trigger" : {
    "schedule" : { "cron" : "0/5 * * * * ?" }
  },
  "input" : {
    "search" : {
      "request" : {
        "indices" : [
          "kibana_sample_data_flights"
        ],
        "body" : {
          "query" : {
            "bool" : {
              "must" : {
                "match": {
                   "Carrier": "Kibana Airlines"
                }
              }
            }
          }
        }
      }
    }
  },
  "condition" : {
    "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}
  },
  "actions" : {
    "my_webhook" : { 
      "webhook" : {
        "method" : "POST", 
        "host" : "localhost", 
        "port" : 3000, 
        "path": ":/{{ctx.watch_id}}", 
        "body" : "id={{ctx.watch_id}}, hits={{ctx.payload.hits.total}}" 
      }
    }
  }
}
  • Got to the Watcher app and click on "edit" on the row of the created watcher

What we want to test is making sure that the body of the HTTP request contains the corresonding values that we have defined in the JSON editor (and of course that the application does not crash) when saving the watcher.

  • In the Kibana JSON editor, at the bottom in the "actions" > "my_action" replace the "webhook" property with:

Hiphat action

"hipchat" : {
    "account" : "user-account",
    "message" : {
    "room" : [ "mission-control", "devops" ],
    "user" : "[email protected]",
    "body" : "Encountered  {{ctx.payload.hits.total}} errors in the last 5 minutes (facepalm)",
    "format" : "text",
    "color" : "red",
    "notify" : true
    }
}
  • Click the "save" button

You should get a toast error message, which is ok. The important part is that the body of the request that has been sent contains the action that we have just defined. It should be under watch.actions.my_actions of the PUT request body.

Repeat the same test with the other action types:

PagerDuty action

"pagerduty" : {
    "account" : "team1",
    "description" : "Main system down, please check! Happened at {{ctx.execution_time}}",
    "attach_payload" : true,
    "client" : "/foo/bar/{{ctx.watch_id}}",
    "client_url" : "http://www.example.org/",
    "contexts" : [
        {
            "type": "link",
            "href": "http://acme.pagerduty.com"
        },{
            "type": "link",
            "href": "http://acme.pagerduty.com",
            "text": "View the incident on {{ctx.payload.link}}"
        }
    ]
}

Jira action

"jira" : {
    "account" : "integration-account", 
    "fields" : {
        "project" : {
        "key": "PROJ" 
        },
        "issuetype" : {
        "name": "Bug" 
        },
        "summary" : "Encountered {{ctx.payload.hits.total}} errors in the last 5 minutes", 
        "description" : "Encountered {{ctx.payload.hits.total}} errors in the last 5 minutes (facepalm)", 
        "labels" : ["auto"], 
        "priority" : {
        "name" : "High" 
        }
    }
}

Index action

"index" : {
    "index" : "my-index", 
    "doc_type" : "my-type", 
    "doc_id": "my-id" 
}

@sebelga sebelga added v7.0.0 Feature:Watcher Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more v6.7.0 labels Feb 5, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/es-ui

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Copy link
Contributor

@cjcenizal cjcenizal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't had time to test locally yet, but the code LGTM! Had some suggestions which we can address separately.

super(props);

allFields.forEach((field) => {
this[field] = get(props, field);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see a potentially difficult problem to debug if we ever add support for a field which collides with one of the class properties (e.g. description). This is compounded by the presence of properties inherited from BaseAction. What do you think of assigning these fields to an instance property, to eliminate this possibility? For example:

this.fields = {};
allFields.forEach((field) => {
  this.fields[field] = get(props, field);
});

This change would have to be applied to all of the other actions too, and if any code is depending on these dynamic properties then this change could be significant; probably too significant for this PR. Let me know if you want me to move this suggestion into a separate issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll check for property collision.

});
}

get upstreamJson() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't look very closely, but this method looks duplicated throughout all of the actions. Would it make sense to extract it into a generalized service?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right this could be optimized but as Watcher will be rewritten I just wanted to prevent future bugs in the meantime because this file was missing.

@cjcenizal
Copy link
Contributor

If possible, it would be very helpful to also update the description with steps for testing, e.g. various requests to make in Dev Tools, and any settings we need to change for X-Pack.

@sebelga
Copy link
Contributor Author

sebelga commented Feb 6, 2019

@cjcenizal I just updated the comment to explain how to test. Can you have another look?

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Copy link
Contributor

@cjcenizal cjcenizal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the steps to test! This really helped me a lot. I was able to test locally and verify the behavior works as expected. This information also helped me learn more about Watcher in general and I was able to spot a few errors in the code.

return i18n.translate('xpack.watcher.models.jiraAction.description', {
defaultMessage: '{issueName} will be created in Jira',
values: {
issueName: this.fields && this.fields.issuetype && this.fields.issuetype.name || ''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the Jira action docs it looks like the expected shape is:

{
  "account" : "integration-account", 
  "fields" : {
    "issue" : {
      "issuetype" : {
        "name": "Bug" 
      }
    }
  }
}

So I think the correct path should be this.fields.fields.issue.issuetype.name, right?

If we're already using lodash's get, I think we might as well use that here too:

get(this.fields, 'fields.issue.issuetype.name', '');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

defaultMessage: 'The {index} will be indexed as {docType}',
values: {
index: this.index,
docType: this.doc_type,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to update the fields references here:

  • this.index -> this.fields.index
  • this.doc_type -> this.fields.doc_type

return i18n.translate('xpack.watcher.models.hipchatAction.description', {
defaultMessage: '{body} will be sent through Hipchat',
values: {
body: this.message && this.message.body || ''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to update references of this.message to this.fields.message.

return i18n.translate('xpack.watcher.models.pagerDutyAction.description', {
defaultMessage: '{description} will be sent to PagerDuty',
values: {
description: this.description,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.description -> this.fields.description

allFields.forEach((field) => {
this[field] = get(props, field);
this.fields[field] = get(props, field);
});

this.fullPath = this.url ? this.url : this.host + this.port + this.path;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const { url, host, port, path } = this.fields;
this.fullPath = url ? url : host + port + path;

@sebelga
Copy link
Contributor Author

sebelga commented Feb 8, 2019

@cjcenizal updated the PR with your changes. Can you have another look? cheers!

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@sebelga sebelga added the v8.0.0 label Feb 8, 2019
@sebelga sebelga added the v7.2.0 label Feb 8, 2019
Copy link
Contributor

@cjcenizal cjcenizal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, Seb!

@cjcenizal
Copy link
Contributor

This is a step towards addressing #18059.

@cjcenizal cjcenizal changed the title [Watcher] Add missing Action types on the client [Watcher] Add Index, HipChat, PagerDuty, and Jira Action types on the client Feb 8, 2019
@sebelga sebelga merged commit 6b376e5 into elastic:master Feb 11, 2019
@sebelga sebelga deleted the fix/watcher-missing-action-types branch February 11, 2019 08:39
sebelga added a commit to sebelga/kibana that referenced this pull request Feb 11, 2019
sebelga added a commit to sebelga/kibana that referenced this pull request Feb 20, 2019
sebelga added a commit to sebelga/kibana that referenced this pull request Feb 20, 2019
@immon
Copy link

immon commented Feb 22, 2019

Since it has been backported to 6.6 and 6.7 branches can we have version labels added to the issue? Which 6.6 version is going to include the fix?

@cjcenizal
Copy link
Contributor

Thanks for the reminder @immon. This will ship in 6.6.2 and 6.7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Watcher Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more v6.6.2 v6.7.0 v7.0.1 v7.2.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants