|
| 1 | +require "../instrument" |
| 2 | + |
| 3 | +# # OpenTelemetry::Instrumentation::CrystalHttpClient |
| 4 | +# |
| 5 | +# ### Instruments |
| 6 | +# * First::Class::Instrumented |
| 7 | +# * Second::Class::Instrumented |
| 8 | +# |
| 9 | +# ### Reference: [https://crystal-lang.org/api/1.4.1/HTTP/Client.html](https://crystal-lang.org/api/1.4.1/HTTP/Client.html) |
| 10 | +# |
| 11 | +# Description of the instrumentation provided, including any nuances, caveats, instructions, or warnings. |
| 12 | +# |
| 13 | +# ## Methods Affected |
| 14 | +# |
| 15 | +# * First::Class#method_name |
| 16 | +# |
| 17 | +struct OpenTelemetry::InstrumentationDocumentation::CrystalHttpClient |
| 18 | +end |
| 19 | + |
| 20 | +# This allows opt-out of specific instrumentation at compile time, via environment variables. |
| 21 | +# Refer to https://wyhaines.github.io/defined.cr/ for details about all supported check types. |
| 22 | +unless_enabled?("OTEL_CRYSTAL_DISABLE_INSTRUMENTATION_HTTP_CLIENT") do |
| 23 | + # Next should be one or more checks intended to validate that the class(es) to be instrumented |
| 24 | + # actually exist. It should be possible to require all instrumentation, regardless of whether |
| 25 | + # a given class/package is actually used, as the instrumentation should not attempt to install |
| 26 | + # itself if that installation will fail. |
| 27 | + if_defined?(HTTP::Client) do |
| 28 | + # This exists to record the instrumentation in the OpenTelemetry::Instrumentation::Registry, |
| 29 | + # which may be used by other code/tools to introspect the installed instrumentation. |
| 30 | + module OpenTelemetry::Instrumentation |
| 31 | + class CrystalHttpClient < OpenTelemetry::Instrumentation::Instrument |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + if_version?(Crystal, :>=, "1.0.0") do |
| 36 | + # TODO: Offer these refactors as a PR to core Crystal. |
| 37 | + class HTTP::Client |
| 38 | + private def io |
| 39 | + io = @io |
| 40 | + return io if io |
| 41 | + unless @reconnect |
| 42 | + raise "This HTTP::Client cannot be reconnected" |
| 43 | + end |
| 44 | + |
| 45 | + do_connect |
| 46 | + end |
| 47 | + |
| 48 | + def do_connect |
| 49 | + hostname = @host.starts_with?('[') && @host.ends_with?(']') ? @host[1..-2] : @host |
| 50 | + io = TCPSocket.new hostname, @port, @dns_timeout, @connect_timeout |
| 51 | + io.read_timeout = @read_timeout if @read_timeout |
| 52 | + io.write_timeout = @write_timeout if @write_timeout |
| 53 | + io.sync = false |
| 54 | + |
| 55 | + {% if !flag?(:without_openssl) %} |
| 56 | + io = do_connect_ssl(io) |
| 57 | + {% end %} |
| 58 | + |
| 59 | + @io = io |
| 60 | + end |
| 61 | + |
| 62 | + def do_connect_ssl(io) |
| 63 | + if tls = @tls |
| 64 | + tcp_socket = io |
| 65 | + begin |
| 66 | + io = OpenSSL::SSL::Socket::Client.new(tcp_socket, context: tls, sync_close: true, hostname: @host) |
| 67 | + rescue exc |
| 68 | + # don't leak the TCP socket when the SSL connection failed |
| 69 | + tcp_socket.close |
| 70 | + raise exc |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + io |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + # #### Actual Instrumentation Here. This way, if the above gets accepted into Crystal |
| 79 | + # #### core, we just need to delete everything above here and we are good to go. |
| 80 | + |
| 81 | + class HTTP::Client |
| 82 | + trace("do_connect") do |
| 83 | + trace = OpenTelemetry.trace |
| 84 | + trace.in_span("HTTP::Client Connect") do |span| |
| 85 | + span.client! |
| 86 | + io = previous_def |
| 87 | + span["net.peer.name"] = @host |
| 88 | + span["net.peer.port"] = @port |
| 89 | + |
| 90 | + io |
| 91 | + end.not_nil! |
| 92 | + end |
| 93 | + |
| 94 | + trace("do_connect_ssl") do |
| 95 | + trace = OpenTelemetry.trace |
| 96 | + trace.in_span("Negotiate SSL") do |_span| |
| 97 | + previous_def |
| 98 | + end.not_nil! |
| 99 | + end |
| 100 | + |
| 101 | + def_around_exec do |request| |
| 102 | + trace = OpenTelemetry.trace |
| 103 | + trace.in_span("HTTP::Client #{request.method}") do |span| |
| 104 | + span.client! |
| 105 | + span["http.host"] = self.host |
| 106 | + span["http.port"] = self.port |
| 107 | + span["http.method"] = request.method |
| 108 | + span["http.flavor"] = request.version.split("/").last |
| 109 | + span["http.scheme"] = request.scheme |
| 110 | + if content_length = request.content_length |
| 111 | + span["http.response_content_length"] = content_length |
| 112 | + end |
| 113 | + span["http.url"] = "#{request.scheme}://#{self.host}:#{self.port}#{request.resource}" |
| 114 | + span["guid"] = span.span_id.hexstring |
| 115 | + |
| 116 | + OpenTelemetry::Propagation::TraceContext.new(span.context).inject(request.headers).not_nil! |
| 117 | + |
| 118 | + response = yield request |
| 119 | + |
| 120 | + span["http.status_code"] = response.status_code |
| 121 | + if response.success? |
| 122 | + # span.status.ok! |
| 123 | + else |
| 124 | + span.status.error! |
| 125 | + span["http.status_message"] = response.status_message.to_s |
| 126 | + end |
| 127 | + |
| 128 | + response |
| 129 | + end.not_nil! |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | +end |
0 commit comments