@@ -23,6 +23,8 @@ defmodule Astarte.PairingWeb.Telemetry do
2323 import Telemetry.Metrics
2424
2525 alias Astarte.PairingWeb.Telemetry.APIUsage
26+ alias Astarte.PairingWeb.Telemetry.DatabaseEvents
27+ alias Astarte.Pairing.Config
2628
2729 def start_link ( arg ) do
2830 Supervisor . start_link ( __MODULE__ , arg , name: __MODULE__ )
@@ -129,10 +131,57 @@ defmodule Astarte.PairingWeb.Telemetry do
129131 counter ( "astarte.pairing.unregister_device.count" ,
130132 tags: [ :realm ] ,
131133 description: "Get unregister device requests total count"
134+ ) ,
135+
136+ # Database exception metrics
137+ counter ( "astarte.pairing.database.execute_query.exception.count" ,
138+ tags: [ :query , :reason , :kind , :stacktrace ] ,
139+ tag_values: & to_valid_values / 1 ,
140+ unit: { :native , :second }
141+ ) ,
142+ counter ( "astarte.pairing.database.execute_query.stop.count" ,
143+ tags: [ :query , :reason ] ,
144+ tag_values: & to_valid_values / 1 ,
145+ unit: { :native , :second }
146+ ) ,
147+
148+ # Database preparation metrics
149+ counter ( "astarte.pairing.database.prepare_query.exception.count" ,
150+ tags: [ :query , :reason , :kind , :stacktrace ] ,
151+ tag_values: & to_valid_values / 1 ,
152+ unit: { :native , :second }
153+ ) ,
154+ counter ( "astarte.pairing.database.prepare_query.stop.count" ,
155+ tags: [ :query , :reason ] ,
156+ tag_values: & to_valid_values / 1 ,
157+ unit: { :native , :second }
158+ ) ,
159+
160+ # Database connection metrics
161+ counter (
162+ "astarte.pairing.database.cluster.control_connection.failed_to_connect.count" ,
163+ tag_values: & to_valid_values / 1 ,
164+ tags: [ :cluster_name , :host , :reason ]
165+ ) ,
166+ counter ( "astarte.pairing.database.failed_to_connect.conut" ,
167+ tag_values: & to_valid_values / 1 ,
168+ tags: [ :connection_name , :address , :port ]
132169 )
133170 ]
134171 end
135172
173+ defp to_valid_values ( % { query: query , reason: reason } ) do
174+ % { query: query . statement , reason: Xandra.Error . message ( reason ) }
175+ end
176+
177+ defp to_valid_values ( % { cluster_name: cluster_name , host: host , reason: reason } ) do
178+ % { cluster_name: cluster_name , host: inspect ( host ) , reason: to_string ( reason ) }
179+ end
180+
181+ defp to_valid_values ( % { connection_name: connection_name , address: address , port: port } ) do
182+ % { connection_name: connection_name , address: inspect ( address ) , port: inspect ( port ) }
183+ end
184+
136185 defp periodic_measurements do
137186 [
138187 # A module, function and arguments to be invoked periodically.
@@ -143,6 +192,36 @@ defmodule Astarte.PairingWeb.Telemetry do
143192
144193 defp attach_handlers do
145194 :telemetry . attach ( APIUsage , [ :cowboy , :request , :stop ] , & APIUsage . handle_event / 4 , nil )
195+
196+ :telemetry . attach_many (
197+ DatabaseEvents ,
198+ xandra_events ( ) ,
199+ & DatabaseEvents . handle_event / 4 ,
200+ Config . database_events_handling_method! ( )
201+ )
202+ end
203+
204+ defp xandra_events do
205+ [
206+ [ :xandra , :connected ] ,
207+ [ :xandra , :disconnected ] ,
208+ [ :xandra , :failed_to_connect ] ,
209+ [ :xandra , :prepared_cache , :hit ] ,
210+ [ :xandra , :prepared_cache , :miss ] ,
211+ [ :xandra , :prepare_query , :stop ] ,
212+ [ :xandra , :execute_query , :stop ] ,
213+ [ :xandra , :client_timeout ] ,
214+ [ :xandra , :timed_out_response ] ,
215+ [ :xandra , :server_warnings ] ,
216+ [ :xandra , :cluster , :change_event ] ,
217+ [ :xandra , :cluster , :control_connection , :connected ] ,
218+ [ :xandra , :cluster , :control_connection , :disconnected ] ,
219+ [ :xandra , :cluster , :control_connection , :failed_to_connect ] ,
220+ [ :xandra , :cluster , :pool , :started ] ,
221+ [ :xandra , :cluster , :pool , :restarted ] ,
222+ [ :xandra , :cluster , :pool , :stopped ] ,
223+ [ :xandra , :cluster , :discovered_peers ]
224+ ]
146225 end
147226
148227 defp extract_phoenix_buckets_metadata ( % {
0 commit comments