Class: Datadog::Core::Transport::Parcel

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/transport/parcel.rb

Overview

Data transfer object for transporting already-encoded data.

A Parcel is a container that holds pre-encoded data along with metadata about its encoding. The key design principle is that encoding happens BEFORE the Parcel is created, not inside it.

The content_type and content_encoding fields are optional but recommended:

  • Set them when creating the Parcel for automatic header management

  • Leave them nil for IO transports or when headers are managed elsewhere

  • HTTP transports use these to set appropriate request headers

Examples:

Creating a Parcel with JSON data

encoder = Core::Encoding::JSONEncoder
parcel = Parcel.new(
  encoder.encode(payload),
  content_type: encoder.content_type,
)

Creating a Parcel with compressed MessagePack data

msgpack_data = MessagePack.pack(payload)
compressed_data = Zlib.gzip(msgpack_data)
parcel = Parcel.new(
  compressed_data,
  content_type: 'application/msgpack',
  content_encoding: 'gzip',
)

Accessing Parcel data in HTTP transport

# In HTTP adapters, Parcel metadata maps to HTTP headers:
env.headers['Content-Type'] = request.parcel.content_type
env.headers['Content-Encoding'] = request.parcel.content_encoding
env.body = request.parcel.data

See Also:

Direct Known Subclasses

Tracing::Transport::Traces::Parcel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, content_type: nil, content_encoding: nil) ⇒ Parcel

Creates a new Parcel with pre-encoded data.

Parameters:

  • data (String)

    The already-encoded data (e.g., JSON string, MessagePack bytes)

  • content_type (String, nil) (defaults to: nil)

    MIME type of the data (e.g., ‘application/json’)

  • content_encoding (String, nil) (defaults to: nil)

    Encoding applied to data (e.g., ‘gzip’)



47
48
49
50
51
# File 'lib/datadog/core/transport/parcel.rb', line 47

def initialize(data, content_type: nil, content_encoding: nil)
  @data = data
  @content_type = content_type
  @content_encoding = content_encoding
end

Instance Attribute Details

#content_encodingObject (readonly)

Returns the value of attribute content_encoding.



70
71
72
# File 'lib/datadog/core/transport/parcel.rb', line 70

def content_encoding
  @content_encoding
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



66
67
68
# File 'lib/datadog/core/transport/parcel.rb', line 66

def content_type
  @content_type
end

#dataObject (readonly)

Returns the value of attribute data.



55
56
57
# File 'lib/datadog/core/transport/parcel.rb', line 55

def data
  @data
end

Instance Method Details

#lengthInteger

Returns the length of the encoded data.

Returns:

  • (Integer)

    Number of bytes in the data



60
61
62
# File 'lib/datadog/core/transport/parcel.rb', line 60

def length
  data.length
end