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

Add logging and instrumentation via features #499

Merged
merged 5 commits into from
Aug 23, 2018

Conversation

paul
Copy link
Contributor

@paul paul commented Aug 16, 2018

Adds logging and AS::Notifications-compatible instrumentation as separate features.

Building upon the features API introduced in #482, this adds a means to register new custom features:

HTTP::Options.register_feature(:my_feature, MyFeature)
HTTP.use(:my_feature).get("https://example.com/")

Also adds two new features, Logging and Instrumentation.

>> HTTP.use(logging: {logger: Logger.new(STDOUT)}).get("https://httpbin.org/get")
I, [2018-08-16T16:33:58.261195 #1538]  INFO -- : > GET https://httpbin.org/get
D, [2018-08-16T16:33:58.261256 #1538] DEBUG -- : Connection: close
Host: httpbin.org
User-Agent: http.rb/4.0.0.dev


I, [2018-08-16T16:33:58.487195 #1538]  INFO -- : < 200 OK
#<HTTP::Response/1.1 200 OK {"Connection"=>"close", "Server"=>"gunicorn/19.9.0", "Date"=>"Thu, 16 Aug 2018 22:33:58 GMT", "Content-Type"=>"application/json", "Content-Length"=>"201", "Access-Control-Allow-Origin"=>"*", "Access-Control-Allow-Credentials"=>"true", "Via"=>"1.1 vegur"}>
D, [2018-08-16T16:33:58.487440 #1538] DEBUG -- : Connection: close
Server: gunicorn/19.9.0
Date: Thu, 16 Aug 2018 22:33:58 GMT
Content-Type: application/json
Content-Length: 201
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: 1.1 vegur

{
  "args": {}, 
  "headers": {
    "Connection": "close", 
    "Host": "httpbin.org", 
    "User-Agent": "http.rb/4.0.0.dev"
  }, 
  "origin": "204.188.102.186", 
  "url": "https://httpbin.org/get"
}
>> HTTP.use(:instrumentation, instrumenter: ActiveSupport::Notifications, namespace: "my_http") # default namespace "http"
# => Emits a `"request.my_http"` event for the request/response

@paul paul force-pushed the feature/instrumentation branch from 3266154 to 2f95c5d Compare August 17, 2018 16:35
@paul paul force-pushed the feature/instrumentation branch from 2f95c5d to 0e09e4f Compare August 17, 2018 17:04
@paul paul force-pushed the feature/instrumentation branch from d030725 to d139773 Compare August 17, 2018 17:23
Copy link
Member

@tarcieri tarcieri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@paul
Copy link
Contributor Author

paul commented Aug 17, 2018

One of the jruby builds randomly failed, but it seems I don't have permission to restart it, and I don't want to commit an empty commit to poke it.

@tarcieri
Copy link
Member

tarcieri commented Aug 17, 2018 via email

@ixti
Copy link
Member

ixti commented Aug 18, 2018

IMO first use should have API:

def use(feature_id, **options); end

@paul
Copy link
Contributor Author

paul commented Aug 18, 2018

@ixti Perhaps, but this PR didn't really touch that code. Want me to make that change as well?

@ixti
Copy link
Member

ixti commented Aug 18, 2018

@paul yes please

@ixti
Copy link
Member

ixti commented Aug 18, 2018

in fact i'm not sure :)) i just find HTTP.use(instrumentation: ActiveSupport::Notifications, namespace: "my_http") a bit confusing (seems like 2 features to me being asked to be used)

@ixti
Copy link
Member

ixti commented Aug 18, 2018

Let me think - will speak out tomorrow :D

@paul
Copy link
Contributor Author

paul commented Aug 19, 2018

Well, I think it would instead be use(:instrumentation, instrumenter: AS::Notifications, namespace: "my_http"). That API doesn't allow multiple features to be used in one call (use(:auto_inflate, :auto_deflate), so it'd be an API-breaking change, unless we do something like:

def features=(*features, **options)
  @features = if features.size > 1 && !options.empty?
    # what here? assign options to every feature?
  elsif !options.empty?
    feature = features.first
    [feature.new(**options)]
  else
    features.map(&:new)
  end
end

@tarcieri
Copy link
Member

tarcieri commented Aug 19, 2018

What about:

use(:instrumentation)
use(instrumentation: { instrumenter: AS::Notifications, namespace: "my_http" })
use(:auto_inflate, :auto_deflate, instrumentation: { instrumenter: AS::Notifications, namespace: "my_http" })
use(:auto_inflate, instrumentation: { instrumenter: AS::Notifications, namespace: "my_http" }, :auto_deflate)

Essentially allow both Symbol and Hash as arguments, where Hash is expected to have one key which is a Symbol whose value is also a Hash (which is parsed into the name and the options hash)

@paul
Copy link
Contributor Author

paul commented Aug 19, 2018

@tarcieri unless I'm mistaken, that's how it works now?

@tarcieri
Copy link
Member

@paul haha okay, so why not just keep it that way?

@paul
Copy link
Contributor Author

paul commented Aug 19, 2018

Oh, i see, I screwed up my example for it in the PR. No wonder I was confused.

@paul
Copy link
Contributor Author

paul commented Aug 21, 2018

@ixti I had messed up the example I used in the description of this PR, so maybe that was your concern? This PR doesn't change the signature of .use at all, and I fixed the example.

Once this gets merged, I'll write a wiki page to document how to use the features feature.

@ixti
Copy link
Member

ixti commented Aug 21, 2018

Yeah. I guess I was confused.

Let's keep it as is.

@paul
Copy link
Contributor Author

paul commented Aug 23, 2018

@ixti @tarcieri I think this is ready to be merged, then? This PR didn't actually change how #use worked.

Any chance of getting a release soon? The original feature stuff I did back in June isn't in the actual released gem yet.

@tarcieri tarcieri merged commit 7c2a861 into httprb:master Aug 23, 2018
@tarcieri
Copy link
Member

:shipit:

@ixti
Copy link
Member

ixti commented Aug 23, 2018

@paul Yeah we have some backward incompatible changes right now in the master. I will check if we can extract somethign to release as 3.4.0 without introducing backward incompatible changes meanwhile. Just give me couple of weeks (on my vacation right now) and I will prepare and push the release.

@mateusluizfb
Copy link

There's any date to when this feature is gonna be available in the stable version?

@tarcieri
Copy link
Member

@mateusluizfb if @ixti doesn't have time, I can do a release

@ixti
Copy link
Member

ixti commented Oct 12, 2018

Yeah. Sorry took me so long to get back in a shell. Will do release this weekend.

@ixti
Copy link
Member

ixti commented Oct 14, 2018

I have released current master as v4.0.0. Was hoping to sneak more breaking changes into major bump, but then realised that they will go to next major easily. :D

@tarcieri
Copy link
Member

Thanks @ixti!

Was hoping to sneak more breaking changes into major bump, but then realised that they will go to next major easily. :D

The joys of semantic versioning!

@khataev
Copy link

khataev commented Mar 20, 2019

Could you update the readme? Luckily found this PR, cause readme/wiki has no information about this feature

@paul
Copy link
Contributor Author

paul commented Mar 20, 2019

@khataev I had updating the wiki on my TODO when I finished this PR, I guess it never happened. Sorry about that. I've added a page to the wiki, hopefully it helps: https://github.com/httprb/http/wiki/Logging-and-Instrumentation

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Jun 7, 2020
Update ruby-http to 4.4.1.


## 4.4.1 (2020-03-29)

* Backport [#590](httprb/http#590)
  Fix parser failing on some edge cases.
  ([@ixti])

## 4.4.0 (2020-03-25)

* Backport [#587](httprb/http#587)
  Fix redirections when server responds with multiple Location headers.
  ([@ixti])

* Backport [#599](httprb/http#599)
  Allow passing HTTP::FormData::{Multipart,UrlEncoded} object directly.
  ([@ixti])

## 4.3.0 (2020-01-09)

* Backport [#581](httprb/http#581)
  Add Ruby-2.7 compatibility.
  ([@ixti], [@janko])


## 4.2.0 (2019-10-22)

* Backport [#489](httprb/http#489)
  Fix HTTP parser.
  ([@ixti], [@fxposter])


## 4.1.1 (2019-03-12)

* Add `HTTP::Headers::ACCEPT_ENCODING` constant.
  ([@ixti])


## 4.1.0 (2019-03-11)

* [#533](httprb/http#533)
  Add URI normalizer feature that allows to swap default URI normalizer.
  ([@mamoonraja])


## 4.0.5 (2019-02-15)

* Backport [#532](httprb/http#532) from master.
  Fix pipes support in request bodies.
  ([@ixti])


## 4.0.4 (2019-02-12)

* Backport [#506](httprb/http#506) from master.
  Skip auto-deflate when there is no body.
  ([@Bonias])


## 4.0.3 (2019-01-18)

* Fix missing URL in response wrapped by auto inflate.
  ([@ixti])

* Provide `HTTP::Request#inspect` method for debugging purposes.
  ([@ixti])


## 4.0.2 (2019-01-15)

* [#506](httprb/http#506)
  Fix instrumentation feature.
  ([@paul])


## 4.0.1 (2019-01-14)

* [#515](httprb/http#515)
  Fix `#build_request` and `#request` to respect default options.
  ([@RickCSong])


## 4.0.0 (2018-10-15)

* [#482](httprb/http#482)
  [#499](httprb/http#499)
  Introduce new features injection API with 2 new feaures: instrumentation
  (compatible with ActiveSupport::Notification) and logging.
  ([@paul])

* [#473](httprb/http#473)
  Handle early responses.
  ([@janko-m])

* [#468](httprb/http#468)
  Rewind `HTTP::Request::Body#source` once `#each` is complete.
  ([@ixti])

* [#467](httprb/http#467)
  Drop Ruby 2.2 support.
  ([@ixti])

* [#436](httprb/http#436)
  Raise ConnectionError when writing to socket fails.
  ([@janko-m])

* [#438](httprb/http#438)
  Expose `HTTP::Request::Body#source`.
  ([@janko-m])

* [#446](httprb/http#446)
  Simplify setting a timeout.
  ([@mikegee])

* [#451](httprb/http#451)
  Reduce memory usage when reading response body.
  ([@janko-m])

* [#458](httprb/http#458)
  Extract HTTP::Client#build_request method.
  ([@tycoon])

* [#462](httprb/http#462)
  Fix HTTP::Request#headline to allow two leading slashes in path.
  ([@scarfacedeb])

* [#454](httprb/http#454)
  [#464](httprb/http#464)
  [#384](httprb/http#384)
  Fix #readpartial not respecting max length argument.
  ([@janko-m], [@marshall-lee])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants