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
# File 'lib/datadog/tracing/contrib/propagation/sql_comment.rb', line 14

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

  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



22
23
24
25
26
27
28
29
30
31
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
# File 'lib/datadog/tracing/contrib/propagation/sql_comment.rb', line 22

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,
  }

  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