Skip to content

Commit

Permalink
Add UNIT to exposition
Browse files Browse the repository at this point in the history
  • Loading branch information
bfolkens committed Jan 3, 2025
1 parent 3acbcc7 commit 825c544
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/core/exporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ defmodule TelemetryMetricsPrometheus.Core.Exporter do
name = format_name(metric.name)
help = "# HELP #{name} #{escape_help(metric.description)}"
type = "# TYPE #{name} histogram"
unit = "# UNIT #{name} #{metric.unit}"

distributions =
Enum.map_join(time_series, "\n", fn ts ->
format_distribution(metric, ts)
end)

Enum.join([help, type, distributions], "\n")
Enum.join([help, type, unit, distributions], "\n")
end

def format_distribution(metric, {{_, labels}, {buckets, count, sum}}) do
Expand Down Expand Up @@ -76,6 +77,7 @@ defmodule TelemetryMetricsPrometheus.Core.Exporter do
name = format_name(metric.name)
help = "# HELP #{name} #{escape_help(metric.description)}"
type = "# TYPE #{name} #{type}"
unit = "# UNIT #{name} #{metric.unit}"

samples =
Enum.map_join(time_series, "\n", fn {{_, labels}, val} ->
Expand All @@ -88,7 +90,7 @@ defmodule TelemetryMetricsPrometheus.Core.Exporter do
end
end)

Enum.join([help, type, samples], "\n")
Enum.join([help, type, unit, samples], "\n")
end

defp format_labels(labels) do
Expand Down
14 changes: 14 additions & 0 deletions test/exporter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP http_request_total
# TYPE http_request_total counter
# UNIT http_request_total unit
http_request_total 1027
# HELP cache_key_total
# TYPE cache_key_total gauge
# UNIT cache_key_total unit
cache_key_total 3
"""

Expand Down Expand Up @@ -39,6 +41,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP http_request_total The total number of HTTP requests.
# TYPE http_request_total counter
# UNIT http_request_total unit
http_request_total{code="200",method="post"} 1027
http_request_total{code="400",method="post"} 3\
"""
Expand All @@ -63,6 +66,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP http_request_total The total number of HTTP requests.
# TYPE http_request_total counter
# UNIT http_request_total unit
http_request_total 1027\
"""

Expand All @@ -85,6 +89,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
~S(# HELP db_query_total The total number of DB queries. \\\\ \\n) <>
"\n" <>
"# TYPE db_query_total counter\n" <>
"# UNIT db_query_total unit\n" <>
~S(db_query_total{query="SELECT a0.\"id\" FROM \"users\" AS a0 WHERE LIMIT $1"} 1027) <>
"\n" <>
~S(db_query_total{query="\\n \\\\ \""} 4242)
Expand All @@ -110,6 +115,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP cache_keys_total The total number of cache keys.
# TYPE cache_keys_total gauge
# UNIT cache_keys_total unit
cache_keys_total{name="users"} 1027
cache_keys_total{name="short_urls"} 3\
"""
Expand All @@ -134,6 +140,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP cache_key_total The total number of cache keys.
# TYPE cache_key_total gauge
# UNIT cache_key_total unit
cache_key_total 1027\
"""

Expand All @@ -155,6 +162,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP cache_key_invalidations_total The total number of cache key invalidations.
# TYPE cache_key_invalidations_total gauge
# UNIT cache_key_invalidations_total unit
cache_key_invalidations_total{name="users"} 1027
cache_key_invalidations_total{name="short_urls"} 3\
"""
Expand All @@ -180,6 +188,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP cache_key_invalidations_total The total number of cache key invalidations.
# TYPE cache_key_invalidations_total counter
# UNIT cache_key_invalidations_total unit
cache_key_invalidations_total{name="users"} 1027
cache_key_invalidations_total{name="short_urls"} 3\
"""
Expand All @@ -204,6 +213,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP cache_key_invalidations_total The total number of cache key invalidations.
# TYPE cache_key_invalidations_total counter
# UNIT cache_key_invalidations_total unit
cache_key_invalidations_total 1027\
"""

Expand All @@ -225,6 +235,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
# UNIT http_request_duration_seconds second
http_request_duration_seconds_bucket{method="GET",le="0.05"} 24054
http_request_duration_seconds_bucket{method="GET",le="0.1"} 33444
http_request_duration_seconds_bucket{method="GET",le="0.2"} 100392
Expand Down Expand Up @@ -276,6 +287,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
# UNIT http_request_duration_seconds second
http_request_duration_seconds_bucket{le="0.05"} 24054
http_request_duration_seconds_bucket{le="0.1"} 33444
http_request_duration_seconds_bucket{le="0.2"} 100392
Expand Down Expand Up @@ -311,6 +323,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP foo123_bar_total FOO123 BAR total
# TYPE foo123_bar_total counter
# UNIT foo123_bar_total unit
foo123_bar_total 1027\
"""

Expand All @@ -329,6 +342,7 @@ defmodule TelemetryMetricsPrometheus.Core.ExporterTest do
expected = """
# HELP foo_bar_total 123FOO BAR total
# TYPE foo_bar_total counter
# UNIT foo_bar_total unit
foo_bar_total 1027\
"""

Expand Down

0 comments on commit 825c544

Please sign in to comment.