Module: Bitfab::Otel::Encoder
- Defined in:
- lib/bitfab/otel.rb
Overview
Turns finished carrier spans into the OTLP/JSON shapes Bitfab ingests.
Class Method Summary collapse
- .encode_request(envelope, spans) ⇒ Object
- .encode_span(span) ⇒ Object
- .request_envelope(first) ⇒ Object
- .span_to_otlp(span) ⇒ Object
- .trim_span(span) ⇒ Object
Class Method Details
.encode_request(envelope, spans) ⇒ Object
738 739 740 |
# File 'lib/bitfab/otel.rb', line 738 def encode_request(envelope, spans) envelope.head + spans.map(&:encoded).join(",") + envelope.tail end |
.encode_span(span) ⇒ Object
710 711 712 713 |
# File 'lib/bitfab/otel.rb', line 710 def encode_span(span) encoded = JSON.generate(span_to_otlp(span)) EncodedSpan.new(encoded, encoded.bytesize, Otel.take_carrier_ref(span.hex_span_id)) end |
.request_envelope(first) ⇒ Object
729 730 731 732 733 734 735 736 |
# File 'lib/bitfab/otel.rb', line 729 def request_envelope(first) scope = first.instrumentation_scope resource = JSON.generate({"attributes" => attributes(first.resource.attribute_enumerator.to_h)}) scope_json = JSON.generate({"name" => scope.name, "version" => scope.version || ""}) head = %({"resourceSpans":[{"resource":#{resource},"scopeSpans":[{"scope":#{scope_json},"spans":[) tail = "]}]}]}" RequestEnvelope.new(head, tail, (head + tail).bytesize) end |
.span_to_otlp(span) ⇒ Object
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/bitfab/otel.rb', line 742 def span_to_otlp(span) result = { "traceId" => span.hex_trace_id, "spanId" => span.hex_span_id, "name" => span.name, "kind" => SPAN_KINDS.fetch(span.kind, 1), "startTimeUnixNano" => (span. || 0).to_s, "endTimeUnixNano" => (span. || 0).to_s, "attributes" => attributes(span.attributes), "droppedAttributesCount" => dropped_count(span.total_recorded_attributes, span.attributes), "droppedEventsCount" => dropped_count(span.total_recorded_events, span.events), "droppedLinksCount" => dropped_count(span.total_recorded_links, span.links), "status" => status(span.status), "flags" => span.trace_flags.sampled? ? 1 : 0 } result["parentSpanId"] = span.hex_parent_span_id unless span.parent_span_id == INVALID_SPAN_ID tracestate = span.tracestate&.to_s result["traceState"] = tracestate unless tracestate.nil? || tracestate.empty? result end |
.trim_span(span) ⇒ Object
715 716 717 718 719 720 721 722 723 724 725 726 727 |
# File 'lib/bitfab/otel.rb', line 715 def trim_span(span) carrier = JSON.parse(span.encoded) attribute = carrier.fetch("attributes").find { |entry| entry["key"] == PAYLOAD_ATTRIBUTE } return nil if attribute.nil? value = attribute.fetch("value") payload = JSON.parse(value.fetch("stringValue")) value["stringValue"] = Serialize.safe_generate(payload) encoded = JSON.generate(carrier) EncodedSpan.new(encoded, encoded.bytesize) rescue KeyError, JSON::ParserError, TypeError nil end |