1
1
% %%------------------------------------------------------------------------
2
- % % Copyright 2022 , OpenTelemetry Authors
2
+ % % Copyright 2024 , OpenTelemetry Authors
3
3
% % Licensed under the Apache License, Version 2.0 (the "License");
4
4
% % you may not use this file except in compliance with the License.
5
5
% % You may obtain a copy of the License at
17
17
% %%-------------------------------------------------------------------------
18
18
-module (otel_metric_reader_prometheus ).
19
19
20
- -behaviour (supervisor ).
21
-
22
20
-export ([start_link /3 ,
23
- collect /1 ,
21
+ collect /0 , collect / 1 ,
24
22
shutdown /1 ]).
25
23
26
- -export ([init /1 ,
27
- do /1 ]).
24
+ -ignore_xref (? MODULE ).
28
25
29
26
-include_lib (" kernel/include/logger.hrl" ).
30
- -include_lib (" inets/include/httpd.hrl" ).
31
27
-include_lib (" opentelemetry_api_experimental/include/otel_metrics.hrl" ).
32
28
33
29
-define (TEMPORALITY_MAPPING , #{
34
- ? KIND_COUNTER => ? TEMPORALITY_CUMULATIVE ,
35
- ? KIND_OBSERVABLE_COUNTER => ? TEMPORALITY_CUMULATIVE ,
36
- ? KIND_HISTOGRAM => ? TEMPORALITY_CUMULATIVE ,
37
- ? KIND_OBSERVABLE_GAUGE => ? TEMPORALITY_CUMULATIVE ,
38
- ? KIND_UPDOWN_COUNTER => ? TEMPORALITY_CUMULATIVE ,
39
- ? KIND_OBSERVABLE_UPDOWNCOUNTER => ? TEMPORALITY_CUMULATIVE
40
- }).
30
+ ? KIND_COUNTER => ? TEMPORALITY_CUMULATIVE ,
31
+ ? KIND_OBSERVABLE_COUNTER => ? TEMPORALITY_CUMULATIVE ,
32
+ ? KIND_HISTOGRAM => ? TEMPORALITY_CUMULATIVE ,
33
+ ? KIND_OBSERVABLE_GAUGE => ? TEMPORALITY_CUMULATIVE ,
34
+ ? KIND_UPDOWN_COUNTER => ? TEMPORALITY_CUMULATIVE ,
35
+ ? KIND_OBSERVABLE_UPDOWNCOUNTER => ? TEMPORALITY_CUMULATIVE
36
+ }).
37
+
38
+ -define (SERVER , ? MODULE ).
41
39
42
- start_link (ReaderId , ProviderSup , Config ) ->
43
- supervisor :start_link (? MODULE , [ReaderId , ProviderSup , Config ]).
40
+ % %%===================================================================
41
+ % %% API
42
+ % %%===================================================================
44
43
45
- init ([ ReaderId , ProviderSup , Config ] ) ->
44
+ start_link ( ReaderId , ProviderSup , Config0 ) ->
46
45
% TODO warning if default_temporality_mapping, export_interval_ms, exporter
47
46
% are present in the configuration
48
- Config1 = maps :put (default_temporality_mapping , ? TEMPORALITY_MAPPING , Config ),
49
- Config2 = maps :remove (export_interval_ms , Config1 ),
50
- Config3 = maps :put (exporter , {otel_metric_exporter_prometheus , Config2 }, Config2 ),
51
-
52
- SupFlags = #{strategy => one_for_one ,
53
- intensity => 5 ,
54
- period => 10 },
47
+ Config1 = maps :remove (export_interval_ms , Config0 ),
48
+ Config2 = Config1 #{default_temporality_mapping => ? TEMPORALITY_MAPPING },
49
+ Config = Config2 #{exporter => {otel_metric_exporter_prometheus , Config2 }},
55
50
56
- ReaderChildSpec = #{
57
- id => ReaderId ,
58
- start => {otel_metric_reader , start_link , [ReaderId , ProviderSup , Config3 ]},
59
- type => worker ,
60
- restart => permanent ,
61
- shutdown => 1000
62
- },
51
+ case Config of
52
+ #{server_name := ServerName } ->
53
+ gen_server :start_link (ServerName , otel_metric_reader ,
54
+ [ReaderId , ProviderSup , Config ], []);
55
+ _ ->
56
+ gen_server :start_link (otel_metric_reader ,
57
+ [ReaderId , ProviderSup , Config ], [])
58
+ end .
63
59
64
- ChildSpecs = case maps :get (endpoint_port , Config , undefined ) of
65
- undefined ->
66
- [ReaderChildSpec ];
67
- HttpdPort when is_integer (HttpdPort ) ->
68
- HttpdOpts = [
69
- {server_name , " OTel Prometheus exporter" },
70
- {server_tokens , {private , " TODO" }},
71
- {server_root , " /tmp" },
72
- {document_root , " /tmp" },
73
- {port , HttpdPort },
74
- {modules , [? MODULE ]},
75
- {otel_metric_reader , {self (), ReaderId }},
76
- {pt_key , make_ref ()}
77
- ],
78
- HttpdChildSpec = #{
79
- id => make_ref (),
80
- start => {inets , start , [httpd , HttpdOpts , stand_alone ]},
81
- type => worker ,
82
- restart => permanent ,
83
- shutdown => 1000
84
- },
85
- [ReaderChildSpec , HttpdChildSpec ]
86
- end ,
87
-
88
- {ok , {SupFlags , ChildSpecs }}.
60
+ collect () ->
61
+ collect (? SERVER ).
89
62
90
63
collect (ReaderPid ) ->
91
64
otel_metric_reader :collect (ReaderPid ).
92
65
93
66
shutdown (ReaderPid ) ->
94
67
otel_metric_reader :shutdown (ReaderPid ).
95
-
96
- do (# mod {method = " GET" ,request_uri = " /metrics" ,config_db = ConfigDb }) ->
97
- ReaderPid = get_reader_pid (ConfigDb ),
98
- Metrics = collect (ReaderPid ),
99
- Headers = [
100
- {code , 200 },
101
- {content_length , integer_to_list (iolist_size (Metrics ))},
102
- {content_type , " text/plain; version=0.0.4" }
103
- ],
104
- {proceed , [{response , {response , Headers , Metrics }}]};
105
- do (# mod {}) ->
106
- {proceed , [{response , {404 , " Not found" }}]}.
107
-
108
- get_reader_pid (ConfigDb ) ->
109
- [PTKey ] = ets :lookup_element (ConfigDb , pt_key , 2 ),
110
- case persistent_term :get (PTKey , undefined ) of
111
- undefined ->
112
- [{ReaderSupPid , ReaderId }] = ets :lookup_element (ConfigDb , otel_metric_reader , 2 ),
113
- Children = supervisor :which_children (ReaderSupPid ),
114
- {value , {_ , ReaderPid , _ , _ }} = lists :search (fun ({Id , _ , _ , _ }) -> Id == ReaderId end , Children ),
115
- persistent_term :put (PTKey , ReaderPid ),
116
- ReaderPid ;
117
- ReaderPid ->
118
- ReaderPid
119
- end .
0 commit comments