npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

connection-spy

v0.4.0

Published

Debug Logger for Socket Connections

Downloads

1

Readme

connection-spy

Debug Logger for Socket Connections

This monkey patches the http.Agent and https.Agent default connection pools in Node.js. When connections are created or reused in a connection pool, connection-spy will register event callbacks that will trigger debug logs. Best effort is made to correlate logs (e.g. with a unique UUID) that are relevant to the same connection.

The monkeyPatch() function is responsible for adding custom behavior to http.Agent and https.Agent. In addition, the spyNewSocket() function can be used for manually created sockets, i.e. outside of a connection pool.

Example

Consider a hypothetical web service https://web.k8s.invalid:

$ dig +short web.k8s.invalid
internal-abe8a4fd143901ecb3562a21080430b6-409806529.us-east-1.elb.amazonaws.com.
10.10.18.239
10.10.29.138
10.10.33.92
10.10.58.21

As the CNAME indicates, this hypothetical web service sits behind an internal AWS ELB. What's more the ELB has the idle timeout set to 10 seconds, so that connections will be closed after 10 seconds of inactivity.

$ aws elb describe-load-balancer-attributes \
>   --load-balancer-name abe8a4fd143901ecb3562a21080430b6 \
>   | jq '.LoadBalancerAttributes.ConnectionSettings'
{
  "IdleTimeout": 10
}

Assuming connection-spy has been activated, three requests will be made in this example: the first two spaced apart by less than 10 seconds and the last two spaced aprt by more than 10 seconds so that the ELB will close the connection. We can see a Socket Connect debug log line for the first and third request and a Reuse Socket debug log line for the second request. Additionally, when the ELB closes the connection we see Socket End and Socket Close debug log lines.

{"id":"aa603c85-1591-4d10-a543-206317986a51","producer":"connection-spy","localAddress":"192.168.244.2","localPort":52238,"remoteAddress":"10.10.29.138","remotePort":443,"target":"GET https://web.k8s.invalid:443/headers?a=b","level":"debug","message":"Socket Connect","_timestamp":"2021-05-07T17:16:29.743Z"}
{"id":"aa603c85-1591-4d10-a543-206317986a51","producer":"connection-spy","localAddress":"192.168.244.2","localPort":52238,"remoteAddress":"10.10.29.138","remotePort":443,"target":"GET https://web.k8s.invalid:443/headers?a=b","level":"debug","message":"HTTP(S) Response","_timestamp":"2021-05-07T17:16:30.003Z"}
{"id":"aa603c85-1591-4d10-a543-206317986a51","producer":"connection-spy","localAddress":"192.168.244.2","localPort":52238,"remoteAddress":"10.10.29.138","remotePort":443,"target":"GET https://web.k8s.invalid:443/headers?c=d","level":"debug","message":"Reuse Socket","_timestamp":"2021-05-07T17:16:35.011Z"}
{"id":"aa603c85-1591-4d10-a543-206317986a51","producer":"connection-spy","localAddress":"192.168.244.2","localPort":52238,"remoteAddress":"10.10.29.138","remotePort":443,"target":"GET https://web.k8s.invalid:443/headers?c=d","level":"debug","message":"HTTP(S) Response","_timestamp":"2021-05-07T17:16:35.138Z"}
{"id":"aa603c85-1591-4d10-a543-206317986a51","producer":"connection-spy","localAddress":"192.168.244.2","localPort":52238,"remoteAddress":"10.10.29.138","remotePort":443,"level":"debug","message":"Socket End","_timestamp":"2021-05-07T17:16:44.924Z"}
{"id":"aa603c85-1591-4d10-a543-206317986a51","producer":"connection-spy","localAddress":null,"localPort":null,"remoteAddress":"10.10.29.138","remotePort":443,"hadError":false,"level":"debug","message":"Socket Close","_timestamp":"2021-05-07T17:16:44.926Z"}
{"id":"83b41ba4-217a-458e-8fa5-f8e5e50b184e","producer":"connection-spy","localAddress":"192.168.244.2","localPort":52246,"remoteAddress":"10.10.29.138","remotePort":443,"target":"GET https://web.k8s.invalid:443/headers?e=f","level":"debug","message":"Socket Connect","_timestamp":"2021-05-07T17:16:46.281Z"}
{"id":"83b41ba4-217a-458e-8fa5-f8e5e50b184e","producer":"connection-spy","localAddress":"192.168.244.2","localPort":52246,"remoteAddress":"10.10.29.138","remotePort":443,"target":"GET https://web.k8s.invalid:443/headers?e=f","level":"debug","message":"HTTP(S) Response","_timestamp":"2021-05-07T17:16:46.544Z"}

Expanding one of these as multiline JSON:

{
    "id": "aa603c85-1591-4d10-a543-206317986a51",
    "producer": "connection-spy",
    "localAddress": "192.168.244.2",
    "localPort": 52238,
    "remoteAddress": "10.10.29.138",
    "remotePort": 443,
    "target": "GET https://web.k8s.invalid:443/headers?a=b",
    "level": "debug",
    "message": "Socket Connect",
    "_timestamp": "2021-05-07T17:16:29.743Z"
}