Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend] [ops golf?] Pass collector URL to frontend via reverse proxy. #508

Merged
merged 18 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=cumulative
FRONTEND_PORT=8080
FRONTEND_ADDR=frontend:${FRONTEND_PORT}

# Envoy
FRONTEND_HOST=frontend
ENVOY_PORT=80

# Redis
REDIS_PORT=6379
REDIS_ADDR=redis-cart:${REDIS_PORT}
Expand Down
25 changes: 22 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ services:
memory: 200M
restart: always
ports:
- "${FRONTEND_PORT}:${FRONTEND_PORT}"
- "${FRONTEND_PORT}"
environment:
- PORT=${FRONTEND_PORT}
- FRONTEND_ADDR
Expand Down Expand Up @@ -210,7 +210,26 @@ services:
- shippingservice
logging: *logging

# Load generator
frontendEnvoy:
austinlparker marked this conversation as resolved.
Show resolved Hide resolved
image: ${IMAGE_NAME}:${IMAGE_VERSION}-frontend-envoy
build:
context: ./
dockerfile: ./src/frontendProxy/Dockerfile
container_name: frontend-envoy
volumes:
- ./src/frontendProxy/envoy.yaml.tmpl:/etc/envoy/envoy.yaml.tmpl
ports:
- "${ENVOY_PORT}:${ENVOY_PORT}"
environment:
- PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
- FRONTEND_PORT
- FRONTEND_HOST
- ENVOY_PORT
- ENVOY_UID=0
depends_on:
- frontend
command: /bin/sh -c "envsubst < /etc/envoy/envoy.yaml.tmpl > /etc/envoy/envoy.yaml && envoy -c /etc/envoy/envoy.yaml;"

loadgenerator:
image: ${IMAGE_NAME}:${IMAGE_VERSION}-loadgenerator
container_name: load-generator
Expand Down Expand Up @@ -389,7 +408,7 @@ services:
memory: 160M
restart: always
ports:
- "${FEATURE_FLAG_SERVICE_PORT}:${FEATURE_FLAG_SERVICE_PORT}" # Feature Flag Service UI
- "${FEATURE_FLAG_SERVICE_PORT}" # Feature Flag Service UI
- "${FEATURE_FLAG_GRPC_SERVICE_PORT}" # Feature Flag Service gRPC API
environment:
- FEATURE_FLAG_SERVICE_PORT
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ from the root folder.
It will start all of the required backend services
and within the container simply run `npm run dev`.
After that the app should be available at <http://localhost:8080/>.

## Collector Config

The app looks for a cookie named 'otelCollectorUrl' and gets its value on page
load. This cookie key + value needs to be set by a reverse proxy.
56 changes: 56 additions & 0 deletions src/frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@opentelemetry/sdk-trace-node": "1.7.0",
"@opentelemetry/sdk-trace-web": "1.7.0",
"@types/styled-components": "5.1.25",
"cookies-next": "^2.1.1",
"currency-symbol-map": "5.1.0",
"dotenv": "16.0.1",
"dotenv-expand": "8.0.3",
Expand Down
6 changes: 5 additions & 1 deletion src/frontend/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import CartProvider from '../providers/Cart.provider';
import { ThemeProvider } from 'styled-components';
import Theme from '../styles/Theme';
import FrontendTracer from '../utils/telemetry/FrontendTracer';
import { getCookie } from 'cookies-next';

if (typeof window !== 'undefined') FrontendTracer();
if (typeof window !== 'undefined') {
const collector = getCookie('otelCollectorUrl')?.toString() || '';
FrontendTracer(collector);
}

const queryClient = new QueryClient();

Expand Down
4 changes: 2 additions & 2 deletions src/frontend/utils/telemetry/FrontendTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';

const FrontendTracer = async () => {
const FrontendTracer = async (collectorString: string) => {
const { ZoneContextManager } = await import('@opentelemetry/context-zone');

const provider = new WebTracerProvider({
Expand All @@ -19,7 +19,7 @@ const FrontendTracer = async () => {
provider.addSpanProcessor(
new SimpleSpanProcessor(
new OTLPTraceExporter({
url: process.env.NEXT_PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT || 'http://localhost:4318/v1/traces',
url: collectorString || 'http://localhost:4318/v1/traces',
})
)
);
Expand Down
2 changes: 2 additions & 0 deletions src/frontendProxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM envoyproxy/envoy-dev:8c202194ac6a2cb781eb6ce27d924b379b1e787f
RUN apt-get update && apt-get install -y gettext-base && apt-get clean && rm -rf /var/lib/apt/lists/*
62 changes: 62 additions & 0 deletions src/frontendProxy/envoy.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
static_resources:
listeners:
- address:
socket_address:
address: 0.0.0.0
port_value: ${ENVOY_PORT}
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
codec_type: AUTO
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: frontend
domains:
- "*"
routes:
- match:
prefix: "/"
route:
cluster: frontend
response_headers_to_add:
- header:
key: "Set-Cookie"
value:
"otelCollectorUrl=${PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT};"
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

clusters:
- name: frontend
type: STRICT_DNS
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: frontend
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: ${FRONTEND_HOST}
port_value: ${FRONTEND_PORT}

admin:
address:
socket_address:
address: 0.0.0.0
port_value: 10000
layered_runtime:
layers:
- name: static_layer_0
static_layer:
envoy:
resource_limits:
listener:
example_listener_name:
connection_limit: 10000
13 changes: 13 additions & 0 deletions src/frontendProxy/frontend.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
upstream frontend {
server frontend:${FRONTEND_PORT};
}

server {
listen ${PROXY_PORT};
server_name _;

location / {
proxy_pass http://frontend;
add_header Set-Cookie otelCollectorUrl=${PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT};
}
}