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

[Alerting] Improved type check for action variables #86506

Closed
sorenlouv opened this issue Dec 18, 2020 · 6 comments
Closed

[Alerting] Improved type check for action variables #86506

sorenlouv opened this issue Dec 18, 2020 · 6 comments
Labels
Feature:Alerting/RuleActions Issues related to the Actions attached to Rules on the Alerting Framework Feature:Alerting Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) technical debt Improvement of the software architecture and operational architecture

Comments

@sorenlouv
Copy link
Member

sorenlouv commented Dec 18, 2020

Type: enhancement
Priority: Low


Currently it's easy to mess up the action variables by forgetting to either

  1. declare action variables in actionVariables.context (1)
  2. specify the value for declared action variables alertInstance.scheduleActions (2)

✅ Correct

The following declares action variables and specifies their values correctly:

export const apmActionVariables = {
  serviceName: {
    description: "The service the alert is created for",
    name: "serviceName",
  },
  transactionType: {
    description: "The transaction type the alert is created for",
    name: "transactionType",
  },
  environment: {
    description: "The transaction type the alert is created for",
    name: "environment",
  },
};

alerts.registerType({
  actionVariables: {
    context: [
      // Declare action variables (1)
      apmActionVariables.serviceName,
      apmActionVariables.transactionType,
    ],
  },
  // ...
  executor: async ({ services }) => {
    const alertInstance = services.alertInstanceFactory("alertInstanceName");
    alertInstance.scheduleActions("defaultActionGroupId", {
      // Set action variable values (2)
      serviceName: 'my service name',
      transactionType: 'my transaction type'
    });
  },
});

❌ Incorrect

In the following the action variable declarations don't match the values specified in scheduleActions:

alerts.registerType({
  actionVariables: {
    context: [
      // Declare action variables (1)
      apmActionVariables.serviceName,
      apmActionVariables.environment,
    ],
  },
  // ...
  executor: async ({ services }) => {
    const alertInstance = services.alertInstanceFactory("alertInstanceName");
    alertInstance.scheduleActions("defaultActionGroupId", {
      // Set action variable values (2)
      serviceName: 'my service name',
      transactionType: 'my transaction type' // this is not declared above. Should not be allowed. 
      // environment is declared above but the value it missing here
    });
  },
});

Question

Would it be possible to narrow the types for alertInstance.scheduleActions so that action variables declared in (1) will be required?

@sorenlouv sorenlouv added the Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) label Dec 18, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-alerting-services (Team:Alerting Services)

@gmmorris
Copy link
Contributor

gmmorris commented Dec 22, 2020

specify the value for declared action variables alertInstance.scheduleActions

I'm already working on this as part of: #83501
It still isn't clear how easy it will be to enforce the typing properly due to several layers of indirection (Recovery groups are dynamic evaluated at registration which makes this even harder), but I'm hoping to figure it out. :)

@YulNaumenko YulNaumenko added the technical debt Improvement of the software architecture and operational architecture label Mar 11, 2021
@gmmorris gmmorris added the Feature:Alerting/RuleActions Issues related to the Actions attached to Rules on the Alerting Framework label Jul 1, 2021
@gmmorris
Copy link
Contributor

gmmorris commented Jul 1, 2021

@sqren - mind checking if the above issue addressed this to your satisfaction? :)
If there are still edge cases that weren't covered by #83501 we can take another stab at it.

@sorenlouv
Copy link
Member Author

Hey @gmmorris

What I was hoping was that based on actionVariables.context:

actionVariables: {
context: [
apmActionVariables.serviceName,
apmActionVariables.transactionType,
apmActionVariables.environment,
apmActionVariables.threshold,
apmActionVariables.triggerValue,
apmActionVariables.interval,
],
},

I'd get suggestions for the context in scheduleActions:

.scheduleActions(alertTypeConfig.defaultActionGroupId, {
transactionType: alertParams.transactionType,
serviceName: alertParams.serviceName,
environment: getEnvironmentLabel(alertParams.environment),
threshold,
triggerValue: transactionDurationFormatted,
interval: `${alertParams.windowSize}${alertParams.windowUnit}`,
});

But even if I delete everything in scheduleActions I don't get any compile errors:
image

@gmmorris
Copy link
Contributor

gmmorris commented Jul 15, 2021

@sqren I think this is because you're using the Rule Registry and it seems to be overwriting the generics.
At framework level we let you specify the type of the context, but from a quick look, I think the RuleRegistry has hard coded all Context types as Record<string, unknown>.

export type LifecycleAlertService<TAlertInstanceContext extends Record<string, unknown>> = (alert: {

From a framework perspective, what you're asking for should already work.
I suggest chatting with @dgieselaar about it (though I know he isn't working on the RuleRegistry anymore but he'll likely know the context of this decision)

@sorenlouv
Copy link
Member Author

Okay, that makes sense. Thanks for the explanation. Closing.

@kobelb kobelb added the needs-team Issues missing a team label label Jan 31, 2022
@botelastic botelastic bot removed the needs-team Issues missing a team label label Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Alerting/RuleActions Issues related to the Actions attached to Rules on the Alerting Framework Feature:Alerting Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) technical debt Improvement of the software architecture and operational architecture
Projects
None yet
Development

No branches or pull requests

6 participants