Skip to content

Commit fb8bd05

Browse files
committed
Squashed update metrics with span attributes
1 parent 14520b0 commit fb8bd05

File tree

6 files changed

+96
-61
lines changed

6 files changed

+96
-61
lines changed

env-config/example.env

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
SENTRY_ORG=
22

3+
NEXT_PUBLIC_DSN=
4+
NEXT_PUBLIC_ENVIRONMENT=
5+
NEXT_RELEASE_PACKAGE_NAME=
6+
NEXT_SENTRY_PROJECT=
7+
NEXT_SOURCEMAPS_DIR=build/static/js
8+
NEXT_SOURCEMAPS_URL_PREFIX=~/static/js
9+
NEXT_PUBLIC_FLASK_BACKEND=
10+
NEXT_PUBLIC_EXPRESS_BACKEND=
11+
NEXT_PUBLIC_SPRINGBOOT_BACKEND=
12+
NEXT_PUBLIC_ASPNETCORE_BACKEND=
13+
NEXT_PUBLIC_LARAVEL_BACKEND=
14+
NEXT_PUBLIC_RUBY_BACKEND=
15+
NEXT_PUBLIC_RUBYONRAILS_BACKEND=
16+
NEXT_LOCAL_PORT=3000 # only needed in local.env
17+
318
REACT_APP_DSN=
419
REACT_APP_ENGINE_SERVICE=
520
REACT_APP_ENVIRONMENT=
@@ -78,4 +93,4 @@ CRONSPYTHON_APP_DSN=
7893
CRONSPYTHON_MONITOR_SLUG=
7994
CRONSPYTHON_DEPLOY_HOST=
8095
CRONSPYTHON_DEPLOY_DIR=
81-
CRONSPYTHON_CRONTAB_USER= # if change this must clear crontab for previus user or will have 2 running simult.
96+
CRONSPYTHON_CRONTAB_USER= # if change this must clear crontab for previus user or will have 2 running simult.

react/config-overrides.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const SentryWebpackPlugin = require('@sentry/webpack-plugin');
2+
const reactsourceMapPlugin = require('@acemarke/react-prod-sourcemaps');
3+
4+
module.exports = function override(config, env) {
5+
//do stuff with the webpack config...
6+
config.plugins.push(
7+
reactsourceMapPlugin.WebpackReactSourcemapsPlugin({
8+
mode: 'strict',
9+
})
10+
);
11+
12+
config.plugins.push(
13+
SentryWebpackPlugin.sentryWebpackPlugin({
14+
authToken: process.env.SENTRY_AUTH_TOKEN,
15+
include: '.',
16+
org: 'demo',
17+
project: 'javascript-react',
18+
ignoreFile: '.sentrycliignore',
19+
ignore: ['webpack.config.js'],
20+
configFile: 'sentry.properties',
21+
reactComponentAnnotation: {enabled:true},
22+
})
23+
);
24+
return config;
25+
};

react/src/components/Cart.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ import * as Sentry from '@sentry/react';
44
import Button from './ButtonLink';
55
import { connect } from 'react-redux';
66
import { setProducts, addProduct, removeProduct } from '../actions';
7+
import countItemsInCart from '../utils/cart';
78

89
function Cart({ cart, removeProduct, addProduct }) {
10+
console.log("Cart component rendered with cart:", cart);
11+
const itemsInCart = countItemsInCart(cart);
12+
console.log("Items in cart calculated in Cart component:", itemsInCart);
13+
const span = Sentry.startInactiveSpan({ name: "items_added_to_cart", op: "function"});
14+
span.setAttribute("items_in_cart", itemsInCart);
15+
span.end();
916
return (
1017
<div className="cart-container">
1118
<h2 className="sentry-unmask">Cart</h2>

react/src/components/Checkout.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import './checkout.css';
55
import * as Sentry from '@sentry/react';
66
import { connect } from 'react-redux';
77
import Loader from 'react-loader-spinner';
8+
import countItemsInCart from '../utils/cart';
89

910
function Checkout({ backend, rageclick, cart }) {
1011
const navigate = useNavigate();
@@ -40,7 +41,12 @@ function Checkout({ backend, rageclick, cart }) {
4041
const [form, setForm] = useState(initialFormValues);
4142

4243
async function checkout(cart, checkout_span) {
43-
checkout_span.setAttribute("checkout.click.span", 1);
44+
console.log("Checkout called with cart:", cart);
45+
console.log("Cart quantities:", cart.quantities);
46+
const itemsInCart = countItemsInCart(cart);
47+
console.log("Calculated itemsInCart:", itemsInCart);
48+
checkout_span.setAttribute("checkout.click", 1);
49+
checkout_span.setAttribute("items_at_checkout", itemsInCart);
4450
const stopMeasurement = measureRequestDuration('/checkout');
4551
const response = await fetch(backend + '/checkout?v2=true', {
4652
method: 'POST',

react/src/components/Products.jsx

Lines changed: 20 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ function Products({ frontendSlowdown, backend }) {
4040

4141
// intentionally supposed to be slow
4242
function renderProducts(data) {
43-
console.log(Sentry.getActiveSpan())
4443
try {
4544
// Trigger a Sentry 'Performance Issue' in the case of
4645
// a frontend slowdown
@@ -80,7 +79,7 @@ function Products({ frontendSlowdown, backend }) {
8079
related to the async keyword + babel transform, hence why it probably got
8180
fixed with hooks (no transform on that class method anymore)"
8281
*/
83-
function getProducts(frontendSlowdown) {
82+
async function getProducts(frontendSlowdown) {
8483
[('/api', '/connect', '/organization')].forEach((endpoint, activeSpan) => {
8584
fetch(backend + endpoint, {
8685
method: 'GET',
@@ -91,70 +90,32 @@ function Products({ frontendSlowdown, backend }) {
9190
});
9291
});
9392
const productsEndpoint = determineProductsEndpoint();
94-
const stopMeasurement = measureRequestDuration(productsEndpoint);
95-
fetch(backend + productsEndpoint, {
96-
method: 'GET',
97-
headers: { 'Content-Type': 'application/json' },
98-
})
99-
.then((result) => {
100-
if (!result.ok) {
93+
Sentry.startSpan({ name: "Fetch Products"}, async (span) => {
94+
const stopMeasurement = measureRequestDuration(productsEndpoint, span);
95+
const response = await fetch(backend + productsEndpoint, {
96+
method: 'GET',
97+
headers: { 'Content-Type': 'application/json' },
98+
});
99+
const data = await response.json();
100+
101+
if (!response.ok) {
101102
Sentry.setContext('err', {
102-
status: result.status,
103-
statusText: result.statusText,
103+
status: response.status,
104+
statusText: response.statusText,
104105
});
105-
return Promise.reject();
106-
} else {
107-
return result.json();
106+
return;
108107
}
108+
renderProducts(data);
109+
stopMeasurement();
109110
})
110-
.then(renderProducts)
111-
.catch((err) => {
112-
return { ok: false, status: 500 };
113-
}).then((res) => {
114-
stopMeasurement()
115-
return res
116-
});
117-
118-
/*
119-
120-
// When triggering a frontend-only slowdown, use the products-join endpoint
121-
// because it returns product data with a fast backend response.
122-
// Otherwise use the /products endpoint, which provides a slow backend response.
123-
Sentry.withActiveSpan(null, () => {
124-
Sentry.startSpan({ name: "Products" }, () => {
125-
const productsEndpoint = determineProductsEndpoint();
126-
const stopMeasurement = measureRequestDuration(productsEndpoint);
127-
fetch(backend + productsEndpoint, {
128-
method: 'GET',
129-
headers: { 'Content-Type': 'application/json' },
130-
})
131-
.then((result) => {
132-
if (!result.ok) {
133-
Sentry.setContext('err', {
134-
status: result.status,
135-
statusText: result.statusText,
136-
});
137-
return Promise.reject();
138-
} else {
139-
return result.json();
140-
}
141-
})
142-
.then(renderProducts)
143-
.catch((err) => {
144-
return { ok: false, status: 500 };
145-
}).then((res) => {
146-
stopMeasurement()
147-
return res
148-
});
149-
})
150-
})*/
151-
152111
}
153112

154113
useEffect(() => {
155-
Sentry.startSpan({ name: "Fetch Products"}, () => {
156-
getProducts(frontendSlowdown);
157-
})
114+
try {
115+
getProducts(frontendSlowdown)
116+
} catch (error) {
117+
Sentry.captureException(error)
118+
}
158119
}, []);
159120

160121
return products.length > 0 ? (

react/src/utils/cart.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export default function countItemsInCart(cart) {
2+
console.log("countItemsInCart called with:", cart);
3+
let totalItems = 0;
4+
5+
if (!cart || !cart.quantities) {
6+
console.log("Cart or cart.quantities is undefined!");
7+
return totalItems;
8+
}
9+
10+
console.log("Cart quantities object:", cart.quantities);
11+
console.log("Object.values(cart.quantities):", Object.values(cart.quantities));
12+
13+
totalItems = Object.values(cart.quantities)
14+
.reduce((sum, quantity) => {
15+
console.log("Adding quantity:", quantity);
16+
return sum + quantity;
17+
}, 0);
18+
19+
console.log("Final total:", totalItems);
20+
return totalItems;
21+
}

0 commit comments

Comments
 (0)