|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package gocql |
| 16 | + |
| 17 | +import ( |
| 18 | + "go.opentelemetry.io/otel/api/kv" |
| 19 | + "go.opentelemetry.io/otel/api/standard" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + // cassVersionKey is the key for the attribute/label describing |
| 24 | + // the cql version. |
| 25 | + cassVersionKey = kv.Key("db.cassandra.version") |
| 26 | + |
| 27 | + // cassHostIDKey is the key for the attribute/label describing the id |
| 28 | + // of the host being queried. |
| 29 | + cassHostIDKey = kv.Key("db.cassandra.host.id") |
| 30 | + |
| 31 | + // cassHostStateKey is the key for the attribute/label describing |
| 32 | + // the state of the casssandra server hosting the node being queried. |
| 33 | + cassHostStateKey = kv.Key("db.cassandra.host.state") |
| 34 | + |
| 35 | + // cassBatchQueriesKey is the key for the attribute describing |
| 36 | + // the number of queries contained within the batch statement. |
| 37 | + cassBatchQueriesKey = kv.Key("db.cassandra.batch.queries") |
| 38 | + |
| 39 | + // cassErrMsgKey is the key for the attribute/label describing |
| 40 | + // the error message from an error encountered when executing a query, batch, |
| 41 | + // or connection attempt to the cassandra server. |
| 42 | + cassErrMsgKey = kv.Key("db.cassandra.error.message") |
| 43 | + |
| 44 | + // cassRowsReturnedKey is the key for the span attribute describing the number of rows |
| 45 | + // returned on a query to the database. |
| 46 | + cassRowsReturnedKey = kv.Key("db.cassandra.rows.returned") |
| 47 | + |
| 48 | + // cassQueryAttemptsKey is the key for the span attribute describing the number of attempts |
| 49 | + // made for the query in question. |
| 50 | + cassQueryAttemptsKey = kv.Key("db.cassandra.attempts") |
| 51 | + |
| 52 | + // Static span names |
| 53 | + cassBatchQueryName = "Batch Query" |
| 54 | + cassConnectName = "New Connection" |
| 55 | + |
| 56 | + // instrumentationName is the name of the instrumentation package. |
| 57 | + instrumentationName = "go.opentelemetry.io/contrib/instrumentation/github.com/gocql/gocql" |
| 58 | +) |
| 59 | + |
| 60 | +// ------------------------------------------ Connection-level Attributes |
| 61 | + |
| 62 | +// cassDBSystem returns the name of the DB system, |
| 63 | +// cassandra, as a KeyValue pair (db.system). |
| 64 | +func cassDBSystem() kv.KeyValue { |
| 65 | + return standard.DBSystemCassandra |
| 66 | +} |
| 67 | + |
| 68 | +// cassPeerName returns the hostname of the cassandra |
| 69 | +// server as a standard KeyValue pair (net.peer.name). |
| 70 | +func cassPeerName(name string) kv.KeyValue { |
| 71 | + return standard.NetPeerNameKey.String(name) |
| 72 | +} |
| 73 | + |
| 74 | +// cassPeerPort returns the port number of the cassandra |
| 75 | +// server as a standard KeyValue pair (net.peer.port). |
| 76 | +func cassPeerPort(port int) kv.KeyValue { |
| 77 | + return standard.NetPeerPortKey.Int(port) |
| 78 | +} |
| 79 | + |
| 80 | +// cassPeerIP returns the IP address of the cassandra |
| 81 | +// server as a standard KeyValue pair (net.peer.ip). |
| 82 | +func cassPeerIP(ip string) kv.KeyValue { |
| 83 | + return standard.NetPeerIPKey.String(ip) |
| 84 | +} |
| 85 | + |
| 86 | +// cassVersion returns the cql version as a KeyValue pair. |
| 87 | +func cassVersion(version string) kv.KeyValue { |
| 88 | + return cassVersionKey.String(version) |
| 89 | +} |
| 90 | + |
| 91 | +// cassHostID returns the id of the cassandra host as a KeyValue pair. |
| 92 | +func cassHostID(id string) kv.KeyValue { |
| 93 | + return cassHostIDKey.String(id) |
| 94 | +} |
| 95 | + |
| 96 | +// cassHostState returns the state of the cassandra host as a KeyValue pair. |
| 97 | +func cassHostState(state string) kv.KeyValue { |
| 98 | + return cassHostStateKey.String(state) |
| 99 | +} |
| 100 | + |
| 101 | +// ------------------------------------------ Call-level attributes |
| 102 | + |
| 103 | +// cassStatement returns the statement made to the cassandra database as a |
| 104 | +// standard KeyValue pair (db.statement). |
| 105 | +func cassStatement(stmt string) kv.KeyValue { |
| 106 | + return standard.DBStatementKey.String(stmt) |
| 107 | +} |
| 108 | + |
| 109 | +// cassDBOperation returns the batch query operation |
| 110 | +// as a standard KeyValue pair (db.operation). This is used in lieu of a |
| 111 | +// db.statement, which is not feasible to include in a span for a batch query |
| 112 | +// because there can be n different query statements in a batch query. |
| 113 | +func cassBatchQueryOperation() kv.KeyValue { |
| 114 | + cassBatchQueryOperation := "db.cassandra.batch.query" |
| 115 | + return standard.DBOperationKey.String(cassBatchQueryOperation) |
| 116 | +} |
| 117 | + |
| 118 | +// cassConnectOperation returns the connect operation |
| 119 | +// as a standard KeyValue pair (db.operation). This is used in lieu of a |
| 120 | +// db.statement since connection creation does not have a CQL statement. |
| 121 | +func cassConnectOperation() kv.KeyValue { |
| 122 | + cassConnectOperation := "db.cassandra.connect" |
| 123 | + return standard.DBOperationKey.String(cassConnectOperation) |
| 124 | +} |
| 125 | + |
| 126 | +// cassKeyspace returns the keyspace of the session as |
| 127 | +// a standard KeyValue pair (db.cassandra.keyspace). |
| 128 | +func cassKeyspace(keyspace string) kv.KeyValue { |
| 129 | + return standard.DBCassandraKeyspaceKey.String(keyspace) |
| 130 | +} |
| 131 | + |
| 132 | +// cassBatchQueries returns the number of queries in a batch query |
| 133 | +// as a KeyValue pair. |
| 134 | +func cassBatchQueries(num int) kv.KeyValue { |
| 135 | + return cassBatchQueriesKey.Int(num) |
| 136 | +} |
| 137 | + |
| 138 | +// cassErrMsg returns the KeyValue pair of an error message |
| 139 | +// encountered when executing a query, batch query, or error. |
| 140 | +func cassErrMsg(msg string) kv.KeyValue { |
| 141 | + return cassErrMsgKey.String(msg) |
| 142 | +} |
| 143 | + |
| 144 | +// cassRowsReturned returns the KeyValue pair of the number of rows |
| 145 | +// returned from a query. |
| 146 | +func cassRowsReturned(rows int) kv.KeyValue { |
| 147 | + return cassRowsReturnedKey.Int(rows) |
| 148 | +} |
| 149 | + |
| 150 | +// cassQueryAttempts returns the KeyValue pair of the number of attempts |
| 151 | +// made for a query. |
| 152 | +func cassQueryAttempts(num int) kv.KeyValue { |
| 153 | + return cassQueryAttemptsKey.Int(num) |
| 154 | +} |
0 commit comments