Files
dance-lessons-coach/scripts/test-opentelemetry.sh
Gabriel Radureau c17fb4f9b4
Some checks failed
CI/CD Pipeline / Build Docker Cache (push) Successful in 10s
CI/CD Pipeline / CI Pipeline (push) Failing after 4m14s
CI/CD Pipeline / Trigger Docker Push (push) Has been skipped
🐛 fix: emit all config-loading logs in correct JSON format from the start (#16)
## Summary

Closes #15

When `logging.json: true` (or `DLC_LOGGING_JSON=true`), the logger was unconditionally initialised to console/text format at the top of `LoadConfig()`, so early log lines — most visibly **"Config file loaded"** — were always written as human-readable text regardless of configuration.

## Root cause

Classic chicken-and-egg: the format flag lives inside the config that is being loaded. The format-switch block only ran *after* `v.Unmarshal()`, too late for the config-file log.

## Changes

### `pkg/config/config.go`
- Add `peekJSONLogging()`: resolves the JSON flag **before** any log is emitted by (1) checking `DLC_LOGGING_JSON` directly via `os.Getenv`, then (2) doing a minimal throwaway Viper pre-read of the config file for the `logging.json` key. This mirrors Viper's own priority order without parsing the full config twice.
- Apply the resolved format immediately and emit **"Logging configured"** as the very first log line.
- Remove the now-redundant format-switch block that ran after `Unmarshal()`.

### `scripts/start-server.sh`, `test-graceful-shutdown.sh`, `test-opentelemetry.sh`
- Replace hardcoded `PROJECT_DIR` path with a dynamic `SCRIPTS_DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))` derivation so scripts work from any worktree or clone location.

## Test plan
- [x] `go test ./pkg/...` — all pass
- [x] `scripts/test-graceful-shutdown.sh` — all JSON valid, all startup logs present
- [x] Manual smoke test: first line is `{"level":"info",...,"message":"Logging configured"}`, every line is valid JSON

Reviewed-on: #16
Co-authored-by: Gabriel Radureau <arcodange@gmail.com>
Co-committed-by: Gabriel Radureau <arcodange@gmail.com>
2026-04-12 23:28:35 +02:00

93 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
# dance-lessons-coach OpenTelemetry Test Script
# This script tests OpenTelemetry integration with Jaeger
set -e
echo -e "\033[1;34m=== dance-lessons-coach OpenTelemetry Test ===\033[0m"
echo ""
# Configuration
SCRIPTS_DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
PROJECT_DIR=$(dirname "$SCRIPTS_DIR")
SERVER_CMD="./scripts/start-server.sh"
LOG_FILE="server.log"
PID_FILE="server.pid"
cd "$PROJECT_DIR"
# Clean up any existing server
echo "Cleaning up any existing server..."
if [ -f "$PID_FILE" ]; then
echo "Found existing PID file, stopping previous server..."
$SERVER_CMD stop > /dev/null 2>&1 || true
rm -f "$PID_FILE" "$LOG_FILE"
fi
# Kill any processes on port 8080
pkill -9 -f "go run" > /dev/null 2>&1 || true
lsof -ti :8080 | xargs -I {} kill -9 {} > /dev/null 2>&1 || true
lsof -ti :4317 | xargs -I {} kill -9 {} > /dev/null 2>&1 || true
sleep 1
echo "Starting Jaeger in Docker..."
# Start Jaeger container if not running
if ! docker ps | grep -q "jaegertracing/all-in-one"; then
docker run -d --name jaeger \
-e COLLECTOR_OTLP_ENABLED=true \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
jaegertracing/all-in-one:latest
echo "Jaeger container started"
sleep 5
else
echo "Jaeger container already running"
fi
echo "Starting server with OpenTelemetry enabled..."
DLC_TELEMETRY_ENABLED=true DLC_TELEMETRY_OTLP_ENDPOINT="localhost:4317" DLC_TELEMETRY_INSECURE=true \
DLC_TELEMETRY_SERVICE_NAME="dance-lessons-coach" $SERVER_CMD start
sleep 3
echo "Testing API endpoints..."
# Test health endpoint
echo "Testing /api/health:"
HEALTH_RESPONSE=$(curl -s http://localhost:8080/api/health)
echo "Response: $HEALTH_RESPONSE"
# Test greet endpoint
echo "Testing /api/v1/greet/:"
GREET_RESPONSE=$(curl -s http://localhost:8080/api/v1/greet/)
echo "Response: $GREET_RESPONSE"
# Test greet with name
echo "Testing /api/v1/greet/TestUser:"
GREET_NAME_RESPONSE=$(curl -s http://localhost:8080/api/v1/greet/TestUser)
echo "Response: $GREET_NAME_RESPONSE"
echo ""
echo "Stopping server..."
$SERVER_CMD stop
sleep 2
echo ""
echo -e "\033[0;32m✅ OpenTelemetry Test Complete!\033[0m"
echo ""
echo "To view traces in Jaeger:"
echo "1. Open http://localhost:16686 in your browser"
echo "2. Select 'dance-lessons-coach' service"
echo "3. Click 'Find Traces' button"
echo ""
echo "You should see traces for:"
echo "- /api/health requests"
echo "- /api/v1/greet/ requests"
echo "- /api/v1/greet/TestUser requests"
echo ""
# Clean up
rm -f "$PID_FILE" "$LOG_FILE"
exit 0