|
| 1 | +defmodule Sentry.Metric do |
| 2 | + @moduledoc """ |
| 3 | + Represents a metric that can be sent to Sentry. |
| 4 | +
|
| 5 | + Metrics follow the Sentry Metrics Protocol as defined in: |
| 6 | + <https://develop.sentry.dev/sdk/telemetry/metrics/> |
| 7 | +
|
| 8 | + This module is used by `Sentry.Metrics` to create and send metric data to Sentry. |
| 9 | + """ |
| 10 | + @moduledoc since: "13.0.0" |
| 11 | + |
| 12 | + alias Sentry.Config |
| 13 | + |
| 14 | + @type metric_type :: :counter | :gauge | :distribution |
| 15 | + |
| 16 | + @typedoc """ |
| 17 | + A metric struct. |
| 18 | + """ |
| 19 | + @type t :: %__MODULE__{ |
| 20 | + type: metric_type(), |
| 21 | + name: String.t(), |
| 22 | + value: number(), |
| 23 | + timestamp: float(), |
| 24 | + trace_id: String.t() | nil, |
| 25 | + span_id: String.t() | nil, |
| 26 | + unit: String.t() | nil, |
| 27 | + attributes: map() |
| 28 | + } |
| 29 | + |
| 30 | + @enforce_keys [:type, :name, :value, :timestamp] |
| 31 | + defstruct [ |
| 32 | + :type, |
| 33 | + :name, |
| 34 | + :value, |
| 35 | + :timestamp, |
| 36 | + :trace_id, |
| 37 | + :span_id, |
| 38 | + :unit, |
| 39 | + attributes: %{} |
| 40 | + ] |
| 41 | + |
| 42 | + @sdk_version Mix.Project.config()[:version] |
| 43 | + |
| 44 | + @doc """ |
| 45 | + Attaches default attributes to a metric. |
| 46 | +
|
| 47 | + This adds Sentry-specific attributes like environment, release, SDK info, and server name. |
| 48 | + Per the Sentry Metrics spec, default attributes should be attached before the |
| 49 | + `before_send_metric` callback is applied (step 5 before step 6). |
| 50 | + """ |
| 51 | + @spec attach_default_attributes(t()) :: t() |
| 52 | + def attach_default_attributes(%__MODULE__{} = metric) do |
| 53 | + default_attrs = %{ |
| 54 | + "sentry.sdk.name" => "sentry.elixir", |
| 55 | + "sentry.sdk.version" => @sdk_version |
| 56 | + } |
| 57 | + |
| 58 | + # Add optional attributes if configured |
| 59 | + default_attrs = |
| 60 | + default_attrs |
| 61 | + |> maybe_put_attr("sentry.environment", Config.environment_name()) |
| 62 | + |> maybe_put_attr("sentry.release", Config.release()) |
| 63 | + |> maybe_put_attr("server.address", Config.server_name()) |
| 64 | + |
| 65 | + # Merge with user attributes (user attributes take precedence) |
| 66 | + %{metric | attributes: Map.merge(default_attrs, metric.attributes)} |
| 67 | + end |
| 68 | + |
| 69 | + defp maybe_put_attr(attrs, _key, nil), do: attrs |
| 70 | + defp maybe_put_attr(attrs, key, value), do: Map.put(attrs, key, value) |
| 71 | + |
| 72 | + @doc """ |
| 73 | + Converts a metric to a map suitable for JSON encoding. |
| 74 | +
|
| 75 | + The output matches the Sentry metrics schema with top-level fields: timestamp, type, name, |
| 76 | + value, unit, trace_id, span_id, and attributes. The attributes are formatted with type |
| 77 | + information as required by the protocol. |
| 78 | + """ |
| 79 | + @spec to_map(t()) :: %{optional(atom()) => term()} |
| 80 | + def to_map(%__MODULE__{} = metric) do |
| 81 | + %{ |
| 82 | + timestamp: metric.timestamp, |
| 83 | + type: to_string(metric.type), |
| 84 | + name: metric.name, |
| 85 | + value: metric.value, |
| 86 | + attributes: format_attributes(metric.attributes) |
| 87 | + } |
| 88 | + |> maybe_put(:unit, metric.unit) |
| 89 | + |> maybe_put(:trace_id, metric.trace_id) |
| 90 | + |> maybe_put(:span_id, metric.span_id) |
| 91 | + end |
| 92 | + |
| 93 | + defp maybe_put(map, _key, nil), do: map |
| 94 | + defp maybe_put(map, key, value), do: Map.put(map, key, value) |
| 95 | + |
| 96 | + ## Helpers |
| 97 | + |
| 98 | + # Format attributes to the protocol format with type information |
| 99 | + defp format_attributes(attributes) when is_map(attributes) do |
| 100 | + Enum.into(attributes, %{}, fn {key, value} -> |
| 101 | + safe_value = sanitize_attribute_value(value) |
| 102 | + {to_string(key), %{value: safe_value, type: attribute_type(safe_value)}} |
| 103 | + end) |
| 104 | + end |
| 105 | + |
| 106 | + # Converts values to JSON-safe attribute types. |
| 107 | + # Primitives (string, boolean, integer, float) pass through unchanged. |
| 108 | + # Atoms are converted to strings. All other types (structs, maps, lists, |
| 109 | + # tuples, PIDs, etc.) are converted to their inspect() representation. |
| 110 | + # Note: is_boolean must come before is_atom since true/false are atoms |
| 111 | + defp sanitize_attribute_value(value) when is_binary(value), do: value |
| 112 | + defp sanitize_attribute_value(value) when is_boolean(value), do: value |
| 113 | + defp sanitize_attribute_value(value) when is_atom(value), do: Atom.to_string(value) |
| 114 | + defp sanitize_attribute_value(value) when is_integer(value), do: value |
| 115 | + defp sanitize_attribute_value(value) when is_float(value), do: value |
| 116 | + defp sanitize_attribute_value(value), do: inspect(value) |
| 117 | + |
| 118 | + defp attribute_type(value) when is_boolean(value), do: "boolean" |
| 119 | + defp attribute_type(value) when is_integer(value), do: "integer" |
| 120 | + defp attribute_type(value) when is_float(value), do: "double" |
| 121 | + defp attribute_type(_value), do: "string" |
| 122 | +end |
0 commit comments