Skip to content

Commit

Permalink
[frontend] Slowloading of images (#1515)
Browse files Browse the repository at this point in the history
* [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 <[email protected]>

---------

Signed-off-by: Michael Beemer <[email protected]>
Co-authored-by: Michael Beemer <[email protected]>
  • Loading branch information
klucsik and beeme1mr authored May 13, 2024
1 parent 3c665d3 commit f909932
Show file tree
Hide file tree
Showing 9 changed files with 2,412 additions and 773 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ the release.
([#1553](https://github.com/open-telemetry/opentelemetry-demo/pull/1553))
* [loadgenerator] Fix feature flag hooks setter method
([#1556](https://github.com/open-telemetry/opentelemetry-demo/pull/1556))
* [frontend] Slowloading of images based on imageSlowLoad flag
([#1515](https://github.com/open-telemetry/opentelemetry-demo/pull/1486))

## 1.9.0

Expand Down
2 changes: 2 additions & 0 deletions docker-compose.minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ services:
- PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
- WEB_OTEL_SERVICE_NAME=frontend-web
- FLAGD_HOST
- FLAGD_PORT
depends_on:
adservice:
condition: service_started
Expand Down
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ services:
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
- WEB_OTEL_SERVICE_NAME=frontend-web
- OTEL_COLLECTOR_HOST
- FLAGD_HOST
- FLAGD_PORT
depends_on:
adservice:
condition: service_started
Expand All @@ -300,6 +302,8 @@ services:
condition: service_started
imageprovider:
condition: service_started
flagd:
condition: service_started
logging: *logging

# Frontend Proxy (Envoy)
Expand Down Expand Up @@ -333,6 +337,8 @@ services:
- OTEL_COLLECTOR_PORT_HTTP
- OTEL_RESOURCE_ATTRIBUTES
- ENVOY_PORT
- FLAGD_HOST
- FLAGD_PORT
depends_on:
frontend:
condition: service_started
Expand Down
10 changes: 10 additions & 0 deletions src/flagd/demo.flagd.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@
"off": 0
},
"defaultVariant": "off"
},
"imageSlowLoad": {
"description": "slow loading images in the frontend",
"state": "ENABLED",
"variants": {
"10sec": 10000,
"5sec": 5000,
"off": 0
},
"defaultVariant": "off"
}
}
}
20 changes: 19 additions & 1 deletion src/frontend/components/ProductCard/ProductCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ import { CypressFields } from '../../utils/Cypress';
import { Product } from '../../protos/demo';
import ProductPrice from '../ProductPrice';
import * as S from './ProductCard.styled';
import { useState, useEffect } from 'react';
import { RequestInfo } from 'undici-types';
import { useNumberFlagValue } from '@openfeature/react-sdk';

interface IProps {
product: Product;
}

async function getImageWithHeaders(url: RequestInfo, headers: Record<string, string>) {
const res = await fetch(url, { headers });
return await res.blob();
}

const ProductCard = ({
product: {
id,
Expand All @@ -22,10 +30,20 @@ const ProductCard = ({
},
},
}: IProps) => {
const imageSlowLoad = useNumberFlagValue('imageSlowLoad', 0);
const [imageSrc, setImageSrc] = useState<string>('');

useEffect(() => {
const headers = { 'x-envoy-fault-delay-request': imageSlowLoad.toString(), 'Cache-Control': 'no-cache' };
getImageWithHeaders('/images/products/' + picture, headers).then(blob => {
setImageSrc(URL.createObjectURL(blob));
});
}, [picture, imageSlowLoad]);

return (
<S.Link href={`/product/${id}`}>
<S.ProductCard data-cy={CypressFields.ProductCard}>
<S.Image $src={"/images/products/" + picture} />
<S.Image $src={imageSrc} />
<div>
<S.ProductName>{name}</S.ProductName>
<S.ProductPrice>
Expand Down
Loading

0 comments on commit f909932

Please sign in to comment.