Skip to content

Commit

Permalink
[protobuf] - encode zipcode as a string (open-telemetry#587)
Browse files Browse the repository at this point in the history
* encode zipcode as a string

* encode zipcode as a string

* encode zipcode as a string

* add zip_code attribute

* update zipCode to string type

* update zipCode to string type

* add address to shipping api call

* add address to shipping api call

* zipcode as string
  • Loading branch information
puckpuck authored Nov 21, 2022
1 parent 985b624 commit 8dff26c
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,5 @@ significant modifications will be credited to OpenTelemetry Authors.
([#536](https://github.com/open-telemetry/opentelemetry-demo/pull/536))
* Add basic metrics support for payment service
([#583](https://github.com/open-telemetry/opentelemetry-demo/pull/583))
* Change ZipCode data type from int to string
([#587](https://github.com/open-telemetry/opentelemetry-demo/pull/587))
11 changes: 6 additions & 5 deletions docs/manual_span_attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ This document contains the list of manual Span Attributes used throughout the de

## ShippingService

| Name | Type | Description |
|----------------------------|--------|----------------------|
| `app.shipping.cost.total` | number | Total shipping cost |
| `app.shipping.items.count` | number | Total items to ship |
| `app.shipping.tracking.id` | string | Shipping tracking Id |
| Name | Type | Description |
|----------------------------|--------|-------------------------------|
| `app.shipping.cost.total` | number | Total shipping cost |
| `app.shipping.items.count` | number | Total items to ship |
| `app.shipping.tracking.id` | string | Shipping tracking Id |
| `app.shipping.zip_code` | string | Zip code used to ship item(s) |
2 changes: 1 addition & 1 deletion pb/demo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ message Address {
string city = 2;
string state = 3;
string country = 4;
int32 zip_code = 5;
string zip_code = 5;
}

// -----------------Currency service-----------------
Expand Down
11 changes: 9 additions & 2 deletions src/frontend/components/CartItems/CartItems.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo } from 'react';
import { useQuery } from 'react-query';
import ApiGateway from '../../gateways/Api.gateway';
import { Money } from '../../protos/demo';
import { Address, Money } from '../../protos/demo';
import { useCurrency } from '../../providers/Currency.provider';
import { IProductCartItem } from '../../types/Cart';
import ProductPrice from '../ProductPrice';
Expand All @@ -15,8 +15,15 @@ interface IProps {

const CartItems = ({ productList, shouldShowPrice = true }: IProps) => {
const { selectedCurrency } = useCurrency();
const address: Address = {
streetAddress: '1600 Amphitheatre Parkway',
city: 'Mountain View',
state: 'CA',
country: 'United States',
zipCode: "94043",
};
const { data: shippingConst = { units: 0, currencyCode: 'USD', nanos: 0 } } = useQuery('shipping', () =>
ApiGateway.getShippingCost(productList, selectedCurrency)
ApiGateway.getShippingCost(productList, selectedCurrency, address)
);

const total = useMemo<Money>(
Expand Down
7 changes: 3 additions & 4 deletions src/frontend/components/CheckoutForm/CheckoutForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface IFormData {
city: string;
state: string;
country: string;
zipCode: number;
zipCode: string;
creditCardNumber: string;
creditCardCvv: number;
creditCardExpirationYear: number;
Expand Down Expand Up @@ -45,7 +45,7 @@ const CheckoutForm = ({ onSubmit }: IProps) => {
city: 'Mountain View',
state: 'CA',
country: 'United States',
zipCode: 94043,
zipCode: "94043",
creditCardNumber: '4432-8015-6152-0454',
creditCardCvv: 672,
creditCardExpirationYear: 2030,
Expand Down Expand Up @@ -99,13 +99,12 @@ const CheckoutForm = ({ onSubmit }: IProps) => {
/>
<Input
label="Zip Code"
type="number"
type="text"
name="zipCode"
id="zip_code"
value={zipCode}
onChange={handleChange}
required
pattern="\d{4,5}"
/>
<Input label="City" type="text" name="city" id="city" value={city} required onChange={handleChange} />

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/components/CheckoutItem/CheckoutItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const CheckoutItem = ({
},
cost = { currencyCode: 'USD', units: 0, nanos: 0 },
},
address: { streetAddress = '', city = '', state = '', zipCode = 0, country = '' },
address: { streetAddress = '', city = '', state = '', zipCode = '', country = '' },
}: IProps) => {
const [isCollapsed, setIsCollapsed] = useState(false);

Expand Down
5 changes: 3 additions & 2 deletions src/frontend/gateways/Api.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ad, Cart, CartItem, Money, PlaceOrderRequest, Product } from '../protos/demo';
import { Ad, Address, Cart, CartItem, Money, PlaceOrderRequest, Product } from '../protos/demo';
import { IProductCart, IProductCartItem, IProductCheckout } from '../types/Cart';
import request from '../utils/Request';
import SessionGateway from './Session.gateway';
Expand Down Expand Up @@ -36,12 +36,13 @@ const ApiGateway = () => ({
});
},

getShippingCost(itemList: IProductCartItem[], currencyCode: string) {
getShippingCost(itemList: IProductCartItem[], currencyCode: string, address: Address) {
return request<Money>({
url: `${basePath}/shipping`,
queryParams: {
itemList: JSON.stringify(itemList.map(({ productId, quantity }) => ({ productId, quantity }))),
currencyCode,
address: JSON.stringify(address),
},
});
},
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/gateways/rpc/Shipping.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ChannelCredentials } from '@grpc/grpc-js';
import { CartItem, GetQuoteResponse, ShippingServiceClient } from '../../protos/demo';
import { Address, CartItem, GetQuoteResponse, ShippingServiceClient } from '../../protos/demo';

const { SHIPPING_SERVICE_ADDR = '' } = process.env;

const client = new ShippingServiceClient(SHIPPING_SERVICE_ADDR, ChannelCredentials.createInsecure());

const AdGateway = () => ({
getShippingCost(itemList: CartItem[]) {
getShippingCost(itemList: CartItem[], address: Address) {
return new Promise<GetQuoteResponse>((resolve, reject) =>
client.getQuote({ items: itemList, address: undefined }, (error, response) =>
client.getQuote({ items: itemList, address: address }, (error, response) =>
error ? reject(error) : resolve(response)
)
);
Expand Down
7 changes: 4 additions & 3 deletions src/frontend/pages/api/shipping.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import InstrumentationMiddleware from '../../utils/telemetry/InstrumentationMiddleware';
import ShippingGateway from '../../gateways/rpc/Shipping.gateway';
import { CartItem, Empty, Money } from '../../protos/demo';
import { Address, CartItem, Empty, Money } from '../../protos/demo';
import CurrencyGateway from '../../gateways/rpc/Currency.gateway';

type TResponse = Money | Empty;

const handler = async ({ method, query }: NextApiRequest, res: NextApiResponse<TResponse>) => {
switch (method) {
case 'GET': {
const { itemList = '', currencyCode = 'USD' } = query;
const { costUsd } = await ShippingGateway.getShippingCost(JSON.parse(itemList as string) as CartItem[]);
const { itemList = '', currencyCode = 'USD', address = '' } = query;
const { costUsd } = await ShippingGateway.getShippingCost(JSON.parse(itemList as string) as CartItem[],
JSON.parse(address as string) as Address);
const cost = await CurrencyGateway.convert(costUsd!, currencyCode as string);

return res.status(200).json(cost!);
Expand Down
18 changes: 9 additions & 9 deletions src/loadgenerator/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"email": "[email protected]",
"address": {
"streetAddress": "1600 Amphitheatre Parkway",
"zipCode": 94043,
"zipCode": "94043",
"city": "Mountain View",
"state": "CA",
"country": "United States",
Expand All @@ -67,7 +67,7 @@
"email": "[email protected]",
"address": {
"streetAddress": "One Microsoft Way",
"zipCode": 98052,
"zipCode": "98052",
"city": "Redmond",
"state": "WA",
"country": "United States",
Expand All @@ -84,7 +84,7 @@
"email": "[email protected]",
"address": {
"streetAddress": "One Apple Park Way",
"zipCode": 95014,
"zipCode": "95014",
"city": "Cupertino",
"state": "CA",
"country": "United States",
Expand All @@ -101,7 +101,7 @@
"email": "[email protected]",
"address": {
"streetAddress": "1 Hacker Way",
"zipCode": 94025,
"zipCode": "94025",
"city": "Menlo Park",
"state": "CA",
"country": "United States",
Expand All @@ -118,7 +118,7 @@
"email": "[email protected]",
"address": {
"streetAddress": "410 Terry Ave N",
"zipCode": 98109,
"zipCode": "98109",
"city": "Seattle",
"state": "WA",
"country": "United States",
Expand All @@ -135,7 +135,7 @@
"email": "[email protected]",
"address": {
"streetAddress": "100 Winchester Circle",
"zipCode": 95032,
"zipCode": "95032",
"city": "Los Gatos",
"state": "CA",
"country": "United States",
Expand All @@ -152,7 +152,7 @@
"email": "[email protected]",
"address": {
"streetAddress": "150 Elgin St",
"zipCode": 214,
"zipCode": "K2P1L4",
"city": "Ottawa",
"state": "ON",
"country": "Canada",
Expand All @@ -169,7 +169,7 @@
"email": "[email protected]",
"address": {
"streetAddress": "1355 Market St",
"zipCode": 94103,
"zipCode": "94103",
"city": "San Francisco",
"state": "CA",
"country": "United States",
Expand All @@ -186,7 +186,7 @@
"email": "[email protected]",
"address": {
"streetAddress": "2200 Mission College Blvd",
"zipCode": 95054,
"zipCode": "95054",
"city": "Santa Clara",
"state": "CA",
"country": "United States",
Expand Down
5 changes: 3 additions & 2 deletions src/paymentservice/package-lock.json

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

7 changes: 4 additions & 3 deletions src/shippingservice/src/shipping_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ impl ShippingService for ShippingServer {
let parent_cx =
global::get_text_map_propagator(|prop| prop.extract(&MetadataMap(request.metadata())));

let itemct: u32 = request
.into_inner()
let request_message = request.into_inner();

let itemct: u32 = request_message
.items
.into_iter()
.fold(0, |accum, cart_item| accum + (cart_item.quantity as u32));
Expand All @@ -62,8 +63,8 @@ impl ShippingService for ShippingServer {
// check out the create_quote_from_count method to see how we use the span created here
let tracer = global::tracer("shippingservice/get-quote");
let mut span = tracer.start_with_context("get-quote", &parent_cx);

span.add_event("Processing get quote request".to_string(), vec![]);
span.set_attribute(KeyValue::new("app.shipping.zip_code", request_message.address.unwrap().zip_code));

let q = match create_quote_from_count(itemct)
.with_context(Context::current_with_span(span))
Expand Down
6 changes: 3 additions & 3 deletions test/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"city": "Seattle",
"state": "Washington",
"country": "United States",
"zipCode": 98109
"zipCode": "98109"
},
"email": "[email protected]",
"creditCard": {
Expand Down Expand Up @@ -50,7 +50,7 @@
"city": "Mountain View",
"state": "California",
"country": "United States",
"zipCode": 94043
"zipCode": "94043"
},
"items": [
{
Expand Down Expand Up @@ -90,7 +90,7 @@
"city": "Redmond",
"state": "Washington",
"country": "United States",
"zipCode": 98052
"zipCode": "98052"
},
"items": [
{
Expand Down

0 comments on commit 8dff26c

Please sign in to comment.