|
| 1 | +defmodule Sentry.CheckIn do |
| 2 | + @moduledoc """ |
| 3 | + This module represents the struct for a "check-in". |
| 4 | +
|
| 5 | + Check-ins are used to report the status of a monitor to Sentry. This is used |
| 6 | + to track the health and progress of **cron jobs**. This module is somewhat |
| 7 | + low level, and mostly useful when you want to report the status of a cron |
| 8 | + but you are not using any common library to manage your cron jobs. |
| 9 | +
|
| 10 | + > #### Using `capture_check_in/1` {: .tip} |
| 11 | + > |
| 12 | + > Instead of using this module directly, you'll probably want to use |
| 13 | + > `Sentry.capture_check_in/1` to manually report the status of your cron jobs. |
| 14 | +
|
| 15 | + See <https://develop.sentry.dev/sdk/check-ins/>. This struct is available |
| 16 | + since v10.2.0. |
| 17 | + """ |
| 18 | + @moduledoc since: "10.2.0" |
| 19 | + |
| 20 | + alias Sentry.{Config, Interfaces, UUID} |
| 21 | + |
| 22 | + @typedoc """ |
| 23 | + The possible status of the check-in. |
| 24 | + """ |
| 25 | + @type status() :: :in_progress | :ok | :error |
| 26 | + |
| 27 | + @typedoc """ |
| 28 | + The possible values for the `:schedule` option under `:monitor_config`. |
| 29 | +
|
| 30 | + If the `:type` is `:crontab`, then the `:value` must be a string representing |
| 31 | + a crontab expression. If the `:type` is `:interval`, then the `:value` must be |
| 32 | + a number representing the interval and the `:unit` must be present and be one of `:year`, |
| 33 | + `:month`, `:week`, `:day`, `:hour`, or `:minute`. |
| 34 | + """ |
| 35 | + @type monitor_config_schedule() :: |
| 36 | + %{type: :crontab, value: String.t()} |
| 37 | + | %{ |
| 38 | + type: :interval, |
| 39 | + value: number(), |
| 40 | + unit: :year | :month | :week | :day | :hour | :minute |
| 41 | + } |
| 42 | + |
| 43 | + @typedoc """ |
| 44 | + The type for the check-in struct. |
| 45 | + """ |
| 46 | + @type t() :: %__MODULE__{ |
| 47 | + check_in_id: String.t(), |
| 48 | + monitor_slug: String.t(), |
| 49 | + status: status(), |
| 50 | + duration: float() | nil, |
| 51 | + release: String.t() | nil, |
| 52 | + environment: String.t() | nil, |
| 53 | + monitor_config: |
| 54 | + nil |
| 55 | + | %{ |
| 56 | + required(:schedule) => monitor_config_schedule(), |
| 57 | + optional(:checkin_margin) => number(), |
| 58 | + optional(:max_runtime) => number(), |
| 59 | + optional(:failure_issue_threshold) => number(), |
| 60 | + optional(:recovery_threshold) => number(), |
| 61 | + optional(:timezone) => String.t() |
| 62 | + }, |
| 63 | + contexts: Interfaces.context() |
| 64 | + } |
| 65 | + |
| 66 | + @enforce_keys [ |
| 67 | + :check_in_id, |
| 68 | + :monitor_slug, |
| 69 | + :status |
| 70 | + ] |
| 71 | + defstruct @enforce_keys ++ |
| 72 | + [ |
| 73 | + :duration, |
| 74 | + :release, |
| 75 | + :environment, |
| 76 | + :monitor_config, |
| 77 | + :contexts |
| 78 | + ] |
| 79 | + |
| 80 | + number_schema_opts = [type: {:or, [:integer, :float]}, type_doc: "`t:number/0`"] |
| 81 | + |
| 82 | + crontab_schedule_opts_schema = [ |
| 83 | + type: [type: {:in, [:crontab]}, required: true], |
| 84 | + value: [type: :string, required: true] |
| 85 | + ] |
| 86 | + |
| 87 | + interval_schedule_opts_schema = [ |
| 88 | + type: [type: {:in, [:interval]}, required: true], |
| 89 | + value: number_schema_opts, |
| 90 | + unit: [type: {:in, [:year, :month, :week, :day, :hour, :minute]}, required: true] |
| 91 | + ] |
| 92 | + |
| 93 | + create_check_in_opts_schema = [ |
| 94 | + check_in_id: [ |
| 95 | + type: :string |
| 96 | + ], |
| 97 | + status: [ |
| 98 | + type: {:in, [:in_progress, :ok, :error]}, |
| 99 | + required: true, |
| 100 | + type_doc: "`t:status/0`" |
| 101 | + ], |
| 102 | + monitor_slug: [ |
| 103 | + type: :string, |
| 104 | + required: true |
| 105 | + ], |
| 106 | + duration: number_schema_opts, |
| 107 | + contexts: [ |
| 108 | + type: :map, |
| 109 | + default: %{}, |
| 110 | + doc: """ |
| 111 | + The contexts to attach to the check-in. This is a map of arbitrary data, |
| 112 | + but right now Sentry supports the `trace_id` key under the |
| 113 | + [trace context](https://develop.sentry.dev/sdk/event-payloads/contexts/#trace-context) |
| 114 | + to connect the check-in with related errors. |
| 115 | + """ |
| 116 | + ], |
| 117 | + monitor_config: [ |
| 118 | + doc: "If you pass this optional option, you **must** pass the nested `:schedule` option.", |
| 119 | + type: :keyword_list, |
| 120 | + keys: [ |
| 121 | + checkin_margin: number_schema_opts, |
| 122 | + max_runtime: number_schema_opts, |
| 123 | + failure_issue_threshold: number_schema_opts, |
| 124 | + recovery_threshold: number_schema_opts, |
| 125 | + timezone: [type: :string], |
| 126 | + schedule: [ |
| 127 | + type: |
| 128 | + {:or, |
| 129 | + [ |
| 130 | + {:keyword_list, crontab_schedule_opts_schema}, |
| 131 | + {:keyword_list, interval_schedule_opts_schema} |
| 132 | + ]}, |
| 133 | + type_doc: "`t:monitor_config_schedule/0`" |
| 134 | + ] |
| 135 | + ] |
| 136 | + ] |
| 137 | + ] |
| 138 | + |
| 139 | + @create_check_in_opts_schema NimbleOptions.new!(create_check_in_opts_schema) |
| 140 | + |
| 141 | + @doc """ |
| 142 | + Creates a new check-in struct with the given options. |
| 143 | +
|
| 144 | + ## Options |
| 145 | +
|
| 146 | + The options you can pass match a subset of the fields of the `t:t/0` struct. |
| 147 | + You can pass: |
| 148 | +
|
| 149 | + #{NimbleOptions.docs(@create_check_in_opts_schema)} |
| 150 | +
|
| 151 | + ## Examples |
| 152 | +
|
| 153 | + iex> check_in = CheckIn.new(status: :ok, monitor_slug: "my-slug") |
| 154 | + iex> check_in.status |
| 155 | + :ok |
| 156 | + iex> check_in.monitor_slug |
| 157 | + "my-slug" |
| 158 | +
|
| 159 | + """ |
| 160 | + @spec new(keyword()) :: t() |
| 161 | + def new(opts) when is_list(opts) do |
| 162 | + opts = NimbleOptions.validate!(opts, @create_check_in_opts_schema) |
| 163 | + |
| 164 | + monitor_config = |
| 165 | + case Keyword.fetch(opts, :monitor_config) do |
| 166 | + {:ok, monitor_config} -> |
| 167 | + monitor_config |
| 168 | + |> Map.new() |
| 169 | + |> Map.update!(:schedule, &Map.new/1) |
| 170 | + |
| 171 | + :error -> |
| 172 | + nil |
| 173 | + end |
| 174 | + |
| 175 | + %__MODULE__{ |
| 176 | + check_in_id: Keyword.get_lazy(opts, :check_in_id, &UUID.uuid4_hex/0), |
| 177 | + status: Keyword.fetch!(opts, :status), |
| 178 | + monitor_slug: Keyword.fetch!(opts, :monitor_slug), |
| 179 | + duration: Keyword.get(opts, :duration), |
| 180 | + release: Config.release(), |
| 181 | + environment: Config.environment_name(), |
| 182 | + monitor_config: monitor_config, |
| 183 | + contexts: Keyword.fetch!(opts, :contexts) |
| 184 | + } |
| 185 | + end |
| 186 | + |
| 187 | + # Used to then encode the returned map to JSON. |
| 188 | + @doc false |
| 189 | + @spec to_map(t()) :: map() |
| 190 | + def to_map(%__MODULE__{} = check_in) do |
| 191 | + Map.from_struct(check_in) |
| 192 | + end |
| 193 | +end |
0 commit comments