@@ -28,6 +28,12 @@ defmodule Logflare.Backends.UserMonitoringTest do
2828 [ source: source , source_b: source_b , user: user ]
2929 end
3030
31+ def start_otel_exporter ( _context ) do
32+ [ spec ] = UserMonitoring . get_otel_exporter ( )
33+ start_supervised! ( spec )
34+ :ok
35+ end
36+
3137 describe "logs" do
3238 setup do
3339 :ok =
@@ -81,44 +87,16 @@ defmodule Logflare.Backends.UserMonitoringTest do
8187 end
8288 end
8389
84- describe "metrics" do
85- setup do
86- insert ( :plan )
87-
88- user_1 = insert ( :user )
89- user_1 = Users . preload_defaults ( user_1 )
90-
91- backend_1 =
92- insert ( :backend ,
93- user: user_1 ,
94- type: :webhook ,
95- config: % { url: "http://test.com" } ,
96- default_ingest?: false
97- )
98-
99- user_2 = insert ( :user )
100- user_2 = Users . preload_defaults ( user_2 )
101-
102- backend_2 =
103- insert ( :backend ,
104- user: user_2 ,
105- type: :webhook ,
106- config: % { url: "http://test.com" } ,
107- default_ingest?: false
108- )
109-
110- { :ok , user_1: user_1 , user_2: user_2 , backend_1: backend_1 , backend_2: backend_2 }
111- end
112- end
11390
11491 describe "system monitoring labels" do
92+ setup :start_otel_exporter
11593 setup do
11694 start_supervised! ( AllLogsLogged )
11795 insert ( :plan )
11896 :ok
11997 end
12098
121- test "applies to backend metrics " do
99+ test "backends.ingest.ingested_bytes and backends.ingest.ingested_count " do
122100 GoogleApi.BigQuery.V2.Api.Tabledata
123101 |> stub ( :bigquery_tabledata_insert_all , fn _conn ,
124102 _project_id ,
@@ -175,74 +153,60 @@ defmodule Logflare.Backends.UserMonitoringTest do
175153 end
176154
177155 test "other users metrics" do
178- GoogleApi.BigQuery.V2.Api.Tabledata
179- |> stub ( :bigquery_tabledata_insert_all , fn _conn ,
180- _project_id ,
181- _dataset_id ,
182- _table_name ,
183- _opts ->
184- { :ok , % GoogleApi.BigQuery.V2.Model.TableDataInsertAllResponse { insertErrors: nil } }
185- end )
156+ pid = self ( )
186157
187158 user =
188159 insert ( :user , system_monitoring: true )
189160
190161 other_user =
191162 insert ( :user , system_monitoring: true )
192163
193- % { id: source_id } = source = insert ( :source , user: user , labels: "my_label=m.value" )
164+ source = insert ( :source , user: user , labels: "my_label=m.value" )
194165
195- % { id: other_source_id } =
196- other_source = insert ( :source , user: other_user , labels: "my_label=m.value" )
166+ other_source = insert ( :source , user: other_user , labels: "my_label=m.value" )
197167
198168 metrics_source = insert ( :source , user: user , system_source_type: :metrics )
199169 other_metrics_source = insert ( :source , user: other_user , system_source_type: :metrics )
200- start_supervised! ( { SourceSup , metrics_source } , id: :metrics_source )
201170 start_supervised! ( { SourceSup , source } , id: :source )
202171 start_supervised! ( { SourceSup , other_source } , id: :other_source )
172+ start_supervised! ( { SourceSup , metrics_source } , id: :metrics_source )
203173 start_supervised! ( { SourceSup , other_metrics_source } , id: :other_metrics_source )
174+ GoogleApi.BigQuery.V2.Api.Tabledata
175+ |> stub ( :bigquery_tabledata_insert_all , fn _conn ,
176+ _project_id ,
177+ dataset_id ,
178+ _table_name ,
179+ 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 } }
184+ end )
185+
186+ :timer . sleep ( 500 )
204187
205- :timer . sleep ( 1000 )
206188
207189 assert { :ok , _ } = Backends . ingest_logs ( [ % { "metadata" => % { "value" => "test" } } ] , source )
208190
209191 assert { :ok , _ } =
210- Backends . ingest_logs ( [ % { "metadata" => % { "value" => "test" } } ] , other_source )
192+ Backends . ingest_logs ( [ % { "metadata" => % { "value" => "different" } } ] , other_source )
193+ :timer . sleep ( 1500 )
211194
212- TestUtils . retry_assert ( fn ->
213- assert [ _ ] = Backends . list_recent_logs_local ( source )
214- assert [ _ ] = Backends . list_recent_logs_local ( other_source )
195+ source_id = source . id
196+ other_source_id = other_source . id
197+ metrics_source_id = metrics_source . id
198+ other_metrics_source_id = other_metrics_source . id
199+ assert_receive { :insert_all , [ % { json: % { "attributes" => _ } } | _ ] = rows } , 5_000
215200
216- assert [
217- _ | _
218- ] = events = Backends . list_recent_logs_local ( metrics_source )
201+ rows = for row <- rows , do: row . json
219202
220- assert Enum . all? (
221- events ,
222- & match? (
223- % LogEvent {
224- body: % { "attributes" => % { "my_label" => "test" , "source_id" => ^ source_id } }
225- } ,
226- & 1
227- )
228- )
203+ assert Enum . all? ( rows , & match? ( % { "attributes" => [ % { "my_label" => "different" } ] } , & 1 ) )
204+ for row <- rows , attr <- row [ "attributes" ] do
205+ assert attr [ "source_id" ] in [ other_source_id , other_metrics_source_id ]
206+ refute attr [ "source_id" ] in [ source_id , metrics_source_id ]
207+ refute attr [ "my_label" ] == "test"
208+ end
229209
230- assert [
231- _ | _
232- ] = events = Backends . list_recent_logs_local ( other_metrics_source )
233-
234- assert Enum . all? (
235- events ,
236- & match? (
237- % LogEvent {
238- body: % {
239- "attributes" => % { "my_label" => "test" , "source_id" => ^ other_source_id }
240- }
241- } ,
242- & 1
243- )
244- )
245- end )
246210 end
247211 end
248212end
0 commit comments