4
0

prometheus.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package executor
  2. import (
  3. "github.com/prometheus/client_golang/prometheus"
  4. "github.com/prometheus/client_golang/prometheus/promauto"
  5. )
  6. const (
  7. executionResultSuccess = "success"
  8. executionResultFailed = "failed"
  9. executionResultBlocked = "blocked"
  10. executionResultTimeout = "timeout"
  11. executionResultError = "error"
  12. )
  13. var (
  14. metricActionsRequested = promauto.NewCounter(prometheus.CounterOpts{
  15. Name: "olivetin_actions_requested_count",
  16. Help: "The actions requested count",
  17. })
  18. metricActionExecutionsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
  19. Name: "olivetin_action_executions_total",
  20. Help: "Total number of finished action executions grouped by result.",
  21. }, []string{"result"})
  22. metricActionExecutionDuration = promauto.NewHistogram(prometheus.HistogramOpts{
  23. Name: "olivetin_action_execution_duration_seconds",
  24. Help: "Action execution duration in seconds from start to finish.",
  25. Buckets: []float64{0.1, 0.5, 1, 2, 5, 10, 30, 60, 120, 300, 600},
  26. })
  27. executionResultLabels = []string{
  28. executionResultSuccess,
  29. executionResultFailed,
  30. executionResultBlocked,
  31. executionResultTimeout,
  32. executionResultError,
  33. }
  34. )
  35. func init() {
  36. for _, result := range executionResultLabels {
  37. metricActionExecutionsTotal.WithLabelValues(result)
  38. }
  39. }
  40. func executionResultLabel(entry *InternalLogEntry) string {
  41. if entry.Blocked {
  42. return executionResultBlocked
  43. }
  44. return finishedExecutionResultLabel(entry)
  45. }
  46. func finishedExecutionResultLabel(entry *InternalLogEntry) string {
  47. if entry.TimedOut {
  48. return executionResultTimeout
  49. }
  50. switch {
  51. case entry.ExitCode == 0:
  52. return executionResultSuccess
  53. case isPreExecutionError(entry):
  54. return executionResultError
  55. default:
  56. return executionResultFailed
  57. }
  58. }
  59. func isPreExecutionError(entry *InternalLogEntry) bool {
  60. return entry.ExitCode == DefaultExitCodeNotExecuted || !entry.ExecutionStarted
  61. }
  62. func recordExecutionMetrics(entry *InternalLogEntry) {
  63. if entry == nil || entry.Queued {
  64. return
  65. }
  66. metricActionExecutionsTotal.WithLabelValues(executionResultLabel(entry)).Inc()
  67. recordExecutionDuration(entry)
  68. }
  69. func recordExecutionDuration(entry *InternalLogEntry) {
  70. if entry.DatetimeFinished.IsZero() || entry.DatetimeStarted.IsZero() {
  71. return
  72. }
  73. duration := entry.DatetimeFinished.Sub(entry.DatetimeStarted).Seconds()
  74. if duration < 0 {
  75. return
  76. }
  77. metricActionExecutionDuration.Observe(duration)
  78. }