Skip to content

Commit

Permalink
Merge pull request #1 from monodot/add-geos-and-devices
Browse files Browse the repository at this point in the history
Add random user agent, IP address, user segment to traces and logs
  • Loading branch information
Alex3k authored Feb 20, 2023
2 parents 1e20e69 + ce5b141 commit 10c2613
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
15 changes: 14 additions & 1 deletion src/src/common/python/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,25 @@ def response_hook(span, status, response_headers):
( 'method', request.method ),
( 'path', request.path ),
( 'status', status.split(' ')[0] ),
( 'user_agent', request.headers.get('User-Agent') ),
( 'device_country', request.headers.get('X-Device-Country') ),
( 'device_id', request.headers.get('X-Device-ID') ),
( 'forwarded_for', request.headers.get('X-Forwarded-For') ),
( 'customer_tier', request.headers.get('X-Customer-Tier') )
]
})

# Add extra attributes to the span
def request_hook(span, environ):
if span and span.is_recording():
if environ.get('HTTP_X_FORWARDED_FOR') is not None:
span.set_attribute('http.request.header.x_forwarded_for', environ.get('HTTP_X_FORWARDED_FOR'))
if environ.get('HTTP_X_CUSTOMER_TIER') is not None:
span.set_attribute('enduser.tier', environ.get('HTTP_X_CUSTOMER_TIER'))

# Instantiate application
app = Flask(config.get('SERVICE_NAME'))
FlaskInstrumentor().instrument_app(app, response_hook=response_hook)
FlaskInstrumentor().instrument_app(app, request_hook=request_hook, response_hook=response_hook)

# Enable CORS
cors = CORS(app, resources={r'*': {'origins': '*'}})
Expand Down
2 changes: 1 addition & 1 deletion src/src/services/synthetics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"build": "webpack"
},
"dependencies": {
"@faker-js/faker": "^5.5.3",
"@faker-js/faker": "^6.3.0",
"creditcard-generator": "^0.0.7",
"dateformat": "^5.0.3"
},
Expand Down
25 changes: 20 additions & 5 deletions src/src/services/synthetics/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Node.js packages
import dateFormat from 'dateformat'
import * as faker from '@faker-js/faker'
import { faker } from '@faker-js/faker'
import { GenCC } from 'creditcard-generator'

// k6 packages
Expand All @@ -9,7 +9,7 @@ import { sleep } from 'k6'

// TODO: Revert default
const SERVICE_HOST_WEB_GATEWAY = __ENV.SERVICE_HOST_WEB_GATEWAY || 'web-gateway.default.svc.cluster.local'
const SERVICE_PORT_WEB_GATEWAY = __ENV.SERVICE_HOST_WEB_GATEWAY || 80
const SERVICE_PORT_WEB_GATEWAY = __ENV.SERVICE_PORT_WEB_GATEWAY || 80
const BASE_URL = `http://${SERVICE_HOST_WEB_GATEWAY}:${SERVICE_PORT_WEB_GATEWAY}`

// k6 Configuration
Expand All @@ -21,13 +21,17 @@ export const options = {
// Utils
const pause = () => sleep(1 + Math.random() * 2)
const json = (data) => JSON.stringify(data)
const jsonParams = () => {
return { headers: { 'Content-Type': 'application/json' }}
}
const randomUser = () => {
faker.setLocale('en_US')
const postalCode = faker.address.zipCode().split('-')[0]
const user = {
tier: faker.helpers.arrayElement(['standard', 'silver', 'gold']),
device: {
id: faker.datatype.uuid(),
user_agent: faker.internet.userAgent(),
ip_address: faker.internet.ip(),
country: faker.address.countryCode()
},
address: {
street_1: faker.address.streetAddress(),
street_2: Math.random() > 0.8 ? faker.address.secondaryAddress() : '',
Expand All @@ -50,6 +54,17 @@ const randomUser = () => {
export default () => {
const user = randomUser()

const jsonParams = () => {
return { headers: {
'Content-Type': 'application/json',
'User-Agent': user.device.user_agent,
'X-Customer-Tier': user.tier,
'X-Device-Id': user.device.id,
'X-Device-Country': user.device.country, // This is random, so unrelated to IP
'X-Forwarded-For': user.device.ip_address
}}
}

//// Load home ////////////////////////////////////////////////////////////

// User request
Expand Down

0 comments on commit 10c2613

Please sign in to comment.