Module: Datadog::Tracing::Contrib::Propagation::SqlComment

Defined in:
lib/datadog/tracing/contrib/propagation/sql_comment.rb,
lib/datadog/tracing/contrib/propagation/sql_comment/ext.rb,
lib/datadog/tracing/contrib/propagation/sql_comment/mode.rb,
lib/datadog/tracing/contrib/propagation/sql_comment/comment.rb

Overview

Implements sql comment propagation related contracts.

Defined Under Namespace

Modules: Ext Classes: Comment, Mode

Class Method Summary collapse

Class Method Details

.annotate!(span_op, mode) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/datadog/tracing/contrib/propagation/sql_comment.rb', line 14

def self.annotate!(span_op, mode)
  return unless mode.enabled?

  config = Datadog.configuration

  # Add DBM propagation hash when enabled and available.
  # This matches the behavior in the Python tracer:
  # https://github.com/DataDog/dd-trace-py/blob/5e94be5c97b42060e5800e35d8fa41472fb8c569/ddtrace/propagation/_database_monitoring.py#L89
  if inject_hash?(mode, config)
    checksum = Datadog.send(:components).agent_info.propagation_checksum
    span_op.set_tag(Ext::TAG_PROPAGATED_HASH, checksum.to_s) if checksum
  end

  span_op.set_tag(Ext::TAG_DBM_TRACE_INJECTED, true) if mode.full?
end

.prepend_comment(sql, span_op, trace_op, mode) ⇒ Object

Inject span_op and trace_op instead of TraceDigest to improve memory usage for disabled and service mode



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/datadog/tracing/contrib/propagation/sql_comment.rb', line 32

def self.prepend_comment(sql, span_op, trace_op, mode)
  return sql unless mode.enabled?

  config = Datadog.configuration

  parent_service = config.service
  peer_service = span_op.get_tag(Tracing::Metadata::Ext::TAG_PEER_SERVICE)

  tags = {
    Ext::KEY_ENVIRONMENT => config.env,
    Ext::KEY_PARENT_SERVICE => parent_service,
    Ext::KEY_VERSION => config.version,
    Ext::KEY_HOSTNAME => span_op.get_tag(Tracing::Metadata::Ext::TAG_PEER_HOSTNAME),
    Ext::KEY_DB_NAME => span_op.get_tag(Contrib::Ext::DB::TAG_INSTANCE),
    Ext::KEY_PEER_SERVICE => peer_service,
  }

  # Add DBM propagation hash to SQL comment when enabled and available.
  # This matches the behavior in the Python tracer:
  # https://github.com/DataDog/dd-trace-py/blob/5e94be5c97b42060e5800e35d8fa41472fb8c569/ddtrace/propagation/_database_monitoring.py#L139
  if inject_hash?(mode, config)
    checksum = Datadog.send(:components).agent_info.propagation_checksum
    tags[Ext::KEY_BASE_HASH] = checksum.to_s if checksum
  end

  db_service = peer_service || span_op.service
  if parent_service != db_service # Only set if it's different from parent_service; otherwise it's redundant
    tags[Ext::KEY_DATABASE_SERVICE] = db_service
  end

  if mode.full?
    # When tracing is disabled, trace_operation is a dummy object that does not contain data to build traceparent
    if config.tracing.enabled
      tags[Ext::KEY_TRACEPARENT] =
        Tracing::Distributed::TraceContext.new(fetcher: nil).send(:build_traceparent, trace_op.to_digest)
    else
      Datadog.logger.warn(
        'Sql comment propagation with `full` mode is aborted, because tracing is disabled. ' \
        'Please set `Datadog.configuration.tracing.enabled = true` to continue.'
      )
    end
  end

  if mode.append?
    "#{sql} #{Comment.new(tags)}"
  else
    "#{Comment.new(tags)} #{sql}"
  end
end