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

load product catalog periodically #1919

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -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
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions docker-compose.minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ services:
environment:
- FLAGD_HOST
- PRODUCT_CATALOG_PORT
- PRODUCT_CATALOG_RELOAD_INTERVAL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we mount the products to the container?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

products.json is mounted in the minimal config now

- OTEL_EXPORTER_OTLP_ENDPOINT
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
- OTEL_RESOURCE_ATTRIBUTES
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,15 @@
- "${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

Check failure on line 488 in docker-compose.yml

View workflow job for this annotation

GitHub Actions / yamllint

488:9 [indentation] wrong indentation: expected 6 but found 8
depends_on:
otel-collector:
condition: service_started
Expand Down
2 changes: 1 addition & 1 deletion src/product-catalog/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
48 changes: 42 additions & 6 deletions src/product-catalog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
puckpuck marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
2 changes: 1 addition & 1 deletion src/product-catalog/products/products.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
{
"id": "1YMWWN1N4O",
"name": "Eclipsmart Travel Refractor Telescope",
"name": "THIS IS MY PRODUCT NAME",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left over?

"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": {
Expand Down
Loading