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

Add browser traffic in loadgenerator and export traces in frontend #1345

Merged
merged 14 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ LOCUST_HOST=http://${FRONTEND_PROXY_ADDR}
LOCUST_WEB_HOST=loadgenerator
LOCUST_AUTOSTART=true
LOCUST_HEADLESS=false
LOCUST_BROWSER_TRAFFIC_ENABLED=false

# Payment Service
PAYMENT_SERVICE_PORT=50051
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ release.
([#1335](https://github.com/open-telemetry/opentelemetry-demo/pull/1335))
* [ffspostgres] define and use demo specific postgres image
([#1338](https://github.com/open-telemetry/opentelemetry-demo/pull/1338))
* [loadgenerator, frontend] enable browser traffic in loadgenerator using playwright
([#1345](https://github.com/open-telemetry/opentelemetry-demo/pull/1345))

## 1.7.2

Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ services:
- PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
- WEB_OTEL_SERVICE_NAME=frontend-web
- OTEL_COLLECTOR_HOST
depends_on:
adservice:
condition: service_started
Expand Down Expand Up @@ -371,7 +372,7 @@ services:
deploy:
resources:
limits:
memory: 120M
memory: 1G
restart: unless-stopped
ports:
- "${LOCUST_WEB_PORT}"
Expand All @@ -381,6 +382,7 @@ services:
- LOCUST_HOST
- LOCUST_HEADLESS
- LOCUST_AUTOSTART
- LOCUST_BROWSER_TRAFFIC_ENABLED
- OTEL_EXPORTER_OTLP_ENDPOINT
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
- OTEL_RESOURCE_ATTRIBUTES
Expand Down
6 changes: 5 additions & 1 deletion kubernetes/opentelemetry-demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9824,6 +9824,8 @@ spec:
value: http://localhost:8080/otlp-http/v1/traces
- name: OTEL_RESOURCE_ATTRIBUTES
value: service.name=$(OTEL_SERVICE_NAME),service.namespace=opentelemetry-demo
- name: OTEL_COLLECTOR_HOST
value: $(OTEL_COLLECTOR_NAME)
resources:
limits:
memory: 200Mi
Expand Down Expand Up @@ -10041,6 +10043,8 @@ spec:
value: "false"
- name: LOCUST_AUTOSTART
value: "true"
- name: LOCUST_BROWSER_TRAFFIC_ENABLED
value: "false"
- name: PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION
value: python
- name: OTEL_EXPORTER_OTLP_ENDPOINT
Expand All @@ -10049,7 +10053,7 @@ spec:
value: service.name=$(OTEL_SERVICE_NAME),service.namespace=opentelemetry-demo
resources:
limits:
memory: 120Mi
memory: 1Gi
---
# Source: opentelemetry-demo/templates/component.yaml
apiVersion: apps/v1
Expand Down
24 changes: 15 additions & 9 deletions src/frontend/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@

import Document, { DocumentContext, Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheet } from 'styled-components';
import {context, propagation} from "@opentelemetry/api";

const { ENV_PLATFORM, WEB_OTEL_SERVICE_NAME, PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT } = process.env;

const envString = `
window.ENV = {
NEXT_PUBLIC_PLATFORM: '${ENV_PLATFORM}',
NEXT_PUBLIC_OTEL_SERVICE_NAME: '${WEB_OTEL_SERVICE_NAME}',
NEXT_PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: '${PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT}',
};
`;
const { ENV_PLATFORM, WEB_OTEL_SERVICE_NAME, PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTEL_COLLECTOR_HOST} = process.env;

export default class MyDocument extends Document<{ envString: string }> {
static async getInitialProps(ctx: DocumentContext) {
Expand All @@ -26,6 +19,19 @@ export default class MyDocument extends Document<{ envString: string }> {
});

const initialProps = await Document.getInitialProps(ctx);
const baggage = propagation.getBaggage(context.active());
const isSyntheticRequest = baggage?.getEntry('synthetic_request')?.value === 'true';

const otlpTracesEndpoint = isSyntheticRequest
? `http://${OTEL_COLLECTOR_HOST}:4318/v1/traces`
: PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT;

const envString = `
window.ENV = {
NEXT_PUBLIC_PLATFORM: '${ENV_PLATFORM}',
NEXT_PUBLIC_OTEL_SERVICE_NAME: '${WEB_OTEL_SERVICE_NAME}',
NEXT_PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: '${otlpTracesEndpoint}',
};`;
return {
...initialProps,
styles: [initialProps.styles, sheet.getStyleElement()],
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/utils/telemetry/FrontendTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ const FrontendTracer = async (collectorString: string) => {
new BatchSpanProcessor(
new OTLPTraceExporter({
url: NEXT_PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT || collectorString || 'http://localhost:4318/v1/traces',
})
}), {
scheduledDelayMillis : 500
}
)
);

Expand Down
2 changes: 2 additions & 0 deletions src/loadgenerator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ WORKDIR /usr/src/app/
COPY --from=builder /reqs /usr/local
COPY ./src/loadgenerator/locustfile.py .
COPY ./src/loadgenerator/people.json .
ENV LOCUST_PLAYWRIGHT=1
RUN playwright install --with-deps chromium
ENTRYPOINT locust
42 changes: 42 additions & 0 deletions src/loadgenerator/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@


import json
import os
import random
import uuid
from locust import HttpUser, task, between
from locust_plugins.users.playwright import PlaywrightUser, pw, PageWithRetry, event

from opentelemetry import context, baggage, trace
from opentelemetry.metrics import set_meter_provider
Expand All @@ -21,6 +23,7 @@
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.instrumentation.system_metrics import SystemMetricsInstrumentor
from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor
from playwright.async_api import Route, Request

exporter = OTLPMetricExporter(insecure=True)
set_meter_provider(MeterProvider([PeriodicExportingMetricReader(exporter)]))
Expand Down Expand Up @@ -130,3 +133,42 @@ def on_start(self):
context.attach(ctx)
self.index()


browser_traffic_enabled = os.environ.get('LOCUST_BROWSER_TRAFFIC_ENABLED', False)

if browser_traffic_enabled:
class WebsiteBrowserUser(PlaywrightUser):
headless = True # to use a headless browser, without a GUI

@task
@pw
async def open_cart_page_and_change_currency(self, page: PageWithRetry):
try:
page.on("console", lambda msg: print(msg.text))
await page.route('**/*', add_baggage_header)
await page.goto("/cart", wait_until="domcontentloaded")
await page.select_option('[name="currency_code"]', 'CHF')
await page.wait_for_timeout(2000) # giving the browser time to export the traces
except:
pass

@task
@pw
async def add_product_to_cart(self, page: PageWithRetry):
try:
page.on("console", lambda msg: print(msg.text))
await page.route('**/*', add_baggage_header)
await page.goto("/", wait_until="domcontentloaded")
await page.click('p:has-text("Roof Binoculars")', wait_until="domcontentloaded")
await page.click('button:has-text("Add To Cart")', wait_until="domcontentloaded")
await page.wait_for_timeout(2000) # giving the browser time to export the traces
except:
pass


async def add_baggage_header(route: Route, request: Request):
headers = {
**request.headers,
'baggage': 'synthetic_request=true'
}
await route.continue_(headers=headers)
1 change: 1 addition & 0 deletions src/loadgenerator/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ idna==3.4
itsdangerous==2.1.2
jinja2==3.1.2
locust==2.18.2
locust_plugins==3.4.0
markupsafe==2.1.3
msgpack==1.0.7
opentelemetry-api==1.21.0
Expand Down
Loading