Module: Datadog::Tracing::Contrib::Sequel::Utils

Defined in:
lib/datadog/tracing/contrib/sequel/utils.rb

Overview

General purpose functions for Sequel

Class Method Summary collapse

Class Method Details

.adapter_name(database) ⇒ Object

Ruby database connector library

e.g. adapter:mysql2 (database:mysql), adapter:jdbc (database:postgres)



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/datadog/tracing/contrib/sequel/utils.rb', line 20

def adapter_name(database)
  scheme = database.adapter_scheme.to_s

  if scheme == "jdbc"
    # The subtype is more important in this case,
    # otherwise all database adapters will be 'jdbc'.
    database_type(database)
  else
    Contrib::Utils::Database.normalize_vendor(scheme)
  end
end

.connection_metadata(db) ⇒ Object

Resolves the connection host/port/database for a Sequel::Database. For Sequel's JDBC adapter (used on JRuby), metadata is parsed from the JDBC connection string regardless of whether opts is set.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/datadog/tracing/contrib/sequel/utils.rb', line 93

def (db)
  opts = db.opts || {}
  host = opts[:host]
  port = opts[:port]
  database = opts[:database]

  # A JDBC connection string (in :uri, :url, or :database) can carry credentials, so
  # always emit only parsed metadata -- never the raw connection string.
  connection_string = opts[:uri] || opts[:url] || opts[:database]
  is_jdbc = connection_string.is_a?(String) &&
    connection_string.byteslice(0, 5)&.casecmp("jdbc:") == 0
  if is_jdbc
    parsed = JDBCConnectionString.parse(connection_string)

    # JNDI/DataSource-managed connections keep only a lookup name in opts (e.g.
    # "jdbc:jndi:..."), so nothing can be parsed from it. Recover the endpoint from the
    # live connection's JDBC metadata, the same way Sequel resolves JNDI. This stays a
    # fallback rather than the primary source: it requires a connection checkout and
    # some drivers report no connection string, whereas the opts value is free and
    # already present for direct connections (and non-JDBC adapters have no such
    # metadata at all).
    if parsed[:host].nil? && parsed[:port].nil? && parsed[:database].nil?
       = (db)
      parsed =  if 
    end

    # Sequel's JDBC adapter connects with the connection string and ignores separate
    # :host/:port options, unlike native adapters where those options take precedence.
    if parsed[:host] || parsed[:port] || parsed[:database]
      host = parsed[:host]
      port = parsed[:port]
    end
    database = parsed[:database]
  end

  {host: host, port: port&.to_s, database: database}
end

.database_type(database) ⇒ Object

Database engine

e.g. database:mysql (adapter:mysql2), database:postgres (adapter:jdbc)



35
36
37
# File 'lib/datadog/tracing/contrib/sequel/utils.rb', line 35

def database_type(database)
  Contrib::Utils::Database.normalize_vendor(database.database_type.to_s)
end

.parse_opts(sql, opts, db_opts, dataset = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/datadog/tracing/contrib/sequel/utils.rb', line 39

def parse_opts(sql, opts, db_opts, dataset = nil)
  # Prepared statements don't provide their sql query in the +sql+ parameter.
  if !sql.is_a?(String) && dataset&.respond_to?(:prepared_sql) &&
      (resolved_sql = dataset.prepared_sql)
    # The dataset contains the resolved SQL query and prepared statement name.
    prepared_name = dataset.prepared_statement_name
    sql = resolved_sql
  end

  {
    name: opts[:type],
    query: sql,
    prepared_name: prepared_name,
    database: db_opts[:database],
    host: db_opts[:host],
  }
end

.set_common_tags(span, db) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/datadog/tracing/contrib/sequel/utils.rb', line 57

def set_common_tags(span, db)
  span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
  span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_QUERY)
  span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_CLIENT)
  span.set_tag(Contrib::Ext::DB::TAG_SYSTEM, database_type(db))

   = (db)

   = false

  # Embedded/hostless databases (e.g. SQLite) have no network peer; skip peer-identifying tags.
  if [:host] && ![:host].empty?
    span.set_tag(Tracing::Metadata::Ext::NET::TAG_DESTINATION_NAME, [:host])
    span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_HOST, [:host])
    span.set_tag(Tracing::Metadata::Ext::TAG_PEER_HOSTNAME, [:host])
    span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_PORT, [:port]) if [:port]
     = true
  end

  if [:database] && ![:database].empty?
    span.set_tag(Contrib::Ext::DB::TAG_INSTANCE, [:database])
    span.set_tag(Ext::TAG_DB_NAME, [:database])
     = true
  end

  if 
    Contrib::SpanAttributeSchema.set_peer_service!(span, Ext::PEER_SERVICE_SOURCES)
  end

  # Set analytics sample rate
  Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?
end