@@ -3,18 +3,32 @@ def index
3
3
# get Sentry tags in application_controller.rb
4
4
set_Sentry_tags
5
5
6
-
7
6
transaction = Sentry . get_current_scope . get_transaction || Sentry . start_transaction ( name : "custom transaction" )
8
7
9
8
span_products_db = transaction . start_child ( op : "custom.products_db_call" )
10
- # Eager load reviews to prevent n+1 queries and ensure proper serialization
11
- products = Products . includes ( : reviews)
12
- . select ( "id, title, description, descriptionfull, price, img, imgcropped" )
9
+ sleep 0.25
10
+ products = Products . select ( "id, title, description, descriptionfull, price, img, imgcropped, Null as pg_sleep, Null as reviews" )
11
+ sleep 0.25
13
12
span_products_db . finish
14
13
15
- # Use the serializer to properly format the response
16
- render json : products , each_serializer : ProductSerializer , status : 200
17
- end
14
+ # n+1 to db if done this way
15
+ products . each do |prod_slow |
16
+ span_products_slow_db = transaction . start_child ( op : "custom.reviews_slow_db_call" )
17
+ prod_slow [ "pg_sleep" ] = ""
18
+ prod_slow [ "reviews" ] = [ ]
19
+ prod_slow [ "reviews" ] = Reviews . select ( "id, productid, rating, customerid, description, created, Null as pg_sleep" ) . where ( "productid=" +prod_slow . id . to_s )
20
+ span_products_slow_db . finish
21
+ end
22
+
23
+ # fewer db calls this way -- done in products-join
24
+ # span_reviews_db = transaction.start_child(op: "custom.reviews_db_call")
25
+ # sleep 0.25
26
+ # reviews = Reviews.select("id, productid, rating, customerid, description, created, Null as pg_sleep")
27
+ # sleep 0.25
28
+ # span_reviews_db.finish
29
+
30
+ # span_response = transaction.start_child(op: "custom.construct_response_object")
31
+ # products.each do |prod|
18
32
# prod["pg_sleep"] = ""
19
33
# reviews_arr = []
20
34
# reviews.each do |review|
@@ -26,7 +40,7 @@ def index
26
40
# end
27
41
# span_response.finish
28
42
29
- render json : products . to_a , status : 200
43
+ render json : products , each_serializer : ProductSerializer , status : 200
30
44
end
31
45
32
46
# sent here if unexpected route was enterered
0 commit comments