Test-data laboratory
+Populate local Logster with realistic errors, warnings, request snapshots, backtraces, duplicates, and edge-case payloads. GET is safe; generation only occurs through POST.
+diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 48063a5..484c200 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -139,6 +139,31 @@ jobs: working-directory: client-app run: pnpm run test:ember + website: + runs-on: ubuntu-latest + + services: + redis: + image: redis:8.10 + ports: + - 6379:6379 + + env: + BUNDLE_GEMFILE: ${{ github.workspace }}/website/Gemfile + + steps: + - uses: actions/checkout@v7 + + - name: Setup ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: "4.0" + bundler-cache: true + + - name: Website tests + working-directory: website + run: bundle exec rake test + linting: runs-on: ubuntu-latest @@ -183,7 +208,7 @@ jobs: publish: if: github.event_name == 'push' && github.ref == 'refs/heads/main' - needs: [backend, rack, frontend, linting] + needs: [backend, rack, frontend, website, linting] runs-on: ubuntu-latest steps: diff --git a/CHANGELOG.md b/CHANGELOG.md index f06fd91..81ea908 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ - FIX: Correct pattern settings state handling for plain JSON arrays and newly created or removed patterns - DEV: Upgrade the client from Ember 3.28 to Ember 7.2 and serve the scripts and stylesheets listed in a manifest generated by the Ember build - DEV: Convert the client components and route templates to Glimmer GJS + - DEV: Add a unified `bundle exec rake dev` workflow that installs dependencies, starts the backend and frontend, waits for readiness, and opens the app + - DEV: Add a reusable sample-data loader and richer development fixtures, and run the website tests in CI - DEV: Ship only the runtime files and the built frontend assets in the gem, and verify the packaged assets before a release - 2026-04-29: 2.21.0 diff --git a/README.md b/README.md index 76df9ff..65f8f42 100644 --- a/README.md +++ b/README.md @@ -160,11 +160,10 @@ Logster UI is built using [Ember.js](http://emberjs.com/) 1. Fork it ( https://github.com/discourse/logster/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) -3. Run `cd client-app && pnpm install` -4. Run `cd website && bundle install` -5. In the root directory, run `bundle exec rake client_dev` to start the Sinatra server (port 9292) and the Vite dev server (port 4200). Use the Vite server for hot reload of client code. It forwards the viewer's endpoints to Sinatra; set `LOGSTER_BACKEND` if the backend is not on `http://127.0.0.1:9292`. -6. Visit `http://localhost:4200/logs/` (with trailing slash) to test the application. Reload `http://localhost:4200/report_error` to add sample log data. -7. Once you're done making changes, run `./build_client_app.sh` to make and copy a production build to the assets folder. -8. Commit your changes (`git commit -am 'Add some feature'`) -9. Push to the branch (`git push origin my-new-feature`) -10. Create a new Pull Request +3. Run `bundle install` +4. Run `bundle exec rake dev`. This installs any missing frontend and website dependencies, starts both development servers, and opens Logster in your browser. +5. Visit `http://localhost:9292/report_error` to add sample log data. The Ember app at `http://localhost:4200/logs/` reloads automatically as you make frontend changes. +6. Once you're done making changes, run `./build_client_app.sh` to make and copy a production build to the assets folder. +7. Commit your changes (`git commit -am 'Add some feature'`) +8. Push to the branch (`git push origin my-new-feature`) +9. Create a new Pull Request diff --git a/Rakefile b/Rakefile index dab4519..aef46a4 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,13 @@ # frozen_string_literal: true require "bundler/gem_tasks" +require "net/http" require "rake/testtask" +require "rbconfig" +require "redis" +require "socket" +require "uri" +require_relative "dev_server_process" require_relative "frontend_assets" Rake::TestTask.new do |t| @@ -27,16 +33,221 @@ end task build: :build_client_app -desc "Starts Sinatra and Ember servers" -task :client_dev do - begin - pid = spawn("cd website && LOGSTER_ENV=development BUNDLE_GEMFILE=Gemfile bundle exec puma") - pid2 = spawn("cd client-app && pnpm start --port 4200") - Process.wait pid - Process.wait pid2 - rescue Interrupt => e - sleep 0.5 - puts "Done!" - exit 0 +module DevServer + extend self + + ROOT = File.expand_path(__dir__) + CLIENT_DIR = File.join(ROOT, "client-app") + WEBSITE_DIR = File.join(ROOT, "website") + + def run + backend_port = Integer(ENV.fetch("BACKEND_PORT", 9292)) + frontend_port = Integer(ENV.fetch("FRONTEND_PORT", 4200)) + backend_url = "http://127.0.0.1:#{backend_port}" + frontend_url = "http://127.0.0.1:#{frontend_port}/logs/" + processes = [] + + check_redis! + check_pnpm! + ensure_port_available!(backend_port) + ensure_port_available!(frontend_port) + install_frontend_dependencies! + install_website_dependencies! + + puts "Starting Logster development servers..." + processes << ["backend", spawn_backend(backend_port)] + processes << ["frontend", spawn_frontend(frontend_port, backend_url)] + + wait_for!("backend", "#{backend_url}/", processes) + wait_for!("frontend", frontend_url, processes) + + puts + puts "Logster is ready at #{frontend_url}" + puts "Generate rich test data at #{backend_url}/report_error" + puts "Press Ctrl-C to stop." + open_browser(frontend_url) + + name, pid = DevServerProcess.wait_for_process_exit(processes) + abort "#{name.capitalize} process #{pid} stopped unexpectedly" + rescue Interrupt + puts "\nStopping Logster..." + ensure + stop_processes(processes || []) + end + + private + + def check_redis! + Redis.new.ping + puts "✓ Redis is available" + rescue Redis::BaseError, SystemCallError => error + abort <<~MESSAGE + Redis is not available: #{error.message} + + Start Redis, then run `bundle exec rake dev` again. + MESSAGE + end + + def check_pnpm! + available = system("pnpm", "--version", out: File::NULL, err: File::NULL) + abort "pnpm is required. Run `npm install -g pnpm`, then try again." unless available + + puts "✓ pnpm is available" + rescue Errno::ENOENT + abort "pnpm is required. Run `npm install -g pnpm`, then try again." + end + + def ensure_port_available!(port) + socket = Socket.tcp("127.0.0.1", port, connect_timeout: 0.2) + socket.close + abort "Port #{port} is already in use. Set #{port == 9292 ? "BACKEND_PORT" : "FRONTEND_PORT"} to use another port." + rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ETIMEDOUT + nil + end + + def install_frontend_dependencies! + installed = File.join(CLIENT_DIR, "node_modules", ".pnpm", "lock.yaml") + lockfile = File.join(CLIENT_DIR, "pnpm-lock.yaml") + if File.exist?(installed) && File.read(installed) == File.read(lockfile) + return puts("✓ Frontend dependencies are current") + end + + puts "Installing frontend dependencies..." + unless system("pnpm", "install", "--dir", CLIENT_DIR) + abort "Frontend dependency installation failed" + end + + puts "✓ Frontend dependencies are current" + end + + def install_website_dependencies! + gemfile = File.join(WEBSITE_DIR, "Gemfile") + env = { "BUNDLE_GEMFILE" => gemfile } + available = + Bundler.with_unbundled_env do + system(env, "bundle", "check", chdir: WEBSITE_DIR, out: File::NULL) + end + return puts("✓ Website dependencies are current") if available + + puts "Installing website dependencies..." + installed = Bundler.with_unbundled_env { system(env, "bundle", "install", chdir: WEBSITE_DIR) } + abort "Website dependency installation failed" unless installed + + puts "✓ Website dependencies are current" + end + + def spawn_backend(port) + env = { + "BUNDLE_GEMFILE" => File.join(WEBSITE_DIR, "Gemfile"), + "LOGSTER_ENV" => "development", + "LOGSTER_ENABLE_CUSTOM_PATTERNS_VIA_UI" => "1", + } + spawn( + env, + "bundle", + "exec", + "puma", + "-p", + port.to_s, + "-e", + "development", + chdir: WEBSITE_DIR, + pgroup: true, + ) + end + + def spawn_frontend(port, backend_url) + spawn( + { "LOGSTER_BACKEND" => backend_url }, + "pnpm", + "start", + "--host", + "127.0.0.1", + "--port", + port.to_s, + chdir: CLIENT_DIR, + pgroup: true, + ) + end + + def wait_for!(name, url, processes, timeout: 90) + uri = URI(url) + deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout + + loop do + stopped = processes.find { |_, pid| Process.waitpid(pid, Process::WNOHANG) } + abort "#{stopped.first.capitalize} stopped before #{name} was ready" if stopped + + begin + response = + Net::HTTP.start(uri.host, uri.port, open_timeout: 1, read_timeout: 1) do |http| + http.get(uri.request_uri) + end + if response.code.to_i < 500 + puts "✓ #{name.capitalize} is ready" + return + end + rescue SystemCallError, Timeout::Error, EOFError + # The server is still starting. + end + + if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline + abort "Timed out waiting for #{name} at #{url}" + end + sleep 0.25 + end + end + + def open_browser(url) + return puts("Browser opening disabled; visit #{url}") if ENV["NO_OPEN"] == "1" + + command = + case RbConfig::CONFIG["host_os"] + when /darwin/ + ["open", url] + when /mswin|mingw|cygwin/ + ["cmd", "/c", "start", "", url] + else + ["xdg-open", url] + end + + pid = spawn(*command, out: File::NULL, err: File::NULL) + Process.detach(pid) + rescue Errno::ENOENT + puts "Could not open a browser automatically; visit #{url}" + end + + def stop_processes(processes) + processes.reverse_each do |_, pid| + Process.kill("TERM", -pid) + rescue Errno::ESRCH + nil + end + + remaining = processes.map(&:last) + deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 5 + while remaining.any? && Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline + remaining.reject! do |pid| + Process.waitpid(pid, Process::WNOHANG) + rescue Errno::ECHILD + true + end + sleep 0.1 if remaining.any? + end + + remaining.each do |pid| + Process.kill("KILL", -pid) + Process.waitpid(pid) + rescue Errno::ESRCH, Errno::ECHILD + nil + end end end + +desc "Start the Logster backend and frontend development servers" +task :dev do + DevServer.run +end + +desc "Deprecated alias for dev" +task client_dev: :dev diff --git a/dev_server_process.rb b/dev_server_process.rb new file mode 100644 index 0000000..4329479 --- /dev/null +++ b/dev_server_process.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module DevServerProcess + module_function + + def wait_for_process_exit(processes, wait: -> { Process.wait }) + stopped_pid = wait.call + processes.find { |_, process_pid| process_pid == stopped_pid } || ["server", stopped_pid] + end +end diff --git a/test/test_dev_server.rb b/test/test_dev_server.rb new file mode 100644 index 0000000..ba47ee9 --- /dev/null +++ b/test/test_dev_server.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require_relative "test_helper" +require_relative "../dev_server_process" + +class TestDevServerProcess < Minitest::Test + def test_identifies_the_first_process_that_exits + processes = [["backend", 101], ["frontend", 202]] + + stopped = DevServerProcess.wait_for_process_exit(processes, wait: -> { 202 }) + + assert_equal(["frontend", 202], stopped) + end +end diff --git a/website/Gemfile b/website/Gemfile index 8320edf..0c5b891 100644 --- a/website/Gemfile +++ b/website/Gemfile @@ -6,3 +6,8 @@ gem "sinatra" gem "redis" gem "logster", path: "../" gem "puma" + +group :development, :test do + gem "minitest" + gem "rake" +end diff --git a/website/Gemfile.lock b/website/Gemfile.lock index cd624fc..6ab5c16 100644 --- a/website/Gemfile.lock +++ b/website/Gemfile.lock @@ -8,9 +8,14 @@ GEM specs: base64 (0.3.0) connection_pool (3.0.2) + drb (2.2.3) logger (1.7.0) + minitest (6.0.6) + drb (~> 2.0) + prism (~> 1.5) mustermann (3.1.1) nio4r (2.7.5) + prism (1.9.0) puma (8.0.2) nio4r (~> 2.0) rack (3.2.7) @@ -21,6 +26,7 @@ GEM rack-session (2.1.2) base64 (>= 0.1.0) rack (>= 3.0.0) + rake (13.4.2) redis (6.0.0) redis-client (= 0.30.1) redis-client (0.30.1) @@ -39,7 +45,9 @@ PLATFORMS DEPENDENCIES logster! + minitest puma + rake redis sinatra diff --git a/website/README.md b/website/README.md index fff6f3d..d3c206e 100644 --- a/website/README.md +++ b/website/README.md @@ -1,10 +1,43 @@ ## Sample Website -This website is running at [logster.info](http://logster.info). +This Sinatra application powers Logster's local development backend. -To start it and test your code changes: +Start the complete development environment from the repository root: -```text -$ bundle install -$ bundle exec puma +```sh +bundle exec rake dev +``` + +The relevant pages are: + +- `http://127.0.0.1:4200/logs/` — the Ember log viewer +- `http://127.0.0.1:9292/report_error` — the test-data laboratory + +`bundle exec rake dev` enables the settings page and grouping-pattern +multi-select automatically. Direct website deployments remain opt-in through +`LOGSTER_ENABLE_CUSTOM_PATTERNS_VIA_UI=1`. + +`GET /report_error` is read-only. The laboratory generates reports with bounded +`POST /report_error` batches and shows progress while creating large datasets. +It can generate mixed data or focused exception, request, database, background +job, duplicate, severity, and Unicode scenarios. + +The POST endpoint also accepts JSON directly: + +```sh +curl -X POST http://127.0.0.1:9292/report_error \ + -H 'Content-Type: application/json' \ + -d '{"scenario":"mixed","count":50,"seed":1234}' +``` + +A single request is limited to 100 reports. The browser laboratory creates +larger datasets through multiple requests so progress and cancellation remain +responsive. + +Run the website tests with: + +```sh +cd website +BUNDLE_GEMFILE=Gemfile bundle exec ruby -Itest test/sample_loader_test.rb +BUNDLE_GEMFILE=Gemfile bundle exec ruby -Itest test/sample_app_test.rb ``` diff --git a/website/Rakefile b/website/Rakefile new file mode 100644 index 0000000..eec0fa5 --- /dev/null +++ b/website/Rakefile @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require "rake/testtask" + +Rake::TestTask.new do |task| + task.libs << "test" + task.pattern = "test/**/*_test.rb" +end + +task default: :test diff --git a/website/sample.rb b/website/sample.rb index c60e98d..a03c309 100644 --- a/website/sample.rb +++ b/website/sample.rb @@ -9,76 +9,18 @@ require "json" require "sinatra" require "sinatra/base" +require_relative "sample_loader" -# log a few errors SAMPLE_REDIS = Redis.new SAMPLE_STORE = Logster.store = Logster::RedisStore.new(SAMPLE_REDIS) Logster.logger = Logster::Logger.new(SAMPLE_STORE) -class SampleLoader - def initialize - @index = 0 - @sample_data_key = "sample_data" - end - - def ensure_samples_loaded - SAMPLE_REDIS.del @sample_data_key - data = File.read("data/data.json") - parsed = JSON.parse(data) - parsed.each { |row| SAMPLE_REDIS.rpush @sample_data_key, JSON.generate(row) } - @length = parsed.length - end - - def load_samples - Thread.new do - while true - sleep 5 - begin - load_next_sample - rescue => e - SAMPLE_STORE.report(4, "logster", e.to_s) - end - end - end - end - - def load_next_sample - message = JSON.parse(SAMPLE_REDIS.lindex(@sample_data_key, @index)) - @index += 1 - @index %= @length - - SAMPLE_STORE.report( - message["severity"], - message["progname"], - message["message"], - backtrace: message["backtrace"], - env: message["env"], - count: message["count"], - ) - end - - def load_error - # 2 = Severity.WARN - params = {} - params["always_present"] = "some_value_#{rand(3)}" - params["key_#{rand(3)}"] = "some_value_#{rand(3)}" - SAMPLE_STORE.report( - 2, - "", - "Message message message", - backtrace: "Backtrace backtrace backtrace", - env: { - something: :foo, - random: rand(3), - array: [1, 2, 3], - rand_array: [10, 11, rand(300)], - params: params, - }, - ) - end -end - -SampleLoaderInstance = SampleLoader.new +SampleLoaderInstance = + SampleLoader.new( + store: SAMPLE_STORE, + redis: SAMPLE_REDIS, + data_path: File.expand_path("data/data.json", __dir__), + ) SampleLoaderInstance.ensure_samples_loaded SampleLoaderInstance.load_samples unless ENV["NO_DATA"] Logster.config.allow_grouping = true @@ -98,23 +40,258 @@ class Sample < Sinatra::Base use Logster::Middleware::Reporter get "/" do - < -
- - -Populate local Logster with realistic errors, warnings, request snapshots, backtraces, duplicates, and edge-case payloads. GET is safe; generation only occurs through POST.
+