Class: Datadog::DI::SnapshotPruner Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/di/snapshot_pruner.rb,
lib/datadog/di/snapshot_pruner/node.rb,
lib/datadog/di/snapshot_pruner/parser.rb,
lib/datadog/di/snapshot_pruner/node_queue.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Reduces an encoded snapshot below a byte limit by replacing captured value objects with the pruned marker, operating directly on the encoded bytes. The largest, most deeply nested captured values are replaced first.

Defined Under Namespace

Classes: Node, NodeQueue, Parser

Constant Summary collapse

PRUNED_MARKER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Replacement for each pruned object.

'{"pruned":true}'
MIN_LEVEL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Shallowest nesting level that is pruned. The snapshot payload nests debugger, snapshot, captures and the capture sets above the captured values, which start at level 5 (method probe arguments and locals members, line probe line objects). Applying the level to promoted parents as well keeps captures and the capture sets.

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the pruner.



29
30
31
# File 'lib/datadog/di/snapshot_pruner.rb', line 29

def initialize
  @parser = Parser.new
end

Instance Attribute Details

#parser ⇒ Parser (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The parser that builds the object tree of an encoded snapshot.

Returns:



35
36
37
# File 'lib/datadog/di/snapshot_pruner.rb', line 35

def parser
  @parser
end

Instance Method Details

#prune(json, max_size:) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prunes an encoded snapshot that exceeds max_size.

Parameters:

  • json (String) —

    the encoded snapshot, a JSON object

  • max_size (Integer) —

    the size limit in bytes

Returns:

  • (String) —

    json itself when it is within max_size or has no object that can be pruned; otherwise a copy with the selected objects replaced by PRUNED_MARKER, which exceeds max_size only when pruning every candidate cannot cover the excess

Raises:

  • (ArgumentError) —

    when the document is malformed



46
47
48
49
50
51
52
53
54
55
# File 'lib/datadog/di/snapshot_pruner.rb', line 46

def prune(json, max_size:)
  excess = json.bytesize - max_size
  return json if excess <= 0

  root = parser.parse(json)
  selected_nodes = select_nodes(root, excess)
  return json if selected_nodes.empty?

  rewrite(json, selected_nodes)
end