ndr_browser_timings 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 854da1ea6f19263e30c9ed180150713d14297ad5a3cf55b57cf892b68568e5c2
4
+ data.tar.gz: 003323ba6b7cacaa9d88f19e81d1d1b32f41d7e74da2ed88ff2caa95a5907448
5
+ SHA512:
6
+ metadata.gz: 334be5233bd4384d709d4bf23f1650b2876a9d56ef92b1f193c24f4811d62cd80247065b6c089d99a85c6106d763ca9cca0bd7d6cda7f71335434eb3dbdcd61e
7
+ data.tar.gz: 79d87b31dd0ced5daaf2e1025c0b28d03bcb5c584d6fbf09b1bbc6b14258d65696c8e67447d082458de553377f6fe18cda2bdc4c260f50e3c4cf6d61e241060e
data/CHANGELOG.md ADDED
@@ -0,0 +1,42 @@
1
+ ## [Unreleased]
2
+ * no unreleased changes
3
+
4
+ ## 0.4.4 / 2026-09-29
5
+ ### Fixed
6
+ * Support Ruby 3.4. Drop support for Rails 7.0, Ruby 3.1
7
+ * Support Ruby 4.0, Rails 8.1. Drop support for Ruby 3.2
8
+
9
+ ## 0.4.3 / 2024-11-19
10
+ ### Added
11
+ * Support Ruby 3.2. Drop support for Ruby 2.7, Rails 6.0
12
+ * Support Ruby 3.3, Rails 7.1, 7.2 and 8.0. Drop support for Ruby 3.0, Rails 6.1
13
+
14
+ ## 0.4.2 / 2022-12-06
15
+ ### Fixed
16
+ * Drop support for Ruby 2.6
17
+ * Support Ruby 3.1, Rails 7.0
18
+ * Replace Public Health England naming with NHS Digital
19
+
20
+ ## 0.4.1 / 2022-04-08
21
+ ### Added
22
+ * Support Rails 6.1
23
+
24
+ ## 0.4.0 / 2020-07-23
25
+ ### Added
26
+ * Support sampling, to reduce data volumes (#1)
27
+
28
+ ### Fixed
29
+ * Fix typo on meta tag names: s/broser/browser/
30
+ * Stop capturing incomplete timing data from AJAX requests (#2)
31
+
32
+ ## 0.3.0 / 2020-02-11
33
+ ### Changed
34
+ * Adjust the `ndr_stats` metric name and tagging
35
+
36
+ ## 0.2.0 / 2020-01-31
37
+ ### Added
38
+ * Add belt-n-braces error handling to client library
39
+
40
+ ## 0.1.0 / 2020-01-30
41
+ ### Added
42
+ * Initial build
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at josh.pencheon@googlemail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020-2024 NHS England
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # NdrBrowserTimings [![Build Status](https://github.com/NHSDigital/ndr_browser_timings/workflows/Test/badge.svg)](https://github.com/NHSDigital/ndr_browser_timings/actions?query=workflow%3Atest) [![Gem Version](https://badge.fury.io/rb/ndr_browser_timings.svg)](https://rubygems.org/gems/ndr_browser_timings)
2
+
3
+ This Rails Engine adds JavaScript that allows for the collection of users' browser timing information.
4
+
5
+ ## Usage
6
+ Once installed (see below), timing information will be submitted from browsers
7
+ via AJAX, back to the mounted engine, and passed to any configured recorders.
8
+
9
+ Recorders can be any callable object, and can be configured like so:
10
+
11
+ ```ruby
12
+ NdrBrowserTimings.recorders << ->(timing) { MyService.notify(timing) }
13
+ ```
14
+
15
+ This gem bundles some recorders:
16
+
17
+ ```ruby
18
+ # Send info to the Rails log:
19
+ require 'ndr_browser_timings/recorders/logger'
20
+ NdrBrowserTimings.recorders << NdrBrowserTimings::Recorders::Logger.new
21
+
22
+ # Send info to prometheus:
23
+ require 'ndr_browser_timings/recorders/ndr_stats'
24
+ NdrStats.configure(...)
25
+ NdrBrowserTimings.recorders << NdrBrowserTimings::Recorders::NdrStats.new
26
+ ```
27
+
28
+ ## Installation
29
+ Add this line to your application's Gemfile:
30
+
31
+ ```ruby
32
+ gem 'ndr_browser_timings'
33
+ ```
34
+
35
+ And then execute:
36
+ ```bash
37
+ $ bundle
38
+ ```
39
+
40
+ Or install it yourself as:
41
+ ```bash
42
+ $ gem install ndr_browser_timings
43
+ ```
44
+
45
+ Inject the client library into pages you want timings submitted from:
46
+
47
+ ```javascript
48
+ //=require 'ndr_browser_timings/ndr_browser_timings'
49
+ ```
50
+
51
+ and the `<meta>` tag to provide configuration to the client library:
52
+
53
+ ```ruby
54
+ <%= ndr_browser_timings_meta_tag %>
55
+ ```
56
+
57
+ By default, all requests will be instrumented. You can alternatively elect to only
58
+ sample a fraction of requests, to reduce data collected:
59
+
60
+ ```ruby
61
+ <%= ndr_browser_timings_meta_tag(sample_rate: 0.1) %>
62
+ ```
63
+
64
+ This is an isolated engine, so you'll need to configure with an authentication check:
65
+
66
+ ```ruby
67
+ # Allow all requests through (careful!):
68
+ NdrBrowserTimings.check_current_user_authentication = ->(request) { true }
69
+ ```
70
+
71
+ ## License
72
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
73
+
74
+ ## Code of Conduct
75
+ Everyone interacting in the NdrBrowserTimings project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the `CODE_OF_CONDUCT.md`.
76
+
77
+ ## TODO:
78
+ * javascript fallback
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'NdrBrowserTimings'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
20
+ load 'rails/tasks/engine.rake'
21
+
22
+ load 'rails/tasks/statistics.rake'
23
+
24
+ require 'bundler/gem_tasks'
25
+
26
+ require 'rake/testtask'
27
+ require 'ndr_dev_support/tasks'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'test'
31
+ t.pattern = 'test/**/*_test.rb'
32
+ t.verbose = false
33
+ end
34
+
35
+ task default: :test
data/SECURITY.md ADDED
@@ -0,0 +1,35 @@
1
+ # Security
2
+
3
+ NHS England takes security and the protection of private data extremely seriously. If you believe you have found a vulnerability or other issue which has compromised or could compromise the security of any of our systems and/or private data managed by our systems, please do not hesitate to contact us using the methods outlined below.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Security](#security)
8
+ - [Table of Contents](#table-of-contents)
9
+ - [Reporting a vulnerability](#reporting-a-vulnerability)
10
+ - [Email](#email)
11
+ - [NCSC](#ncsc)
12
+ - [General Security Enquiries](#general-security-enquiries)
13
+
14
+ ## Reporting a vulnerability
15
+
16
+ Please note, email is our preferred method of receiving reports.
17
+
18
+ ### Email
19
+
20
+ If you wish to notify us of a vulnerability via email, please include detailed information on the nature of the vulnerability and any steps required to reproduce it.
21
+
22
+ You can reach us at:
23
+
24
+ - [Brian.Shand@nhs.net](mailto:Brian.Shand@nhs.net)
25
+ - [cybersecurity@nhs.net](mailto:cybersecurity@nhs.net)
26
+
27
+ ### NCSC
28
+
29
+ You can send your report to the National Cyber Security Centre, who will assess your report and pass it on to NHS England if necessary.
30
+
31
+ You can report vulnerabilities here: [https://www.ncsc.gov.uk/information/vulnerability-reporting](https://www.ncsc.gov.uk/information/vulnerability-reporting)
32
+
33
+ ## General Security Enquiries
34
+
35
+ If you have general enquiries regarding our cybersecurity, please reach out to us at [cybersecurity@nhs.net](mailto:cybersecurity@nhs.net)
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/ndr_browser_timings .css
File without changes
@@ -0,0 +1,84 @@
1
+ /* global XMLHttpRequest */
2
+
3
+ (function () {
4
+ function NdrBrowserTimings () {
5
+ this.bindListeners = function () {
6
+ window.addEventListener('load', function () {
7
+ // Defer the timing collection in order to allow the onLoad event to finish first.
8
+ setTimeout(this.sendPerformanceTimingData, 0)
9
+
10
+ // Periodically, send timing for AJAX requests:
11
+ this.intervalId = setInterval(this.sendNewPerformanceResourceTimingData, 1000)
12
+ }.bind(this))
13
+ }
14
+
15
+ this.sendPerformanceTimingData = function () {
16
+ try {
17
+ if (this.shouldSample()) {
18
+ this.sendTimingData({
19
+ pathname: window.location.pathname,
20
+ performance_timing: window.performance.timing
21
+ })
22
+ }
23
+ } catch (_err) { }
24
+ }
25
+
26
+ this.sendNewPerformanceResourceTimingData = function () {
27
+ try {
28
+ var newEntries = window.performance.getEntriesByType('resource')
29
+ .filter(function (entry) { return entry.responseEnd }) // drop those still in progress
30
+ .filter(function (entry) { return !~this.recordedEntries.indexOf(JSON.stringify(entry)) }.bind(this))
31
+ .filter(function (entry) { return !~entry.name.indexOf(this.endpoint) }.bind(this))
32
+ .filter(function (entry) { return this.shouldSample() }.bind(this))
33
+
34
+ if (newEntries.length) {
35
+ this.sendTimingData({ resource_timings: newEntries })
36
+
37
+ // IE11 can't compare the timing objects directly; compare stringified versions instead...
38
+ newEntries = newEntries.map(function (entry) { return JSON.stringify(entry) })
39
+ this.recordedEntries = this.recordedEntries.concat(newEntries)
40
+ }
41
+ } catch (_err) {
42
+ this.abort()
43
+ }
44
+ }
45
+
46
+ this.sendTimingData = function (data) {
47
+ var request = new XMLHttpRequest()
48
+ var token = this.readMetaTag('csrf-token')
49
+
50
+ data.user_agent = navigator.userAgent
51
+
52
+ request.open('POST', this.endpoint)
53
+ if (token) request.setRequestHeader('X-CSRF-Token', token)
54
+ request.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')
55
+ request.send(JSON.stringify(data))
56
+ }
57
+
58
+ this.readMetaTag = function (name) {
59
+ var metaTag = document.querySelector('meta[name="' + name + '"]')
60
+ return metaTag && metaTag.content
61
+ }
62
+
63
+ this.abort = function () {
64
+ if (this.intervalId) clearInterval(this.intervalId)
65
+ }
66
+
67
+ this.shouldSample = function () {
68
+ return this.sampleRate > Math.random()
69
+ }
70
+
71
+ // resource timings that have been sent:
72
+ this.recordedEntries = []
73
+
74
+ // Path to which data is sent:
75
+ this.endpoint = this.readMetaTag('ndr_browser_timings_endpoint')
76
+ // What percentage of requests should we record data for?
77
+ this.sampleRate = parseFloat(this.readMetaTag('ndr_browser_timings_sample_rate') || 1)
78
+
79
+ this.intervalId = null
80
+ if (this.endpoint) this.bindListeners()
81
+ }
82
+
83
+ NdrBrowserTimings()
84
+ }).call(this)
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NdrBrowserTimings
4
+ class ApplicationController < ActionController::Base
5
+ protect_from_forgery with: :exception
6
+
7
+ before_action :check_authentication
8
+
9
+ private
10
+
11
+ def check_authentication
12
+ return if NdrBrowserTimings.check_current_user_authentication.call(self)
13
+
14
+ head :unauthorized
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ require_dependency 'ndr_browser_timings/application_controller'
2
+
3
+ module NdrBrowserTimings
4
+ # controller for receiving timings sent from clients' browsers.
5
+ class TimingsController < ApplicationController
6
+ # POST "/"
7
+ def receive
8
+ if params.key?(:performance_timing)
9
+ source = source_route(params[:pathname])
10
+ timing = PerformanceTiming.new(source.merge(performance_timing))
11
+ NdrBrowserTimings.record(timing)
12
+ elsif params.key?(:resource_timings)
13
+ resource_timings.each do |data|
14
+ source = source_route(data.delete('name'))
15
+ timing = ResourceTiming.new(source.merge(data))
16
+ NdrBrowserTimings.record(timing)
17
+ end
18
+ end
19
+
20
+ head :no_content
21
+ end
22
+
23
+ private
24
+
25
+ def resource_timings
26
+ params.require(:resource_timings).map(&:permit!)
27
+ end
28
+
29
+ def performance_timing
30
+ params.require(:performance_timing).permit!
31
+ end
32
+
33
+ def source_route(path)
34
+ Rails.application.routes.recognize_path(path)
35
+ rescue ActionController::RoutingError
36
+ {}
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NdrBrowserTimings
4
+ # This helper file is injected into the host application.
5
+ module MetaTagHelper
6
+ # Allow engine mount location to be made available to javascript
7
+ # included by the host application. The alternative would be to
8
+ # server pre-configured JS through the engine directly, rather than
9
+ # as an asset.
10
+ def ndr_browser_timings_meta_tag(sample_rate: 1)
11
+ tag('meta', name: 'ndr_browser_timings_endpoint', content: ndr_browser_timings.receive_path) +
12
+ tag('meta', name: 'ndr_browser_timings_sample_rate', content: sample_rate.to_f)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ module NdrBrowserTimings
2
+ module TimingsHelper
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NdrBrowserTimings
4
+ class ApplicationJob < ActiveJob::Base
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NdrBrowserTimings
4
+ class ApplicationMailer < ActionMailer::Base
5
+ default from: 'from@example.com'
6
+ layout 'mailer'
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NdrBrowserTimings
4
+ class ApplicationRecord < ActiveRecord::Base
5
+ self.abstract_class = true
6
+ end
7
+ end
@@ -0,0 +1,58 @@
1
+ module NdrBrowserTimings
2
+ # Wraps window.performance.timing objects
3
+ class PerformanceTiming
4
+ include ActiveModel::Model
5
+
6
+ # As defined by https://www.w3.org/TR/navigation-timing/#sec-navigation-timing
7
+ TIMING_FIELDS = %w[
8
+ navigationStart unloadEventStart unloadEventEnd redirectStart redirectEnd fetchStart
9
+ domainLookupStart domainLookupEnd connectStart connectEnd secureConnectionStart
10
+ requestStart responseStart responseEnd domLoading domInteractive domContentLoadedEventStart
11
+ domContentLoadedEventEnd domComplete loadEventStart loadEventEnd
12
+ ].freeze
13
+
14
+ attr_accessor :controller, :action
15
+
16
+ TIMING_FIELDS.each do |field|
17
+ attr_accessor field.underscore
18
+ alias_attribute field, field.underscore if field != field.underscore
19
+ end
20
+
21
+ def tags
22
+ { controller: controller, action: action }
23
+ end
24
+
25
+ def recordable?
26
+ tags.values.all?(&:present?)
27
+ end
28
+
29
+ def timeline
30
+ @timeline ||= {
31
+ unload: diff(unload_event_end, unload_event_start),
32
+ redirect: diff(redirect_end, redirect_start),
33
+ dns: diff(domain_lookup_end, domain_lookup_start),
34
+ connect: diff(connect_end, connect_start),
35
+ request: diff(response_start, request_start),
36
+ response: diff(response_end, response_start),
37
+ dom_loading: diff(dom_loading, response_start),
38
+ dom_interactive: diff(dom_interactive, dom_loading),
39
+ dom_content_loaded_event: diff(dom_content_loaded_event_end, dom_content_loaded_event_start),
40
+ dom_complete: diff(dom_complete, dom_content_loaded_event_end),
41
+ load_event: diff(load_event_end, load_event_start)
42
+ }
43
+ end
44
+
45
+ private
46
+
47
+ # Some browsers will report funky extra values; drop them.
48
+ def sanitize_for_mass_assignment(attributes)
49
+ super.reject { |attr, _val| !respond_to?("#{attr}=") }
50
+ end
51
+
52
+ def diff(end_time, start_time)
53
+ return 0 unless start_time && end_time
54
+
55
+ (end_time.to_i - start_time.to_i).round
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,17 @@
1
+ module NdrBrowserTimings
2
+ # Wraps window.performance.getEntry() objects
3
+ #
4
+ # These are similar to, but not the same as, the data structure
5
+ # returned from the primary navigation.
6
+ class ResourceTiming < PerformanceTiming
7
+ # As defined by https://www.w3.org/TR/resource-timing-2/#sec-performanceresourcetiming
8
+ RESOURCE_FIELDS = %w[
9
+ initiatorType nextHopProtocol workerStart transferSize encodedBodySize decodedBodySize
10
+ ].freeze
11
+
12
+ RESOURCE_FIELDS.each do |field|
13
+ attr_accessor field.underscore
14
+ alias_attribute field, field.underscore if field != field.underscore
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Ndr browser timings</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "ndr_browser_timings/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ NdrBrowserTimings::Engine.routes.draw do
4
+ post '/', to: 'timings#receive', as: 'receive'
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NdrBrowserTimings
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace NdrBrowserTimings
6
+
7
+ initializer 'ndr_browser_timings.inject_helpers' do |app|
8
+ app.config.to_prepare do
9
+ ::ApplicationHelper.include(MetaTagHelper)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module NdrBrowserTimings
2
+ module Recorders
3
+ # Allows timing information to be recorded via the application log.
4
+ class Logger
5
+ def call(timing)
6
+ # Ensure consistent log format, even though Hash#to_s changed in ruby 3.4
7
+ format_hash = lambda do |hash|
8
+ format('{%<entries>s}',
9
+ entries: hash.collect { |k, v| "#{k.inspect}=>#{v.inspect}" }.join(', '))
10
+ end
11
+ Rails.logger.info "NdrBrowserTimings #{format_hash.call(timing.tags)} #{format_hash.call(timing.timeline)}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ begin
2
+ require 'ndr_stats'
3
+ rescue LoadError => e
4
+ raise e, <<~MESSAGE
5
+ The NdrStats recorder requires the ndr_stats library to be loaded.
6
+ As an optional codepath it is not an explicit dependency of
7
+ ndr_browser_timings, so you must add it to your Gemfile separately.
8
+ MESSAGE
9
+ end
10
+
11
+ module NdrBrowserTimings
12
+ module Recorders
13
+ # Allows timing information to be recorded via ndr_stats.
14
+ class NdrStats
15
+ def initialize(raise_unless_configured: true)
16
+ raise 'ndr_stats is not configured!' if raise_unless_configured && !::NdrStats.configured?
17
+ end
18
+
19
+ def call(timing)
20
+ tags = timing.tags
21
+
22
+ timing.timeline.each do |key, duration|
23
+ ::NdrStats.timing(:browser_timing, duration, **tags, type: key)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NdrBrowserTimings
4
+ VERSION = '0.4.4'
5
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ndr_browser_timings/engine'
4
+
5
+ # NdrBrowserTimings is a Rails engine that allows clients' browsers
6
+ # timing information to be sent back to the application and captured.
7
+ module NdrBrowserTimings
8
+ # Callable object (called with controller context) that is used to check
9
+ # if the current user is authenticated with the host app.
10
+ mattr_accessor :check_current_user_authentication
11
+ self.check_current_user_authentication = nil # Must be configured by the host app
12
+
13
+ # Recorders are callable objects that receiving timings
14
+ mattr_accessor :recorders
15
+ self.recorders = []
16
+
17
+ class << self
18
+ def record(timing)
19
+ return unless timing.recordable?
20
+
21
+ recorders.each { |recorder| recorder.call(timing) }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # desc "Explaining what the task does"
3
+ # task :ndr_browser_timings do
4
+ # # Task goes here
5
+ # end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ndr_browser_timings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.4
5
+ platform: ruby
6
+ authors:
7
+ - NDR Development Team
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.1'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '8.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '7.1'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '8.2'
32
+ - !ruby/object:Gem::Dependency
33
+ name: mocha
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: ndr_dev_support
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '6.0'
53
+ type: :development
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '6.0'
60
+ - !ruby/object:Gem::Dependency
61
+ name: ndr_stats
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ - !ruby/object:Gem::Dependency
75
+ name: puma
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ type: :development
82
+ prerelease: false
83
+ version_requirements: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ - !ruby/object:Gem::Dependency
89
+ name: sprockets-rails
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 3.0.0
95
+ type: :development
96
+ prerelease: false
97
+ version_requirements: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: 3.0.0
102
+ - !ruby/object:Gem::Dependency
103
+ name: sqlite3
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ type: :development
110
+ prerelease: false
111
+ version_requirements: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - CHANGELOG.md
121
+ - CODE_OF_CONDUCT.md
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - SECURITY.md
126
+ - app/assets/config/ndr_browser_timings_manifest.js
127
+ - app/assets/images/ndr_browser_timings/.keep
128
+ - app/assets/javascripts/ndr_browser_timings/ndr_browser_timings.js
129
+ - app/assets/stylesheets/ndr_browser_timings/application.css
130
+ - app/controllers/ndr_browser_timings/application_controller.rb
131
+ - app/controllers/ndr_browser_timings/timings_controller.rb
132
+ - app/helpers/ndr_browser_timings/meta_tag_helper.rb
133
+ - app/helpers/ndr_browser_timings/timings_helper.rb
134
+ - app/jobs/ndr_browser_timings/application_job.rb
135
+ - app/mailers/ndr_browser_timings/application_mailer.rb
136
+ - app/models/ndr_browser_timings/application_record.rb
137
+ - app/models/ndr_browser_timings/performance_timing.rb
138
+ - app/models/ndr_browser_timings/resource_timing.rb
139
+ - app/views/layouts/ndr_browser_timings/application.html.erb
140
+ - config/routes.rb
141
+ - lib/ndr_browser_timings.rb
142
+ - lib/ndr_browser_timings/engine.rb
143
+ - lib/ndr_browser_timings/recorders/logger.rb
144
+ - lib/ndr_browser_timings/recorders/ndr_stats.rb
145
+ - lib/ndr_browser_timings/version.rb
146
+ - lib/tasks/ndr_browser_timings_tasks.rake
147
+ licenses: []
148
+ metadata: {}
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '3.3'
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubygems_version: 3.6.9
164
+ specification_version: 4
165
+ summary: Capture request timing data from clients
166
+ test_files: []