Class: Datadog::DI::EL::Compiler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/di/el/compiler.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.

DI Expression Language compiler.

Converts AST in probe definitions into Expression objects.

WARNING: this class produces strings that are then eval'd as Ruby code. Input ASTs are user-controlled. As such the compiler must sanitize and escape all input to avoid injection.

Besides quotes and backslashes we must also escape # which is starting string interpolation (#...).

Instance Method Summary collapse

Instance Method Details

#compile(ast) ⇒ Array(String, Array<Regexp>)

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.

Compiles ast into eval'able Ruby source.

Returns the compiled source and companion compiled Regexp objects.

Parameters:

  • ast (untyped) —

    expression AST from the probe definition.

Returns:

  • (Array(String, Array<Regexp>)) —

    the compiled Ruby source and the precompiled Regexps, indexed in the order the compiled code references them.



28
29
30
31
32
# File 'lib/datadog/di/el/compiler.rb', line 28

def compile(ast)
  regexps = []
  code = compile_partial(ast, regexps)
  [code, regexps]
end

#redaction_identifier(ast) ⇒ 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.

Returns the identifier that ast directly references at its top level, so the message path can redact direct references the same way snapshots redact by name.

Parameters:

  • ast (untyped) —

    top-level expression AST from a probe definition.

Returns:

  • (String, nil) —

    the variable name for a ref/iref, the field name for a getmember, or the string key for an index; nil when the top-level expression is not a direct reference.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/datadog/di/el/compiler.rb', line 42

def redaction_identifier(ast)
  return nil unless Hash === ast && ast.length == 1

  entry = ast.first
  return nil if entry.nil?

  op, target = entry
  case op
  when "ref"
    target if String === target
  when "getmember", "index"
    target[1] if Array === target && target.length == 2 && String === target[1]
  end
end