Skip to content

Commit 98f8bf5

Browse files
authored
adding front end exporter custom url (#512)
* adding front end exporter custom url * adding front end exporter custom url * adding front end exporter custom url * adding front end exporter custom url * adding front end exporter custom url * updating CHANGELOG
1 parent 7a499f8 commit 98f8bf5

File tree

10 files changed

+51
-18
lines changed

10 files changed

+51
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,5 @@ significant modifications will be credited to OpenTelemetry Authors.
126126
([#439](https://github.com/open-telemetry/opentelemetry-demo/pull/439))
127127
* Add Envoy as reverse proxy for all user-facing services
128128
([#508](https://github.com/open-telemetry/opentelemetry-demo/pull/508))
129+
* Added frontend instrumentation exporter custom url
130+
([#512](https://github.com/open-telemetry/opentelemetry-demo/pull/512))

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ services:
197197
- OTEL_EXPORTER_OTLP_ENDPOINT
198198
- ENV_PLATFORM
199199
- OTEL_SERVICE_NAME=frontend
200-
- PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4318/v1/traces
200+
- PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
201201
depends_on:
202202
- adservice
203203
- cartservice

src/frontend/pages/cart/CartDetail.tsx renamed to src/frontend/components/Cart/CartDetail.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useRouter } from 'next/router';
22
import { useCallback } from 'react';
3-
import CartItems from '../../components/CartItems';
4-
import CheckoutForm from '../../components/CheckoutForm';
5-
import { IFormData } from '../../components/CheckoutForm/CheckoutForm';
3+
import CartItems from '../CartItems';
4+
import CheckoutForm from '../CheckoutForm';
5+
import { IFormData } from '../CheckoutForm/CheckoutForm';
66
import SessionGateway from '../../gateways/Session.gateway';
77
import { useCart } from '../../providers/Cart.provider';
88
import { useCurrency } from '../../providers/Currency.provider';

src/frontend/pages/cart/EmptyCart.tsx renamed to src/frontend/components/Cart/EmptyCart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Link from 'next/link';
2-
import Button from '../../components/Button';
2+
import Button from '../Button';
33
import * as S from '../../styles/Cart.styled';
44

55
const EmptyCart = () => {

src/frontend/components/PlatformFlag/PlatformFlag.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as S from './PlatformFlag.styled';
22

3-
const platform = (process.env.NEXT_PUBLIC_ANALYTICS_ID || 'local') as S.Platform;
3+
const { NEXT_PUBLIC_PLATFORM = 'local' } = typeof window !== 'undefined' ? window.ENV : {};
4+
5+
const platform = NEXT_PUBLIC_PLATFORM as S.Platform;
46

57
const PlatformFlag = () => {
68
return (

src/frontend/components/ProductPrice/ProductPrice.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ interface IProps {
88
price: Money;
99
}
1010

11-
const ProductPrice = ({ price: { units, currencyCode, nanos }, price }: IProps) => {
11+
const ProductPrice = ({ price: { units, currencyCode, nanos } }: IProps) => {
1212
const { selectedCurrency } = useCurrency();
1313

14-
console.log('@@@price', price);
15-
1614
const currencySymbol = useMemo(
1715
() => getSymbolFromCurrency(currencyCode) || selectedCurrency,
1816
[currencyCode, selectedCurrency]

src/frontend/pages/_app.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import '../styles/globals.css';
22
import { QueryClient, QueryClientProvider } from 'react-query';
3-
import type { AppProps } from 'next/app';
3+
import App, { AppContext, AppProps } from 'next/app';
4+
import { getCookie } from 'cookies-next';
45
import CurrencyProvider from '../providers/Currency.provider';
56
import CartProvider from '../providers/Cart.provider';
67
import { ThemeProvider } from 'styled-components';
78
import Theme from '../styles/Theme';
89
import FrontendTracer from '../utils/telemetry/FrontendTracer';
9-
import { getCookie } from 'cookies-next';
10+
11+
declare global {
12+
interface Window {
13+
ENV: {
14+
NEXT_PUBLIC_PLATFORM?: string;
15+
NEXT_PUBLIC_OTEL_SERVICE_NAME?: string;
16+
NEXT_PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT?: string;
17+
};
18+
}
19+
}
1020

1121
if (typeof window !== 'undefined') {
1222
const collector = getCookie('otelCollectorUrl')?.toString() || '';
@@ -15,7 +25,7 @@ if (typeof window !== 'undefined') {
1525

1626
const queryClient = new QueryClient();
1727

18-
function App({ Component, pageProps }: AppProps) {
28+
function MyApp({ Component, pageProps }: AppProps) {
1929
return (
2030
<ThemeProvider theme={Theme}>
2131
<QueryClientProvider client={queryClient}>
@@ -29,4 +39,10 @@ function App({ Component, pageProps }: AppProps) {
2939
);
3040
}
3141

32-
export default App;
42+
MyApp.getInitialProps = async (appContext: AppContext) => {
43+
const appProps = await App.getInitialProps(appContext);
44+
45+
return { ...appProps };
46+
};
47+
48+
export default MyApp;

src/frontend/pages/_document.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import Document, { DocumentContext, Html, Head, Main, NextScript } from 'next/document';
22
import { ServerStyleSheet } from 'styled-components';
33

4-
export default class MyDocument extends Document {
4+
const { ENV_PLATFORM, OTEL_SERVICE_NAME, PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT } = process.env;
5+
6+
const envString = `
7+
window.ENV = {
8+
NEXT_PUBLIC_PLATFORM: '${ENV_PLATFORM}',
9+
NEXT_PUBLIC_OTEL_SERVICE_NAME: '${OTEL_SERVICE_NAME}',
10+
NEXT_PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: '${PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT}',
11+
};
12+
`;
13+
14+
export default class MyDocument extends Document<{ envString: string }> {
515
static async getInitialProps(ctx: DocumentContext) {
616
const sheet = new ServerStyleSheet();
717
const originalRenderPage = ctx.renderPage;
@@ -16,6 +26,7 @@ export default class MyDocument extends Document {
1626
return {
1727
...initialProps,
1828
styles: [initialProps.styles, sheet.getStyleElement()],
29+
envString,
1930
};
2031
} finally {
2132
sheet.seal();
@@ -35,6 +46,7 @@ export default class MyDocument extends Document {
3546
</Head>
3647
<body>
3748
<Main />
49+
<script dangerouslySetInnerHTML={{ __html: this.props.envString }}></script>
3850
<NextScript />
3951
</body>
4052
</Html>

src/frontend/pages/cart/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import Footer from '../../components/Footer';
33
import Layout from '../../components/Layout';
44
import Recommendations from '../../components/Recommendations';
55
import * as S from '../../styles/Cart.styled';
6-
import CartDetail from './CartDetail';
7-
import EmptyCart from './EmptyCart';
6+
import CartDetail from '../../components/Cart/CartDetail';
7+
import EmptyCart from '../../components/Cart/EmptyCart';
88
import { useCart } from '../../providers/Cart.provider';
99
import AdProvider from '../../providers/Ad.provider';
1010

src/frontend/utils/telemetry/FrontendTracer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ import { Resource } from '@opentelemetry/resources';
77
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
88
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
99

10+
const { NEXT_PUBLIC_OTEL_SERVICE_NAME = '', NEXT_PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = '' } =
11+
typeof window !== 'undefined' ? window.ENV : {};
12+
1013
const FrontendTracer = async (collectorString: string) => {
1114
const { ZoneContextManager } = await import('@opentelemetry/context-zone');
1215

1316
const provider = new WebTracerProvider({
1417
resource: new Resource({
15-
[SemanticResourceAttributes.SERVICE_NAME]: process.env.NEXT_PUBLIC_OTEL_SERVICE_NAME,
18+
[SemanticResourceAttributes.SERVICE_NAME]: NEXT_PUBLIC_OTEL_SERVICE_NAME,
1619
}),
1720
});
1821

1922
provider.addSpanProcessor(
2023
new SimpleSpanProcessor(
2124
new OTLPTraceExporter({
22-
url: collectorString || 'http://localhost:4318/v1/traces',
25+
url: NEXT_PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT || collectorString || 'http://localhost:4318/v1/traces',
2326
})
2427
)
2528
);

0 commit comments

Comments
 (0)