@@ -12,6 +12,7 @@ defmodule Logflare.Backends.UserMonitoringTest do
1212 alias Logflare.Backends.UserMonitoring
1313 alias Logflare.SystemMetrics.AllLogsLogged
1414 alias Logflare.LogEvent
15+ alias Logflare.Endpoints
1516
1617 def source_and_user ( _context ) do
1718 start_supervised! ( AllLogsLogged )
@@ -87,16 +88,16 @@ defmodule Logflare.Backends.UserMonitoringTest do
8788 end
8889 end
8990
90-
9191 describe "system monitoring labels" do
9292 setup :start_otel_exporter
93+
9394 setup do
9495 start_supervised! ( AllLogsLogged )
9596 insert ( :plan )
9697 :ok
9798 end
9899
99- test "backends.ingest.ingested_bytes and backends.ingest.ingested_count" do
100+ test "backends.ingest.ingested_bytes and backends.ingest.ingested_count" do
100101 GoogleApi.BigQuery.V2.Api.Tabledata
101102 |> stub ( :bigquery_tabledata_insert_all , fn _conn ,
102103 _project_id ,
@@ -171,42 +172,101 @@ test "backends.ingest.ingested_bytes and backends.ingest.ingested_count" do
171172 start_supervised! ( { SourceSup , other_source } , id: :other_source )
172173 start_supervised! ( { SourceSup , metrics_source } , id: :metrics_source )
173174 start_supervised! ( { SourceSup , other_metrics_source } , id: :other_metrics_source )
175+
174176 GoogleApi.BigQuery.V2.Api.Tabledata
175177 |> stub ( :bigquery_tabledata_insert_all , fn _conn ,
176178 _project_id ,
177179 dataset_id ,
178180 _table_name ,
179181 opts ->
180- if String . starts_with? ( dataset_id , "#{ other_user . id } " ) do
181- send ( pid , { :insert_all , opts [ :body ] . rows } )
182- end
183- { :ok , % GoogleApi.BigQuery.V2.Model.TableDataInsertAllResponse { insertErrors: nil } }
182+ if String . starts_with? ( dataset_id , "#{ other_user . id } " ) do
183+ send ( pid , { :insert_all , opts [ :body ] . rows } )
184+ end
185+
186+ { :ok , % GoogleApi.BigQuery.V2.Model.TableDataInsertAllResponse { insertErrors: nil } }
184187 end )
185188
186189 :timer . sleep ( 500 )
187190
188-
189191 assert { :ok , _ } = Backends . ingest_logs ( [ % { "metadata" => % { "value" => "test" } } ] , source )
190192
191193 assert { :ok , _ } =
192194 Backends . ingest_logs ( [ % { "metadata" => % { "value" => "different" } } ] , other_source )
193- :timer . sleep ( 1500 )
195+
196+ :timer . sleep ( 1500 )
194197
195198 source_id = source . id
196199 other_source_id = other_source . id
197200 metrics_source_id = metrics_source . id
198201 other_metrics_source_id = other_metrics_source . id
199- assert_receive { :insert_all , [ % { json: % { "attributes" => _ } } | _ ] = rows } , 5_000
202+ assert_receive { :insert_all , [ % { json: % { "attributes" => _ } } | _ ] = rows } , 5_000
200203
201204 rows = for row <- rows , do: row . json
202205
203206 assert Enum . all? ( rows , & match? ( % { "attributes" => [ % { "my_label" => "different" } ] } , & 1 ) )
207+
204208 for row <- rows , attr <- row [ "attributes" ] do
205209 assert attr [ "source_id" ] in [ other_source_id , other_metrics_source_id ]
206210 refute attr [ "source_id" ] in [ source_id , metrics_source_id ]
207- refute attr [ "my_label" ] == "test"
211+ refute attr [ "my_label" ] == "test"
208212 end
213+ end
214+ end
215+
216+ describe "endpoints" do
217+ setup :start_otel_exporter
218+
219+ setup do
220+ start_supervised! ( AllLogsLogged )
221+ insert ( :plan )
222+ :ok
223+ end
209224
225+ test "endpoints.query.total_processed_bytes" do
226+ pid = self ( )
227+
228+ GoogleApi.BigQuery.V2.Api.Tabledata
229+ |> stub ( :bigquery_tabledata_insert_all , fn _conn ,
230+ _project_id ,
231+ dataset_id ,
232+ _table_name ,
233+ opts ->
234+ send ( pid , { :insert_all , opts [ :body ] . rows } )
235+ { :ok , % GoogleApi.BigQuery.V2.Model.TableDataInsertAllResponse { insertErrors: nil } }
236+ end )
237+
238+ expect ( GoogleApi.BigQuery.V2.Api.Jobs , :bigquery_jobs_query , 1 , fn _conn , _proj_id , opts ->
239+ { :ok , TestUtils . gen_bq_response ( [ % { "result" => "1" } ] ) }
240+ end )
241+
242+ user = insert ( :user , system_monitoring: true )
243+ source = insert ( :source , user: user , system_source_type: :metrics )
244+ start_supervised! ( { SourceSup , source } , id: :source )
245+ # execute a query on the endpoint
246+ endpoint =
247+ insert ( :endpoint ,
248+ user: user ,
249+ query: "SELECT 1" ,
250+ labels: "my_label=some_value" ,
251+ parsed_labels: % { "my_label" => "some_value" }
252+ )
253+
254+ assert { :ok , _ } = Endpoints . run_query ( endpoint )
255+ :timer . sleep ( 1000 )
256+ endpoint_id = endpoint . id
257+
258+ assert_receive { :insert_all ,
259+ [
260+ % {
261+ json: % {
262+ "attributes" => [
263+ % { "my_label" => "some_value" , "endpoint_id" => ^ endpoint_id }
264+ ]
265+ }
266+ }
267+ | _
268+ ] } ,
269+ 5_000
210270 end
211271 end
212272end
0 commit comments