Skip to content

Commit 70e4306

Browse files
committed
Initial commit
0 parents  commit 70e4306

25 files changed

+972
-0
lines changed

.github/workflows/main.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: build
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
8+
runs-on: ubuntu-20.04
9+
10+
strategy:
11+
matrix:
12+
ruby-version:
13+
- 3.0.0
14+
- 2.7.2
15+
- 2.6.6
16+
- 2.5.8
17+
- 2.4.10
18+
- 2.3.8
19+
- 2.2.10
20+
- jruby-9.2.14.0
21+
- truffleruby-21.0.0
22+
23+
steps:
24+
- uses: actions/checkout@v2
25+
26+
- name: Set up Ruby ${{ matrix.ruby-version }}
27+
uses: ruby/setup-ruby@v1
28+
with:
29+
ruby-version: ${{ matrix.ruby-version }}
30+
bundler-cache: true
31+
32+
- name: Run tests
33+
run: bundle exec rspec --format documentation

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/reports/
8+
/tmp/
9+
10+
Gemfile.lock
11+
12+
# rspec failure tracking
13+
.rspec_status
14+
15+
# Ignore gemset
16+
/.gem

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source "https://rubygems.org"
2+
3+
gemspec

LICENSE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# License
2+
3+
Copyright (c) 2021, Logtail
4+
5+
Permission to use, copy, modify, and/or distribute this software for any purpose
6+
with or without fee is hereby granted, provided that the above copyright notice
7+
and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
13+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
15+
THIS SOFTWARE.
16+

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 🪵 Logtail Integration For Rails
2+
3+
[![ISC License](https://img.shields.io/badge/license-ISC-ff69b4.svg)](LICENSE.md)
4+
[![Build Status](https://github.com/logtail/logtail-ruby-rack/workflows/build/badge.svg)](https://github.com/logtail/logtail-ruby-rack/actions?query=workflow%3Abuild)
5+
6+
This library integrates the [`logtail` Ruby library](https://github.com/logtail/logtail-ruby) with the [rack](https://github.com/rack/rack) framework,
7+
turning your rails logs into rich structured events.
8+
9+
* **Sign-up: [https://logtail.com](https://logtail.com)**

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
6+
task :default => :spec

lib/logtail-rack.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require "rack"
2+
require "logtail"
3+
require "logtail-rack/config"
4+
require "logtail-rack/error_event"
5+
require "logtail-rack/http_context"
6+
require "logtail-rack/http_events"
7+
require "logtail-rack/session_context"
8+
require "logtail-rack/user_context"
9+
10+
module Logtail
11+
module Integrations
12+
module Rack
13+
end
14+
end
15+
end
16+

lib/logtail-rack/config.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
require "logtail"
2+
3+
Logtail::Config.instance.define_singleton_method(:logrageify!) do
4+
integrations.rack.http_events.collapse_into_single_event = true
5+
end
6+
7+
module Logtail
8+
class Config
9+
module Integrations
10+
extend self
11+
# Convenience module for accessing the various `Logtail::Integrations::Rack::*` classes
12+
# through the {Logtail::Config} object. Logtail couples configuration with the class
13+
# responsibls for implementing it. This provides for a tighter design, but also
14+
# requires the user to understand and access the various classes. This module aims
15+
# to provide a simple ruby-like configuration interface for internal Logtail classes.
16+
#
17+
# For example:
18+
#
19+
# config = Logtail::Config.instance
20+
# config.integrations.rack.http_events.enabled = false
21+
def rack
22+
Rack
23+
end
24+
25+
module Rack
26+
extend self
27+
28+
# Convenience method for accessing the {Logtail::Integrations::Rack::ErrorEvent}
29+
# middleware class specific configuration. See {Logtail::Integrations::Rack::ExceptionEvent}
30+
# for a list of methods available.
31+
#
32+
# @example
33+
# config = Logtail::Config.instance
34+
# config.integrations.rack.error_event.enabled = false
35+
def error_event
36+
Logtail::Integrations::Rack::ErrorEvent
37+
end
38+
39+
# Convenience method for accessing the {Logtail::Integrations::Rack::HTTPContext}
40+
# middleware class specific configuration. See {Logtail::Integrations::Rack::HTTPContext}
41+
# for a list of methods available.
42+
#
43+
# @example
44+
# config = Logtail::Config.instance
45+
# config.integrations.rack.http_context.enabled = false
46+
def http_context
47+
Logtail::Integrations::Rack::HTTPContext
48+
end
49+
50+
# Convenience method for accessing the {Logtail::Integrations::Rack::HTTPEvents}
51+
# middleware class specific configuration. See {Logtail::Integrations::Rack::HTTPEvents}
52+
# for a list of methods available.
53+
#
54+
# @example
55+
# config = Logtail::Config.instance
56+
# config.integrations.rack.http_events.enabled = false
57+
def http_events
58+
Logtail::Integrations::Rack::HTTPEvents
59+
end
60+
61+
# Convenience method for accessing the {Logtail::Integrations::Rack::SessionContext}
62+
# middleware class specific configuration. See {Logtail::Integrations::Rack::SessionContext}
63+
# for a list of methods available.
64+
#
65+
# @example
66+
# config = Logtail::Config.instance
67+
# config.integrations.rack.session_context.enabled = false
68+
def session_context
69+
Logtail::Integrations::Rack::SessionContext
70+
end
71+
72+
# Convenience method for accessing the {Logtail::Integrations::Rack::UserContext}
73+
# middleware class specific configuration. See {Logtail::Integrations::Rack::UserContext}
74+
# for a list of methods available.
75+
#
76+
# @example
77+
# config = Logtail::Config.instance
78+
# config.integrations.rack.user_context.enabled = false
79+
def user_context
80+
Logtail::Integrations::Rack::UserContext
81+
end
82+
end
83+
end
84+
end
85+
end

lib/logtail-rack/error_event.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "logtail/config"
2+
require "logtail/events/error"
3+
require "logtail-rack/middleware"
4+
5+
module Logtail
6+
module Integrations
7+
module Rack
8+
# A Rack middleware that is reponsible for capturing exception and error events
9+
class ErrorEvent < Middleware
10+
def call(env)
11+
begin
12+
status, headers, body = @app.call(env)
13+
rescue Exception => exception
14+
Config.instance.logger.fatal do
15+
Events::Error.new(
16+
name: exception.class.name,
17+
error_message: exception.message,
18+
backtrace: exception.backtrace
19+
)
20+
end
21+
22+
raise exception
23+
end
24+
end
25+
end
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)