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

Primarily Prometheus Metrics #129

Merged
merged 10 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 39 additions & 14 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,51 @@
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

!/log/.keep
.byebug_history
.github-token
.npmrc
.tags
# Ignore vscode config
.vscode
.yarn-integrity

## Ignore bundler configuration:
/.bundle
/vendor/bundle
/lib/bundler/man/

# Ignore all logfiles and tempfiles.
/log/*
/node_modules
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore Byebug command history file.
.byebug_history

# Ignore node_modules
node_modules/

# Ignore precompiled javascript packs
/public/packs
/public/packs-test
/tmp
coverage
fc_simple.json
/public/assets

# Ignore uploaded files in development
/storage/*
!/storage/.keep
/public/uploads

### Rails specific ###
.byebug_history
/public/system
/coverage/
tmp

# Ignore files specific to the development environment
fc.json
fc_simple.json
index-names.txt
index.json
node_modules/
public/assets
tags
yarn-debug.log*
yarn-error.log

# Ignore dot files used by environment or IDE tools
.tags
.tool-versions
.github-token
.npmrc
25 changes: 21 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@
This app presents the landing page experience for landregistry.data.gov.uk,
including the SPARQL Qonsole

## 1.8.0 - 2024-08

- (Jon) Implemented improved boilerplate metrics integration to offer analysis of
current application usage stats
- (Jon) Implemented the dynamic page title approach used in the other suite apps
to the accessibility and privacy translation templates
- (Jon) Converted the privacy templates to match the same haml formatting
language used in the app
- (Jon) Tweaked the application controller to improve selected language option
to be applied for the pages
- (Jon) Reorganised makefile targets alphabetically as well as mirrored other
improvements from the other applications in the suite
- (Jon) Updated .gitignore file to mirror the current approach in the other HMLR
apps

## 1.7.7 - 2024-08

- (Dan) Updates gemfile to use v1.9.5 lr_common_styles
- (Dan) Adds underlines to links in body text to meet WCAG 2.2 accessibiliy requirments[GH-126](https://github.com/epimorphics/lr-landing/issues/126)
- (Dan) Adds underlines to links in body text to meet WCAG 2.2 accessibiliy
requirments[GH-126](https://github.com/epimorphics/lr-landing/issues/126)

## 1.7.6 - 2024-03-12

Expand All @@ -29,7 +45,8 @@ including the SPARQL Qonsole

- (Jon) Updated the `app/controllers/application_controller.rb` to include the
`before_action` for the `change_default_caching_policy` method to ensure the
default `Cache-Control` header for all requests is set to 5 minutes (300 seconds).
default `Cache-Control` header for all requests is set to 5 minutes (300
seconds).

## 1.7.3 - 2023-06-07

Expand Down Expand Up @@ -58,8 +75,8 @@ including the SPARQL Qonsole
- (Jon) Updated and improved the build files for the new infrastructure use.
- (Jon) Minor text changes to the `Gemfile` to include instructions for running
Epimorphics specific gems locally during the development of those gems.
- (Jon) Updated the production `json_rails_logger` gem version to be at least the
current version `~>0.3.5` (this is to cover out of sync release versions)
- (Jon) Updated the production `json_rails_logger` gem version to be at least
the current version `~>0.3.5` (this is to cover out of sync release versions)
- (Jon) Updated the production `lr_common_styles` gem version to be at least the
current version `~>1.9.1` (this is to cover out of sync release versions)
- (Jon) Refactored the version cadence creation to include a SUFFIX value if
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ end
# ! These "local" paths do not work with a docker image - use the repo instead
# gem 'qonsole-rails', path: '~/Epimorphics/clients/land-registry/projects/qonsole-rails'
# gem 'json_rails_logger', '~> 1.0.0', path: '~/Epimorphics/shared/json-rails-logger/'
# gem 'lr_common_styles', '~> 1.9.3', path: '~/Epimorphics/clients/land-registry/projects/lr_common_styles/'
# gem 'lr_common_styles', '~> 1.9', '>= 1.9.6', path: '~/Epimorphics/clients/land-registry/projects/lr_common_styles/'
# rubocop:enable Layout/LineLength

# TODO: In production you want to set this to the gem from the epimorphics github repo
Expand All @@ -46,5 +46,5 @@ gem 'qonsole-rails', git: 'https://github.com/epimorphics/qonsole-rails'
# TODO: In production you want to set this to the gem from the epimorphics package repo
source 'https://rubygems.pkg.github.com/epimorphics' do
gem 'json_rails_logger', '~> 1.0.0'
gem 'lr_common_styles', '~> 1.9', '>= 1.9.5'
gem 'lr_common_styles', '~> 1.9', '>= 1.9.6'
end
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ GEM
tzinfo (~> 1.1)
arel (9.0.0)
ast (2.4.2)
autoprefixer-rails (10.4.16.0)
autoprefixer-rails (10.4.19.0)
execjs (~> 2)
bindex (0.8.1)
bootstrap-sass (3.4.1)
Expand Down Expand Up @@ -155,7 +155,7 @@ GEM
parser (3.1.1.0)
ast (~> 2.4.1)
prometheus-client (4.0.0)
puma (5.6.7)
puma (6.4.2)
nio4r (~> 2.0)
racc (1.6.2)
rack (2.2.8)
Expand Down Expand Up @@ -263,7 +263,7 @@ GEM
json
lograge
railties
lr_common_styles (1.9.3)
lr_common_styles (1.9.6)
bootstrap-sass (~> 3.4.0)
font-awesome-rails (~> 4.7.0.1)
govuk_elements_rails (~> 2.0.0)
Expand All @@ -290,7 +290,7 @@ DEPENDENCIES
jbuilder (~> 2.0)
jquery-rails
json_rails_logger (~> 1.0.0)!
lr_common_styles (~> 1.9.3)!
lr_common_styles (~> 1.9, >= 1.9.6)!
prometheus-client (~> 4.0)
puma
qonsole-rails!
Expand Down
31 changes: 24 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: assets clean image lint publish realclean run tag test vars
.PHONY: assets auth check clean image lint local publish realclean run tag test vars

ACCOUNT?=$(shell aws sts get-caller-identity | jq -r .Account)
ALPINE_VERSION?=3.13
Expand Down Expand Up @@ -27,23 +27,27 @@ REPO?=${ECR}/${IMAGE}
GITHUB_TOKEN=.github-token
BUNDLE_CFG=.bundle/config

all: image

${BUNDLE_CFG}: ${GITHUB_TOKEN}
@./bin/bundle config set --local rubygems.pkg.github.com ${GPR_OWNER}:`cat ${GITHUB_TOKEN}`

${GITHUB_TOKEN}:
@echo ${PAT} > ${GITHUB_TOKEN}

assets:
@./bin/bundle config set --local without 'development'
all: image

assets: auth
@./bin/bundle config set --local without 'development test'
@./bin/bundle install
@./bin/rails assets:clean assets:precompile

auth: ${GITHUB_TOKEN} ${BUNDLE_CFG}

check: lint test
@echo "All checks passed."

clean:
@[ -d public/assets ] && ./bin/rails assets:clobber || :
@@ rm -rf bundle coverage log node_modules

image: auth
@echo Building ${REPO}:${TAG} ...
Expand All @@ -63,6 +67,12 @@ image: auth
lint: assets
@./bin/bundle exec rubocop

local:
@echo "Installing all packages ..."
@./bin/bundle install
@echo "Starting local server ..."
@./bin/rails server -p ${PORT}

publish: image
@echo Publishing image: ${REPO}:${TAG} ...
@docker push ${REPO}:${TAG} 2>&1
Expand All @@ -71,16 +81,23 @@ publish: image
realclean: clean
@rm -f ${GITHUB_TOKEN} ${BUNDLE_CFG}

run:
run: start
@if docker network inspect dnet > /dev/null 2>&1; then echo "Using docker network dnet"; else echo "Create docker network dnet"; docker network create dnet; sleep 2; fi
@docker run -p ${PORT}:3000 -e API_SERVICE_URL=${API_SERVICE_URL} --network dnet --rm --name ${SHORTNAME} ${REPO}:${TAG}

server: assets start
@export SECRET_KEY_BASE=$(./bin/rails secret)
@API_SERVICE_URL=${API_SERVICE_URL} ./bin/rails server -p ${PORT}

start:
@docker stop ${SHORTNAME} > /dev/null 2>&1 || :
@echo "Starting ${SHORTNAME} ..."
@docker run -p ${PORT}:3000 --network dnet --rm --name ${SHORTNAME} ${REPO}:${TAG}

tag:
@echo ${TAG}

test: assets
@echo "Running unit tests ..."
@./bin/rails test

vars:
Expand Down
27 changes: 17 additions & 10 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

before_action :set_locale, :change_default_caching_policy

private
protect_from_forgery with: :exception
before_action :set_locale
before_action :change_default_caching_policy

# Set the user's preferred locale. An explicit locale set via
# the URL param `lang` is preeminent, otherwise we look to the
# user's preferred language specified via browser headers
# Set the user's preferred locale. An explicit locale set via the URL param
# `lang` is preeminent, otherwise we look to the user's preferred language
# specified via browser headers
def set_locale
user_locale =
params['lang'] ||
http_accept_language.compatible_language_from(I18n.available_locales)
user_locale = params['lang']
user_locale ||= http_accept_language.compatible_language_from(I18n.available_locales)

I18n.locale = user_locale if Rails.application.config.welsh_language_enabled
end
Expand All @@ -28,4 +26,13 @@ def set_locale
def change_default_caching_policy
expires_in 5.minutes, public: true, must_revalidate: true if Rails.env.production?
end

# Notify subscriber(s) of an internal error event with the payload of the
# exception once done
# @param [Exception] exp the exception that caused the error
# @return [ActiveSupport::Notifications::Event] provides an object-oriented
# interface to the event
def instrument_internal_error(exception)
ActiveSupport::Notifications.instrument('internal_error.application', exception: exception)
end
end
4 changes: 2 additions & 2 deletions app/lib/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module Version
MAJOR = 1
MINOR = 7
REVISION = 7
MINOR = 8
REVISION = 0
SUFFIX = nil
VERSION = "#{MAJOR}.#{MINOR}.#{REVISION}#{SUFFIX && ".#{SUFFIX}"}"
end
63 changes: 63 additions & 0 deletions app/subscribers/action_dispatch_prometheus_subscriber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

# Subscribe to `*.action_controller`` events
#
# Note: This is Rails 5 specific. In Rails 6, we'd subscribe to
# `request.action_dispatch`
class ActionDispatchPrometheusSubscriber < ActiveSupport::Subscriber
attach_to :action_controller

# rubocop:disable Metrics/AbcSize
def process_action(_event)
mem = GetProcessMem.new
Prometheus::Client.registry
.get(:memory_used_mb)
.set(mem.mb)
# description: 'Thread is aborting'
Prometheus::Client.registry
.get(:process_threads)
.set(
Thread.list.select { |thread| thread.status == 'aborting' }.count,
labels: {
status: 'aborting'
}
)
# description: 'Thread is sleeping or waiting on I/O'
Prometheus::Client.registry
.get(:process_threads)
.set(
Thread.list.select { |thread| thread.status == 'sleep' }.count,
labels: {
status: 'sleep'
}
)
# description: 'Thread is executing'
Prometheus::Client.registry
.get(:process_threads)
.set(
Thread.list.select { |thread| thread.status == 'run' }.count,
labels: {
status: 'run'
}
)
# description: 'Thread is terminated normally'
Prometheus::Client.registry
.get(:process_threads)
.set(
Thread.list.select { |thread| thread.status == false }.count,
labels: {
status: 'false'
}
)
# description: 'Thread is terminated with an exception'
Prometheus::Client.registry
.get(:process_threads)
.set(
Thread.list.select { |thread| thread.status.nil? }.count,
labels: {
status: 'nil'
}
)
end
# rubocop:enable Metrics/AbcSize
end
Loading