Skip to content

Commit f909932

Browse files
klucsikbeeme1mr
andauthored
[frontend] Slowloading of images (#1515)
* [frontend] Add FeatureFlag Provider to client side, add http fault filter to envoy, add slowloading featureflag * refactor web sdk usage Signed-off-by: Michael Beemer <beeme1mr@users.noreply.github.com> --------- Signed-off-by: Michael Beemer <beeme1mr@users.noreply.github.com> Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
1 parent 3c665d3 commit f909932

File tree

9 files changed

+2412
-773
lines changed

9 files changed

+2412
-773
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ the release.
3535
([#1553](https://github.com/open-telemetry/opentelemetry-demo/pull/1553))
3636
* [loadgenerator] Fix feature flag hooks setter method
3737
([#1556](https://github.com/open-telemetry/opentelemetry-demo/pull/1556))
38+
* [frontend] Slowloading of images based on imageSlowLoad flag
39+
([#1515](https://github.com/open-telemetry/opentelemetry-demo/pull/1486))
3840

3941
## 1.9.0
4042

docker-compose.minimal.yml

+2
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ services:
209209
- PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
210210
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
211211
- WEB_OTEL_SERVICE_NAME=frontend-web
212+
- FLAGD_HOST
213+
- FLAGD_PORT
212214
depends_on:
213215
adservice:
214216
condition: service_started

docker-compose.yml

+6
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ services:
279279
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
280280
- WEB_OTEL_SERVICE_NAME=frontend-web
281281
- OTEL_COLLECTOR_HOST
282+
- FLAGD_HOST
283+
- FLAGD_PORT
282284
depends_on:
283285
adservice:
284286
condition: service_started
@@ -300,6 +302,8 @@ services:
300302
condition: service_started
301303
imageprovider:
302304
condition: service_started
305+
flagd:
306+
condition: service_started
303307
logging: *logging
304308

305309
# Frontend Proxy (Envoy)
@@ -333,6 +337,8 @@ services:
333337
- OTEL_COLLECTOR_PORT_HTTP
334338
- OTEL_RESOURCE_ATTRIBUTES
335339
- ENVOY_PORT
340+
- FLAGD_HOST
341+
- FLAGD_PORT
336342
depends_on:
337343
frontend:
338344
condition: service_started

src/flagd/demo.flagd.json

+10
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@
9696
"off": 0
9797
},
9898
"defaultVariant": "off"
99+
},
100+
"imageSlowLoad": {
101+
"description": "slow loading images in the frontend",
102+
"state": "ENABLED",
103+
"variants": {
104+
"10sec": 10000,
105+
"5sec": 5000,
106+
"off": 0
107+
},
108+
"defaultVariant": "off"
99109
}
100110
}
101111
}

src/frontend/components/ProductCard/ProductCard.tsx

+19-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ import { CypressFields } from '../../utils/Cypress';
55
import { Product } from '../../protos/demo';
66
import ProductPrice from '../ProductPrice';
77
import * as S from './ProductCard.styled';
8+
import { useState, useEffect } from 'react';
9+
import { RequestInfo } from 'undici-types';
10+
import { useNumberFlagValue } from '@openfeature/react-sdk';
811

912
interface IProps {
1013
product: Product;
1114
}
1215

16+
async function getImageWithHeaders(url: RequestInfo, headers: Record<string, string>) {
17+
const res = await fetch(url, { headers });
18+
return await res.blob();
19+
}
20+
1321
const ProductCard = ({
1422
product: {
1523
id,
@@ -22,10 +30,20 @@ const ProductCard = ({
2230
},
2331
},
2432
}: IProps) => {
33+
const imageSlowLoad = useNumberFlagValue('imageSlowLoad', 0);
34+
const [imageSrc, setImageSrc] = useState<string>('');
35+
36+
useEffect(() => {
37+
const headers = { 'x-envoy-fault-delay-request': imageSlowLoad.toString(), 'Cache-Control': 'no-cache' };
38+
getImageWithHeaders('/images/products/' + picture, headers).then(blob => {
39+
setImageSrc(URL.createObjectURL(blob));
40+
});
41+
}, [picture, imageSlowLoad]);
42+
2543
return (
2644
<S.Link href={`/product/${id}`}>
2745
<S.ProductCard data-cy={CypressFields.ProductCard}>
28-
<S.Image $src={"/images/products/" + picture} />
46+
<S.Image $src={imageSrc} />
2947
<div>
3048
<S.ProductName>{name}</S.ProductName>
3149
<S.ProductPrice>

0 commit comments

Comments
 (0)