Skip to content

Commit 4642808

Browse files
committed
can set labels from span attributes, can export as histogram
1 parent 426fc49 commit 4642808

6 files changed

Lines changed: 141 additions & 42 deletions

File tree

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule OpencensusErlangPrometheus.Mixfile do
3232

3333
defp deps do
3434
[{:prometheus, "~> 3.4", manager: :rebar3},
35-
{:opencensus, git: "https://github.com/deadtrickster/opencensus-erlang", branch: "fix-mix-dep"},
35+
{:opencensus, git: "https://github.com/deadtrickster/opencensus-erlang", branch: "dev"},
3636
{:credo, "~> 0.8.7", only: [:dev, :test]},
3737
{:excoveralls, "~> 0.7.3", only: [:test]}]
3838
end

mix.lock

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
%{"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [], [], "hexpm"},
2-
"certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [], [], "hexpm"},
3-
"credo": {:hex, :credo, "0.8.7", "b1aad9cd3aa7acdbaea49765bfc9f1605dc4555023a037dc9ea7a70539615bc8", [], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}], "hexpm"},
4-
"excoveralls": {:hex, :excoveralls, "0.7.3", "6c05cc452e6ebfc347fccc0de7339e9bc1ed8678c52765353303fa0d2c44c70d", [], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
5-
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"},
6-
"hackney": {:hex, :hackney, "1.9.0", "51c506afc0a365868469dcfc79a9d0b94d896ec741cfd5bd338f49a5ec515bfe", [], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
7-
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
8-
"jsx": {:hex, :jsx, "2.8.2", "7acc7d785b5abe8a6e9adbde926a24e481f29956dd8b4df49e3e4e7bcc92a018", [], [], "hexpm"},
9-
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [], [], "hexpm"},
10-
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [], [], "hexpm"},
11-
"opencensus": {:git, "https://github.com/deadtrickster/opencensus-erlang", "c31853dfb1656f0d057cf69b569a8792ef5bd6c5", [branch: "fix-mix-dep"]},
12-
"prometheus": {:hex, :prometheus, "3.4.1", "90c4f37670c4dc7de9319a0c310bf12cb619715ebe285f583f3b46d955ee8f81", [], [], "hexpm"},
13-
"rebar_erl_vsn": {:hex, :rebar_erl_vsn, "0.1.3", "431667f3203f19130805a8ac7cde9666d1bff7235eb2b96e2048eb839db0ed1f", [], [], "hexpm"},
14-
"rfc3339": {:hex, :rfc3339, "0.9.0", "2075653dc9407541c84b1e15f8bda2abe95fb17c9694025e079583f2d19c1060", [], [], "hexpm"},
15-
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [], [], "hexpm"},
16-
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [], [], "hexpm"},
17-
"wts": {:hex, :wts, "0.3.0", "5cdf22c775cb1ebae24c326a5db6074d753c42f4bd12a9aa47cc62d3e2c71ad1", [], [{:rfc3339, "~>0.9.0", [hex: :rfc3339, repo: "hexpm", optional: false]}], "hexpm"}}
1+
%{"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
2+
"certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [:rebar3], [], "hexpm"},
3+
"credo": {:hex, :credo, "0.8.7", "b1aad9cd3aa7acdbaea49765bfc9f1605dc4555023a037dc9ea7a70539615bc8", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}], "hexpm"},
4+
"excoveralls": {:hex, :excoveralls, "0.7.3", "6c05cc452e6ebfc347fccc0de7339e9bc1ed8678c52765353303fa0d2c44c70d", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
5+
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"},
6+
"hackney": {:hex, :hackney, "1.9.0", "51c506afc0a365868469dcfc79a9d0b94d896ec741cfd5bd338f49a5ec515bfe", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
7+
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
8+
"jsx": {:hex, :jsx, "2.8.2", "7acc7d785b5abe8a6e9adbde926a24e481f29956dd8b4df49e3e4e7bcc92a018", [:mix, :rebar3], [], "hexpm"},
9+
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
10+
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
11+
"opencensus": {:git, "https://github.com/deadtrickster/opencensus-erlang", "7e248db230955fb3c441627dd2c7375aa67eb5a5", [branch: "dev"]},
12+
"prometheus": {:hex, :prometheus, "3.4.1", "90c4f37670c4dc7de9319a0c310bf12cb619715ebe285f583f3b46d955ee8f81", [:mix, :rebar3], [], "hexpm"},
13+
"rfc3339": {:hex, :rfc3339, "0.9.0", "2075653dc9407541c84b1e15f8bda2abe95fb17c9694025e079583f2d19c1060", [:mix, :rebar], [], "hexpm"},
14+
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"},
15+
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [:rebar3], [], "hexpm"},
16+
"wts": {:hex, :wts, "0.3.0", "5cdf22c775cb1ebae24c326a5db6074d753c42f4bd12a9aa47cc62d3e2c71ad1", [:rebar3], [{:rfc3339, "~>0.9.0", [hex: :rfc3339, repo: "hexpm", optional: false]}], "hexpm"}}

rebar.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
{deps, [{prometheus, "3.4.1"},
77
{opencensus,
8-
{git, "https://github.com/census-instrumentation/opencensus-erlang",
9-
{branch, "master"}}}]}.
8+
{git, "https://github.com/deadtrickster/opencensus-erlang",
9+
{branch, "dev"}}}]}.
1010
% {shell, [{apps, [opencensus_erlang_prometheus]}]}.
1111
{cover_enabled, true}.
1212
{cover_export_enabled, true}.

rebar.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{"1.1.0",
22
[{<<"opencensus">>,
3-
{git,"https://github.com/census-instrumentation/opencensus-erlang",
4-
{ref,"b8fecf303896a9bdbfe9d1f83371a5bc1e88a28a"}},
3+
{git,"https://github.com/deadtrickster/opencensus-erlang",
4+
{ref,"7e248db230955fb3c441627dd2c7375aa67eb5a5"}},
55
0},
66
{<<"prometheus">>,{pkg,<<"prometheus">>,<<"3.4.1">>},0},
77
{<<"rfc3339">>,{pkg,<<"rfc3339">>,<<"0.9.0">>},2},

src/oc_prometheus_reporter.erl

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,65 @@
1111
%% API
1212
%% ===================================================================
1313

14-
init(_) ->
15-
prometheus_summary:declare([{name, "span"},
16-
{labels, [name]},
17-
{help, "Opencensus span"}]),
18-
ok.
19-
20-
report(Spans, _Opts) ->
21-
[
22-
prometheus_summary:observe("span", [Span#span.name],
23-
wts:duration(Span#span.start_time, Span#span.end_time))
14+
init(Config) ->
15+
Type = config_metric_type(Config),
16+
Name = config_metric_name(Config),
17+
Labels = config_metric_labels(Config),
18+
{LabelNames, Attributes} = lists:unzip(Labels),
19+
Type:declare([{name, Name},
20+
{labels, [name] ++ LabelNames},
21+
{buckets, config_metric_buckets(Config)},
22+
{help, "Opencensus span metric"}]),
23+
{Type, Name, Attributes}.
2424

25-
|| Span <- Spans],
25+
report(Spans, Opts) ->
26+
[observe_span(Span, Opts) || Span <- Spans],
2627
ok.
2728

28-
2929
%% ===================================================================
3030
%% Private functions
3131
%% ===================================================================
32+
33+
config_metric_type(Config) ->
34+
type_to_module(proplists:get_value(type, Config, summary)).
35+
36+
config_metric_name(Config) ->
37+
proplists:get_value(name, Config, span).
38+
39+
config_metric_buckets(Config) ->
40+
proplists:get_value(buckets, Config, default).
41+
42+
config_metric_labels(Config) ->
43+
[normalize_label(Label)
44+
|| Label <- proplists:get_value(labels, Config, [])].
45+
46+
normalize_label(Name) when is_atom(Name) ->
47+
NameStr = atom_to_binary(Name, utf8),
48+
{NameStr, NameStr};
49+
normalize_label(Name) when is_binary(Name) ->
50+
{Name, Name};
51+
normalize_label(Name) when is_list(Name) ->
52+
{Name, list_to_binary(Name)};
53+
normalize_label({Label, Attribute}) when is_binary(Attribute) ->
54+
{Label, Attribute}.
55+
56+
compute_labels(Span, Attributes) ->
57+
Map = Span#span.attributes,
58+
[maps:get(Attribute, Map, "N/A") || Attribute <- Attributes].
59+
60+
observe_span(Span, {Type, MetricName, Attributes}) ->
61+
Labels = [Span#span.name] ++ compute_labels(Span, Attributes),
62+
Type:observe(MetricName, Labels, Span#span.duration).
63+
64+
type_to_module(counter) ->
65+
prometheus_counter;
66+
type_to_module(gauge) ->
67+
prometheus_gauge;
68+
type_to_module(summary) ->
69+
prometheus_summary;
70+
type_to_module(histogram) ->
71+
prometheus_histogram;
72+
type_to_module(boolean) ->
73+
prometheus_boolean;
74+
type_to_module(Type) ->
75+
Type.

test/ct/oep_SUITE.erl

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,46 @@
88
-include_lib("opencensus/include/opencensus.hrl").
99

1010
all() ->
11-
[prometheus_reporter].
11+
[prometheus_reporter_default,
12+
prometheus_reporter_labels,
13+
prometheus_reporter_histogram].
1214

1315
init_per_suite(Config) ->
16+
ok = application:load(opencensus),
1417
Config.
1518

1619
end_per_suite(_Config) ->
1720
ok.
1821

22+
init_per_testcase(prometheus_reporter_default, Config) ->
23+
application:set_env(opencensus, reporter, {oc_prometheus_reporter, []}),
24+
{ok, _} = application:ensure_all_started(opencensus_erlang_prometheus),
25+
Config;
26+
init_per_testcase(prometheus_reporter_labels, Config) ->
27+
application:set_env(opencensus, reporter,
28+
{oc_prometheus_reporter, [{name, span_with_labels},
29+
{labels, [op_name, {common_name, <<"cname">>}]}]}),
30+
{ok, _} = application:ensure_all_started(opencensus_erlang_prometheus),
31+
Config;
32+
init_per_testcase(prometheus_reporter_histogram, Config) ->
33+
application:set_env(opencensus, reporter,
34+
{oc_prometheus_reporter, [{type, histogram},
35+
{name, span_histogram_seconds},
36+
{buckets, [0, 1, 2]}]}),
37+
{ok, _} = application:ensure_all_started(opencensus_erlang_prometheus),
38+
Config.
39+
40+
end_per_testcase(_, _Config) ->
41+
ok = application:stop(opencensus_erlang_prometheus),
42+
ok = application:stop(opencensus),
43+
ok = application:stop(prometheus),
44+
ok.
45+
1946
%% ===================================================================
2047
%% Tests
2148
%% ===================================================================
2249

23-
prometheus_reporter(_Config) ->
24-
application:set_env(opencensus, reporter, {oc_prometheus_reporter, []}),
25-
ok = application:load(opencensus),
26-
application:set_env(opencensus, reporter, {oc_prometheus_reporter, []}),
27-
{ok, _} = application:ensure_all_started(opencensus_erlang_prometheus),
50+
prometheus_reporter_default(_Config) ->
2851

2952
SpanName1 = <<"span-1">>,
3053
Span1 = opencensus:start_span(SpanName1, opencensus:generate_trace_id(), undefined),
@@ -36,8 +59,41 @@ prometheus_reporter(_Config) ->
3659

3760
timer:sleep(1000),
3861

39-
?assertMatch({1, _}, prometheus_summary:value("span", [SpanName1])),
40-
?assertMatch({1, _}, prometheus_summary:value("span", [ChildSpanName1])).
62+
?assertMatch({1, _}, prometheus_summary:value(span, [SpanName1])),
63+
?assertMatch({1, _}, prometheus_summary:value(span, [ChildSpanName1])).
64+
65+
prometheus_reporter_labels(_Config) ->
66+
67+
SpanName1 = <<"span-1">>,
68+
Span1 = opencensus:start_span(SpanName1, opencensus:generate_trace_id(), #{<<"op_name">> => "test"}),
69+
70+
ChildSpanName1 = <<"child-span-1">>,
71+
ChildSpan1 = opencensus:start_span(ChildSpanName1, Span1, #{<<"cname">> => "github"}),
72+
73+
opencensus:finish_span(ChildSpan1),
74+
opencensus:finish_span(Span1),
75+
76+
timer:sleep(1000),
77+
78+
?assertMatch({1, _}, prometheus_summary:value(span_with_labels, [SpanName1, "test", "N/A"])),
79+
?assertMatch({1, _}, prometheus_summary:value(span_with_labels, [ChildSpanName1, "N/A", "github"])).
80+
81+
prometheus_reporter_histogram(_Config) ->
82+
83+
SpanName1 = <<"span-1">>,
84+
Span1 = opencensus:start_span(SpanName1, opencensus:generate_trace_id(), #{<<"op_name">> => "test"}),
85+
86+
ChildSpanName1 = <<"child-span-1">>,
87+
ChildSpan1 = opencensus:start_span(ChildSpanName1, Span1, #{<<"cname">> => "github"}),
88+
timer:sleep(1500),
89+
opencensus:finish_span(ChildSpan1),
90+
timer:sleep(1000),
91+
opencensus:finish_span(Span1),
92+
93+
timer:sleep(1000),
94+
95+
?assertMatch({[0,0,0,1], _}, prometheus_histogram:value(span_histogram_seconds, [SpanName1])),
96+
?assertMatch({[0,0,1,0], _}, prometheus_histogram:value(span_histogram_seconds, [ChildSpanName1])).
4197

4298
%% ===================================================================
4399
%% Private functions

0 commit comments

Comments
 (0)