Skip to content

Commit d2a1c7f

Browse files
load product catalog periodically (open-telemetry#1919)
* load product catalog periodically * load product catalog periodically * revert changed product name * fix indent * add products.json mount * remove COPY products.json --------- Co-authored-by: Juliano Costa <julianocosta89@outlook.com>
1 parent 880f79c commit d2a1c7f

File tree

7 files changed

+52
-8
lines changed

7 files changed

+52
-8
lines changed

.env

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ PAYMENT_ADDR=payment:${PAYMENT_PORT}
107107
PAYMENT_DOCKERFILE=./src/payment/Dockerfile
108108

109109
# Product Catalog Service
110+
PRODUCT_CATALOG_RELOAD_INTERVAL=10
110111
PRODUCT_CATALOG_PORT=3550
111112
PRODUCT_CATALOG_ADDR=product-catalog:${PRODUCT_CATALOG_PORT}
112113
PRODUCT_CATALOG_DOCKERFILE=./src/product-catalog/Dockerfile

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ the release.
7272
([#1897](https://github.com/open-telemetry/opentelemetry-demo/pull/1897))
7373
* [frontend-proxy] rename frontendproxy to frontend-proxy
7474
([#1910](https://github.com/open-telemetry/opentelemetry-demo/pull/1910))
75+
* [product-catalog] load product list on a periodic timer
76+
([#1919](https://github.com/open-telemetry/opentelemetry-demo/pull/1919))
7577
* [flagd-ui] fixed eslint ignore comment with useCallback
7678
([#1923](https://github.com/open-telemetry/opentelemetry-demo/pull/1923))
7779
* [chore] Add memory for frontend-proxy, kafka, grafana, opensearch

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ start:
171171
@echo "Go to http://localhost:8080/jaeger/ui for the Jaeger UI."
172172
@echo "Go to http://localhost:8080/grafana/ for the Grafana UI."
173173
@echo "Go to http://localhost:8080/loadgen/ for the Load Generator UI."
174-
@echo "Go to http://localhost:8080/feature/ to to change feature flags."
174+
@echo "Go to http://localhost:8080/feature/ to change feature flags."
175175

176176
.PHONY: start-minimal
177177
start-minimal:

docker-compose.minimal.yml

+3
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,13 @@ services:
399399
environment:
400400
- FLAGD_HOST
401401
- PRODUCT_CATALOG_PORT
402+
- PRODUCT_CATALOG_RELOAD_INTERVAL
402403
- OTEL_EXPORTER_OTLP_ENDPOINT
403404
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
404405
- OTEL_RESOURCE_ATTRIBUTES
405406
- OTEL_SERVICE_NAME=product-catalog
407+
volumes:
408+
- ./src/product-catalog/products:/usr/src/app/products
406409
depends_on:
407410
otel-collector:
408411
condition: service_started

docker-compose.yml

+3
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,15 @@ services:
477477
- "${PRODUCT_CATALOG_PORT}"
478478
environment:
479479
- PRODUCT_CATALOG_PORT
480+
- PRODUCT_CATALOG_RELOAD_INTERVAL
480481
- FLAGD_HOST
481482
- FLAGD_PORT
482483
- OTEL_EXPORTER_OTLP_ENDPOINT
483484
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
484485
- OTEL_RESOURCE_ATTRIBUTES
485486
- OTEL_SERVICE_NAME=product-catalog
487+
volumes:
488+
- ./src/product-catalog/products:/usr/src/app/products
486489
depends_on:
487490
otel-collector:
488491
condition: service_started

src/product-catalog/Dockerfile

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ FROM alpine AS release
2020

2121
WORKDIR /usr/src/app/
2222

23-
COPY ./src/product-catalog/products/ ./products/
2423
COPY --from=builder /go/bin/product-catalog/ ./
2524

2625
EXPOSE ${PRODUCT_CATALOG_PORT}

src/product-catalog/main.go

+42-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net"
1414
"os"
1515
"os/signal"
16+
"strconv"
1617
"strings"
1718
"sync"
1819
"syscall"
@@ -53,14 +54,12 @@ var (
5354
initResourcesOnce sync.Once
5455
)
5556

57+
const DEFAULT_RELOAD_INTERVAL = 10
58+
5659
func init() {
5760
log = logrus.New()
58-
var err error
59-
catalog, err = readProductFiles()
60-
if err != nil {
61-
log.Fatalf("Reading Product Files: %v", err)
62-
os.Exit(1)
63-
}
61+
62+
loadProductCatalog()
6463
}
6564

6665
func initResource() *sdkresource.Resource {
@@ -178,6 +177,43 @@ type productCatalog struct {
178177
pb.UnimplementedProductCatalogServiceServer
179178
}
180179

180+
func loadProductCatalog() {
181+
log.Info("Loading Product Catalog...")
182+
var err error
183+
catalog, err = readProductFiles()
184+
if err != nil {
185+
log.Fatalf("Error reading product files: %v\n", err)
186+
os.Exit(1)
187+
}
188+
189+
// Default reload interval is 10 seconds
190+
interval := DEFAULT_RELOAD_INTERVAL
191+
si := os.Getenv("PRODUCT_CATALOG_RELOAD_INTERVAL")
192+
if si != "" {
193+
interval, _ = strconv.Atoi(si)
194+
if interval <= 0 {
195+
interval = DEFAULT_RELOAD_INTERVAL
196+
}
197+
}
198+
log.Infof("Product Catalog reload interval: %d", interval)
199+
200+
ticker := time.NewTicker(time.Duration(interval) * time.Second)
201+
202+
go func() {
203+
for {
204+
select {
205+
case <-ticker.C:
206+
log.Info("Reloading Product Catalog...")
207+
catalog, err = readProductFiles()
208+
if err != nil {
209+
log.Errorf("Error reading product files: %v", err)
210+
continue
211+
}
212+
}
213+
}
214+
}()
215+
}
216+
181217
func readProductFiles() ([]*pb.Product, error) {
182218

183219
// find all .json files in the products directory

0 commit comments

Comments
 (0)