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

Feature Request: Access to {{ .Duration }} in alert message template #1605

Closed
thuck opened this issue Oct 8, 2017 · 8 comments
Closed

Feature Request: Access to {{ .Duration }} in alert message template #1605

thuck opened this issue Oct 8, 2017 · 8 comments

Comments

@thuck
Copy link
Contributor

thuck commented Oct 8, 2017

InfluxDB v1.1.1 (git: unknown unknown)
Kapacitor 1.3.2 (git: HEAD 2b6a049e5b9dc7d7e8d58a8bbde8d186dc8543e3)
Telegraf v1.3.1 (git: release-1.3 f93615672b02a41d9bc867bd92bf31c1d777989b)

Maybe I'm missing something, but If I try to use {{ .Duration }} as part of the alarm message I have an error:

ID: host_down
Error: alert5: template: message:1:95: executing "message" at <.Duration>: can't evaluate field Duration in type kapacitor.messageInfo
Template: 
Type: stream
Status: enabled
Executing: false
Created: 08 Oct 17 14:06 UTC
Modified: 08 Oct 17 19:02 UTC
LastEnabled: 08 Oct 17 19:02 UTC
Databases Retention Policies: ["telegraf"."autogen"]
TICKscript:
var data = stream
    |from()
        .database('telegraf')
        .retentionPolicy('autogen')
        .measurement('system')
        .groupBy('host')

data
    |deadman(1.0, 60s)
        .id('{{ index .Tags "host"}}/state')
        .message('{{ index .Tags "host" }} is {{ if eq .Level "OK" }} UP  {{ else }} DOWN  {{ end }} duration {{ .Duration }}')
        .details('')
        .stateChangesOnly(1m)
        .post('http://192.168.1.4:5000')
        .slack()
        .log('/tmp/cpu_alert_log.txt')

DOT:
digraph host_down {
stream0 -> from1;
from1 -> noop3;
stats2 -> derivative4;
derivative4 -> alert5;
}

Taking a look on the source code of the alert.go; I cannot find duration as part of the renderMessageAndDetails
Am I missing something or this is not implemented? Also if implemented how could I change this from nanoseconds to hours or minutes.

@desa
Copy link
Contributor

desa commented Oct 11, 2017

@thuck What is Duration?

@thuck
Copy link
Contributor Author

thuck commented Oct 11, 2017

@desa for the AlertNode there is some predefined fields:

Each event that gets sent to a handler contains the following alert data:

    ID – the ID of the alert, user defined.
    Message – the alert message, user defined.
    Details – the alert details, user defined HTML content.
    Time – the time the alert occurred.
    Duration – the duration of the alert in nanoseconds.
    Level – one of OK, INFO, WARNING or CRITICAL.
    Data – influxql.Result containing the data that triggered the alert.

This is extract from here: https://docs.influxdata.com/kapacitor/v1.3//nodes/alert_node/

If you check the json generated by the alert there is the field for duration there:

{"id":"XXXX/state","message":"YYYYYY XXXX is  DOWN  ","details":"","time":"2017-10-10T21:28:00Z","duration":600000000000,"level":"CRITICAL","data":{"series":[{"name":"stats","tags":{"env":"YYYY","host":"XXXX","objectname":"System","project":"XXXX","role":"XXXXX"},"columns":["time","emitted"],"values":[["2017-10-10T21:28:00Z",0]]}]}}

So, as far as I can understand, the Duration here should be the duration that we have on the json, that's is related to the time that the alert is in that state; but if you try to use {{ .Duration }} as you would use {{ .Level }} it do not find it.

So this works:

.message('{{ .ID }} is {{ .Level }} value:{{ index .Fields "value" }}')

But this do not:

.message('{{ .ID }} is {{ .Level }} value:{{ index .Fields "value" }} time on state {{ .Duration }}')

@desa
Copy link
Contributor

desa commented Oct 11, 2017

From the documentation it looks like the following are available in the message template

http://docs.influxdata.com/kapacitor/v1.3/nodes/alert_node/#message

Template for constructing a meaningful message for the alert. Available template data:
ID – The ID of the alert.
Name – Measurement name.
TaskName – The name of the task
Group – Concatenation of all group-by tags of the form [key=value,]+. If no groupBy is performed equal to literal 'nil'.
Tags – Map of tags. Use '{{ index .Tags "key" }}' to get a specific tag value.
Level – Alert Level, one of: INFO, WARNING, CRITICAL.
Fields – Map of fields. Use '{{ index .Fields "key" }}' to get a specific field value.
Time – The time of the point that triggered the event.

@desa
Copy link
Contributor

desa commented Oct 11, 2017

Hmm weird, it should actually be available

https://github.com/influxdata/kapacitor/blob/master/alert/types.go#L100

// TemplateData is a structure containing all information available to use in templates for an Event.
type TemplateData struct {
	// The ID of the alert.
	ID string

	// The Message of the Alert
	Message string

	// Alert Level, one of: INFO, WARNING, CRITICAL.
	Level string

	// Time the event occurred.
	Time time.Time

	// Duration of the event
	Duration time.Duration

	// Measurement name
	Name string

	// Task name
	TaskName string

	// Concatenation of all group-by tags of the form [key=value,]+.
	// If not groupBy is performed equal to literal 'nil'
	Group string

	// Map of tags
	Tags map[string]string

	// Fields of alerting data point.
	Fields map[string]interface{}
}

@desa
Copy link
Contributor

desa commented Oct 11, 2017

Wait, actually I'm wrong

https://github.com/influxdata/kapacitor/blob/master/alert.go#L1035-L1049

type messageInfo struct {
	idInfo

	// The ID of the alert.
	ID string

	// Fields of alerting data point.
	Fields map[string]interface{}

	// Alert Level, one of: INFO, WARNING, CRITICAL.
	Level string

	// Time
	Time time.Time
}

I'm going to change this to a Feature Request.

@desa desa changed the title Issue accessing {{ .Duration }} on alert message Feature Request: Access to {{ .Duration }} in alert message template Oct 11, 2017
@thuck
Copy link
Contributor Author

thuck commented Oct 11, 2017

Great, thanks

@desa
Copy link
Contributor

desa commented Oct 11, 2017

@thuck if you're interested, it'd be a pretty easy contribution. Essentially it'd be

Step 1

https://github.com/influxdata/kapacitor/blob/master/alert.go#L1035-L1049
Add Duration to the messageInfo struct

Step 2

https://github.com/influxdata/kapacitor/blob/master/alert.go#L698
Modify renderMessageAndDetails to take in a duration and set it on the messageInfo

Step 3

Write tests to verify the behavior.

@thuck
Copy link
Contributor Author

thuck commented Oct 11, 2017

@desa yep, I'll give a try on this, looks simple to implement

nathanielc added a commit that referenced this issue Oct 27, 2017
Includes the .Duration for the Message and Details on alert #1605
@thuck thuck closed this as completed Oct 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants