From 7ece3af4deccc915078defc288a26fa92983f644 Mon Sep 17 00:00:00 2001 From: Pierre Tessier Date: Thu, 16 Jan 2025 00:12:40 -0500 Subject: [PATCH 1/6] load product catalog periodically --- .env | 1 + Makefile | 2 +- docker-compose.minimal.yml | 1 + docker-compose.yml | 3 ++ src/product-catalog/Dockerfile | 2 +- src/product-catalog/main.go | 48 +++++++++++++++++++--- src/product-catalog/products/products.json | 2 +- 7 files changed, 50 insertions(+), 9 deletions(-) diff --git a/.env b/.env index fc20a17aaa..8ea47eeb19 100644 --- a/.env +++ b/.env @@ -107,6 +107,7 @@ PAYMENT_ADDR=payment:${PAYMENT_PORT} PAYMENT_DOCKERFILE=./src/payment/Dockerfile # Product Catalog Service +PRODUCT_CATALOG_RELOAD_INTERVAL=10 PRODUCT_CATALOG_PORT=3550 PRODUCT_CATALOG_ADDR=product-catalog:${PRODUCT_CATALOG_PORT} PRODUCT_CATALOG_DOCKERFILE=./src/product-catalog/Dockerfile diff --git a/Makefile b/Makefile index 3f0eb3fd91..c40aad7702 100644 --- a/Makefile +++ b/Makefile @@ -170,7 +170,7 @@ start: @echo "Go to http://localhost:8080/jaeger/ui for the Jaeger UI." @echo "Go to http://localhost:8080/grafana/ for the Grafana UI." @echo "Go to http://localhost:8080/loadgen/ for the Load Generator UI." - @echo "Go to http://localhost:8080/feature/ to to change feature flags." + @echo "Go to http://localhost:8080/feature/ to change feature flags." .PHONY: start-minimal start-minimal: diff --git a/docker-compose.minimal.yml b/docker-compose.minimal.yml index 6d85afd3d2..b951b5978d 100644 --- a/docker-compose.minimal.yml +++ b/docker-compose.minimal.yml @@ -399,6 +399,7 @@ services: environment: - FLAGD_HOST - PRODUCT_CATALOG_PORT + - PRODUCT_CATALOG_RELOAD_INTERVAL - OTEL_EXPORTER_OTLP_ENDPOINT - OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE - OTEL_RESOURCE_ATTRIBUTES diff --git a/docker-compose.yml b/docker-compose.yml index 436a70d45b..abf03f4517 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -477,12 +477,15 @@ services: - "${PRODUCT_CATALOG_PORT}" environment: - PRODUCT_CATALOG_PORT + - PRODUCT_CATALOG_RELOAD_INTERVAL - FLAGD_HOST - FLAGD_PORT - OTEL_EXPORTER_OTLP_ENDPOINT - OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE - OTEL_RESOURCE_ATTRIBUTES - OTEL_SERVICE_NAME=product-catalog + volumes: + - ./src/product-catalog/products:/usr/src/app/products depends_on: otel-collector: condition: service_started diff --git a/src/product-catalog/Dockerfile b/src/product-catalog/Dockerfile index 66fb63f0da..56af15474d 100644 --- a/src/product-catalog/Dockerfile +++ b/src/product-catalog/Dockerfile @@ -20,7 +20,7 @@ FROM alpine AS release WORKDIR /usr/src/app/ -COPY ./src/product-catalog/products/ ./products/ +# COPY ./src/product-catalog/products/ ./products/ COPY --from=builder /go/bin/product-catalog/ ./ EXPOSE ${PRODUCT_CATALOG_PORT} diff --git a/src/product-catalog/main.go b/src/product-catalog/main.go index ae6393d994..fbdfd4718e 100644 --- a/src/product-catalog/main.go +++ b/src/product-catalog/main.go @@ -13,6 +13,7 @@ import ( "net" "os" "os/signal" + "strconv" "strings" "sync" "syscall" @@ -53,14 +54,12 @@ var ( initResourcesOnce sync.Once ) +const DEFAULT_RELOAD_INTERVAL = 10 + func init() { log = logrus.New() - var err error - catalog, err = readProductFiles() - if err != nil { - log.Fatalf("Reading Product Files: %v", err) - os.Exit(1) - } + + loadProductCatalog() } func initResource() *sdkresource.Resource { @@ -178,6 +177,43 @@ type productCatalog struct { pb.UnimplementedProductCatalogServiceServer } +func loadProductCatalog() { + log.Info("Loading Product Catalog...") + var err error + catalog, err = readProductFiles() + if err != nil { + log.Fatalf("Error reading product files: %v\n", err) + os.Exit(1) + } + + // Default reload interval is 10 seconds + interval := DEFAULT_RELOAD_INTERVAL + si := os.Getenv("PRODUCT_CATALOG_RELOAD_INTERVAL") + if si != "" { + interval, _ = strconv.Atoi(si) + if interval <= 0 { + interval = DEFAULT_RELOAD_INTERVAL + } + } + log.Infof("Product Catalog reload interval: %d", interval) + + ticker := time.NewTicker(time.Duration(interval) * time.Second) + + go func() { + for { + select { + case <-ticker.C: + log.Info("Reloading Product Catalog...") + catalog, err = readProductFiles() + if err != nil { + log.Errorf("Error reading product files: %v", err) + continue + } + } + } + }() +} + func readProductFiles() ([]*pb.Product, error) { // find all .json files in the products directory diff --git a/src/product-catalog/products/products.json b/src/product-catalog/products/products.json index b7d27b06ed..8c32a2157a 100644 --- a/src/product-catalog/products/products.json +++ b/src/product-catalog/products/products.json @@ -26,7 +26,7 @@ }, { "id": "1YMWWN1N4O", - "name": "Eclipsmart Travel Refractor Telescope", + "name": "THIS IS MY PRODUCT NAME", "description": "Dedicated white-light solar scope for the observer on the go. The 50mm refracting solar scope uses Solar Safe, ISO compliant, full-aperture glass filter material to ensure the safest view of solar events. The kit comes complete with everything you need, including the dedicated travel solar scope, a Solar Safe finderscope, tripod, a high quality 20mm (18x) Kellner eyepiece and a nylon backpack to carry everything in. This Travel Solar Scope makes it easy to share the Sun as well as partial and total solar eclipses with the whole family and offers much higher magnifications than you would otherwise get using handheld solar viewers or binoculars.", "picture": "EclipsmartTravelRefractorTelescope.jpg", "priceUsd": { From 36821692c20d98ea67ab649110de2a2c5c78246f Mon Sep 17 00:00:00 2001 From: Pierre Tessier Date: Thu, 16 Jan 2025 00:20:19 -0500 Subject: [PATCH 2/6] load product catalog periodically --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 984415f6fd..fea06b2d7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,8 @@ the release. ([#1897](https://github.com/open-telemetry/opentelemetry-demo/pull/1897)) * [frontend-proxy] rename frontendproxy to frontend-proxy ([#1910](https://github.com/open-telemetry/opentelemetry-demo/pull/1910)) +* [product-catalog] load product list on a periodic timer + ([#1919](https://github.com/open-telemetry/opentelemetry-demo/pull/1919)) ## 1.12.0 From 0a64e105f28e8f761471969dac351f7a2c229253 Mon Sep 17 00:00:00 2001 From: Pierre Tessier Date: Fri, 24 Jan 2025 11:40:42 -0500 Subject: [PATCH 3/6] revert changed product name --- src/product-catalog/products/products.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/product-catalog/products/products.json b/src/product-catalog/products/products.json index 8c32a2157a..b7d27b06ed 100644 --- a/src/product-catalog/products/products.json +++ b/src/product-catalog/products/products.json @@ -26,7 +26,7 @@ }, { "id": "1YMWWN1N4O", - "name": "THIS IS MY PRODUCT NAME", + "name": "Eclipsmart Travel Refractor Telescope", "description": "Dedicated white-light solar scope for the observer on the go. The 50mm refracting solar scope uses Solar Safe, ISO compliant, full-aperture glass filter material to ensure the safest view of solar events. The kit comes complete with everything you need, including the dedicated travel solar scope, a Solar Safe finderscope, tripod, a high quality 20mm (18x) Kellner eyepiece and a nylon backpack to carry everything in. This Travel Solar Scope makes it easy to share the Sun as well as partial and total solar eclipses with the whole family and offers much higher magnifications than you would otherwise get using handheld solar viewers or binoculars.", "picture": "EclipsmartTravelRefractorTelescope.jpg", "priceUsd": { From c912d7ac1e0be104f845d5911e3898f7f5a0eaaa Mon Sep 17 00:00:00 2001 From: Pierre Tessier Date: Fri, 24 Jan 2025 11:43:22 -0500 Subject: [PATCH 4/6] fix indent --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index abf03f4517..0941a66999 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -485,7 +485,7 @@ services: - OTEL_RESOURCE_ATTRIBUTES - OTEL_SERVICE_NAME=product-catalog volumes: - - ./src/product-catalog/products:/usr/src/app/products + - ./src/product-catalog/products:/usr/src/app/products depends_on: otel-collector: condition: service_started From 65aef6a5f604f318bf87297697561f3d42546753 Mon Sep 17 00:00:00 2001 From: Pierre Tessier Date: Fri, 24 Jan 2025 11:43:32 -0500 Subject: [PATCH 5/6] add products.json mount --- docker-compose.minimal.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose.minimal.yml b/docker-compose.minimal.yml index b951b5978d..3bff441c64 100644 --- a/docker-compose.minimal.yml +++ b/docker-compose.minimal.yml @@ -404,6 +404,8 @@ services: - OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE - OTEL_RESOURCE_ATTRIBUTES - OTEL_SERVICE_NAME=product-catalog + volumes: + - ./src/product-catalog/products:/usr/src/app/products depends_on: otel-collector: condition: service_started From 162d1d3ecd98a58392c270c121097ad999f3172d Mon Sep 17 00:00:00 2001 From: Pierre Tessier Date: Fri, 24 Jan 2025 11:46:16 -0500 Subject: [PATCH 6/6] remove COPY products.json --- src/product-catalog/Dockerfile | 1 - 1 file changed, 1 deletion(-) diff --git a/src/product-catalog/Dockerfile b/src/product-catalog/Dockerfile index 56af15474d..a13aafb9bb 100644 --- a/src/product-catalog/Dockerfile +++ b/src/product-catalog/Dockerfile @@ -20,7 +20,6 @@ FROM alpine AS release WORKDIR /usr/src/app/ -# COPY ./src/product-catalog/products/ ./products/ COPY --from=builder /go/bin/product-catalog/ ./ EXPOSE ${PRODUCT_CATALOG_PORT}