Skip to content

Commit

Permalink
Add remaining Otel (0.7.0) semantic attributes (#38)
Browse files Browse the repository at this point in the history
* Add remaining Otel (0.7.0) semantic attributes

* Use SeeAlso instead of See in documentation blocks

Co-authored-by: Konrad `ktoso` Malawski <[email protected]>
  • Loading branch information
slashmo and ktoso committed Jan 27, 2021
1 parent cc3033f commit fce1f7c
Show file tree
Hide file tree
Showing 14 changed files with 967 additions and 158 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ When creating a tracer you need to create two types:
1. Your tracer conforming to `Tracer`
2. A span class conforming to `Span`

> The `Span` conforms to the standard rules defined in [OpenTelemetry](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#span), so if unsure about usage patterns, you can refer to this specification and examples referring to it.
> The `Span` conforms to the standard rules defined in [OpenTelemetry](https://github.com/open-telemetry/opentelemetry-specification/blob/v0.7.0/specification/trace/api.md#span), so if unsure about usage patterns, you can refer to this specification and examples referring to it.
### Defining, injecting and extracting Baggage

Expand Down
4 changes: 2 additions & 2 deletions Sources/Tracing/Span.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Dispatch
///
/// Creating a `Span` is delegated to a `Tracer` and end users should never create them directly.
///
/// - SeeAlso: For more details refer to the [OpenTelemetry Specification: Span](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#span) which this type is compatible with.
/// - SeeAlso: For more details refer to the [OpenTelemetry Specification: Span](https://github.com/open-telemetry/opentelemetry-specification/blob/v0.7.0/specification/trace/api.md#span) which this type is compatible with.
public protocol Span: AnyObject {
/// The read-only `Baggage` of this `Span`, set when starting this `Span`.
var baggage: Baggage { get }
Expand Down Expand Up @@ -600,7 +600,7 @@ public struct SpanStatus {

/// A code representing the status of a `Span`.
///
/// - SeeAlso: For the semantics of status codes see [OpenTelemetry Specification: setStatus](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#set-status)
/// - SeeAlso: For the semantics of status codes see [OpenTelemetry Specification: setStatus](https://github.com/open-telemetry/opentelemetry-specification/blob/v0.7.0/specification/trace/api.md#set-status)
public enum Code {
/// The Span has been validated by an Application developer or Operator to have completed successfully.
case ok
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Distributed Tracing open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift Distributed Tracing project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Tracing

extension SpanAttributeName {
/// - SeeAlso: DatabaseAttributes
public enum Database {
/// - SeeAlso: DatabaseAttributes
public static let system = "db.system"
/// - SeeAlso: DatabaseAttributes
public static let connectionString = "db.connection_string"
/// - SeeAlso: DatabaseAttributes
public static let user = "db.user"
/// - SeeAlso: DatabaseAttributes
public static let name = "db.name"
/// - SeeAlso: DatabaseAttributes
public static let statement = "db.statement"
/// - SeeAlso: DatabaseAttributes
public static let operation = "db.operation"

/// - SeeAlso: DatabaseAttributes.MSSQLAttributes
public enum MSSQL {
/// - SeeAlso: DatabaseAttributes.MSSQLAttributes
public static let instanceName = "db.mssql.instance_name"
}

/// - SeeAlso: DatabaseAttributes.CassandraAttributes
public enum Cassandra {
/// - SeeAlso: DatabaseAttributes.CassandraAttributes
public static let keyspace = "db.cassandra.keyspace"
}

/// - SeeAlso: DatabaseAttributes.HBaseAttributes
public enum HBase {
/// - SeeAlso: DatabaseAttributes.HBaseAttributes
public static let namespace = "db.hbase.namespace"
}

/// - SeeAlso: DatabaseAttributes.RedisAttributes
public enum Redis {
/// - SeeAlso: DatabaseAttributes.RedisAttributes
public static let databaseIndex = "db.redis.database_index"
}

/// - SeeAlso: DatabaseAttributes.MongoDBAttributes
public enum MongoDB {
/// - SeeAlso: DatabaseAttributes.MongoDBAttributes
public static let collection = "db.mongodb.collection"
}

/// - SeeAlso: DatabaseAttributes.SQLAttributes
public enum SQL {
/// - SeeAlso: DatabaseAttributes.SQLAttributes
public static let table = "db.sql.table"
}
}
}

#if swift(>=5.2)
extension SpanAttributes {
/// Semantic database client call attributes.
public var db: DatabaseAttributes {
get {
.init(attributes: self)
}
set {
self = newValue.attributes
}
}
}

/// Semantic conventions for database client calls as defined in the OpenTelemetry spec.
///
/// - SeeAlso: [OpenTelemetry: Database attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/v0.7.0/specification/trace/semantic_conventions/database.md)
@dynamicMemberLookup
public struct DatabaseAttributes: SpanAttributeNamespace {
public var attributes: SpanAttributes

public init(attributes: SpanAttributes) {
self.attributes = attributes
}

// MARK: - General

public struct NestedSpanAttributes: NestedSpanAttributesProtocol {
public init() {}

/// An identifier for the database management system (DBMS) product being used. See [OpenTelemetry: Database attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/v0.7.0/specification/trace/semantic_conventions/database.md) for a list of well-known identifiers.
public var system: Key<String> { .init(name: SpanAttributeName.Database.system) }

/// The connection string used to connect to the database. It is recommended to remove embedded credentials.
public var connectionString: Key<String> { .init(name: SpanAttributeName.Database.connectionString) }

/// Username for accessing the database.
public var user: Key<String> { .init(name: SpanAttributeName.Database.user) }

/// If no [tech-specific attribute](https://github.com/open-telemetry/opentelemetry-specification/blob/v0.7.0/specification/trace/semantic_conventions/database.md#call-level-attributes-for-specific-technologies)
/// is defined, this attribute is used to report the name of the database being accessed. For commands that switch the database, this should be set to the target database (even if the command fails).
///
/// - Note: In some SQL databases, the database name to be used is called "schema name".
public var name: Key<String> { .init(name: SpanAttributeName.Database.name) }

/// The database statement being executed.
///
/// - Note: The value may be sanitized to exclude sensitive information.
public var statement: Key<String> { .init(name: SpanAttributeName.Database.statement) }

/// The name of the operation being executed, e.g. the [MongoDB command name](https://docs.mongodb.com/manual/reference/command/#database-operations)
/// such as `findAndModify`, or the SQL keyword.
///
/// - Note: When setting this to an SQL keyword, it is not recommended to attempt any client-side parsing of `db.statement` just to get this
/// property, but it should be set if the operation name is provided by the library being instrumented.
/// If the SQL statement has an ambiguous operation, or performs more than one operation, this value may be omitted.
public var operation: Key<String> { .init(name: SpanAttributeName.Database.operation) }
}

// MARK: - MSSQL

/// Semantic MSSQL client call attributes.
public var mssql: MSSQLAttributes {
get {
.init(attributes: self.attributes)
}
set {
self.attributes = newValue.attributes
}
}

/// Semantic conventions for MSSQL client calls as defined in the OpenTelemetry spec.
public struct MSSQLAttributes: SpanAttributeNamespace {
public var attributes: SpanAttributes

public init(attributes: SpanAttributes) {
self.attributes = attributes
}

public struct NestedSpanAttributes: NestedSpanAttributesProtocol {
public init() {}

/// The Microsoft SQL Server [instance name](https://docs.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15)
/// connecting to. This name is used to determine the port of a named instance.
///
/// - Note: If setting a `db.mssql.instance_name`, `net.peer.port` is no longer required (but still recommended if non-standard).
public var instanceName: Key<String> { .init(name: SpanAttributeName.Database.MSSQL.instanceName) }
}
}

// MARK: - Cassandra

/// Semantic Cassandra client call attributes.
public var cassandra: CassandraAttributes {
get {
.init(attributes: self.attributes)
}
set {
self.attributes = newValue.attributes
}
}

/// Semantic conventions for Cassandra client calls as defined in the OpenTelemetry spec.
public struct CassandraAttributes: SpanAttributeNamespace {
public var attributes: SpanAttributes

public init(attributes: SpanAttributes) {
self.attributes = attributes
}

public struct NestedSpanAttributes: NestedSpanAttributesProtocol {
public init() {}

/// The name of the keyspace being accessed. To be used instead of the generic `db.name` attribute.
public var keyspace: Key<String> { .init(name: SpanAttributeName.Database.Cassandra.keyspace) }
}
}

// MARK: - HBase

/// Semantic HBase client call attributes.
public var hbase: HBaseAttributes {
get {
.init(attributes: self.attributes)
}
set {
self.attributes = newValue.attributes
}
}

/// Semantic conventions for HBase client calls as defined in the OpenTelemetry spec.
public struct HBaseAttributes: SpanAttributeNamespace {
public var attributes: SpanAttributes

public init(attributes: SpanAttributes) {
self.attributes = attributes
}

public struct NestedSpanAttributes: NestedSpanAttributesProtocol {
public init() {}

/// The [HBase namespace](https://hbase.apache.org/book.html#_namespace) being accessed.
/// To be used instead of the generic `db.name` attribute.
public var namespace: Key<String> { .init(name: SpanAttributeName.Database.HBase.namespace) }
}
}

// MARK: - Redis

/// Semantic Redis client call attributes.
public var redis: RedisAttributes {
get {
.init(attributes: self.attributes)
}
set {
self.attributes = newValue.attributes
}
}

/// Semantic conventions for Redis client calls as defined in the OpenTelemetry spec.
public struct RedisAttributes: SpanAttributeNamespace {
public var attributes: SpanAttributes

public init(attributes: SpanAttributes) {
self.attributes = attributes
}

public struct NestedSpanAttributes: NestedSpanAttributesProtocol {
public init() {}

/// The index of the database being accessed as used in the [`SELECT` command](https://redis.io/commands/select),
/// provided as an integer. To be used instead of the generic `db.name` attribute.
public var databaseIndex: Key<Int> { .init(name: SpanAttributeName.Database.Redis.databaseIndex) }
}
}

// MARK: - MongoDB

/// Semantic MongoDB client call attributes.
public var mongodb: MongoDBAttributes {
get {
.init(attributes: self.attributes)
}
set {
self.attributes = newValue.attributes
}
}

/// Semantic conventions for MongoDB client calls as defined in the OpenTelemetry spec.
public struct MongoDBAttributes: SpanAttributeNamespace {
public var attributes: SpanAttributes

public init(attributes: SpanAttributes) {
self.attributes = attributes
}

public struct NestedSpanAttributes: NestedSpanAttributesProtocol {
public init() {}

/// The collection being accessed within the database stated in `db.name`.
public var collection: Key<String> { .init(name: SpanAttributeName.Database.MongoDB.collection) }
}
}

// MARK: - SQL

/// Semantic SQL client call attributes.
public var sql: SQLAttributes {
get {
.init(attributes: self.attributes)
}
set {
self.attributes = newValue.attributes
}
}

/// Semantic conventions for SQL client calls as defined in the OpenTelemetry spec.
public struct SQLAttributes: SpanAttributeNamespace {
public var attributes: SpanAttributes

public init(attributes: SpanAttributes) {
self.attributes = attributes
}

public struct NestedSpanAttributes: NestedSpanAttributesProtocol {
public init() {}

/// The name of the primary table that the operation is acting upon, including the schema name (if applicable).
///
/// - Note: It is not recommended to attempt any client-side parsing of `db.statement` just to get this property,
/// but it should be set if it is provided by the library being instrumented.
/// If the operation is acting upon an anonymous table, or more than one table, this value MUST NOT be set.
public var table: Key<String> { .init(name: SpanAttributeName.Database.SQL.table) }
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import Tracing

extension SpanAttributeName {
/// - See: EndUserAttributes
/// - SeeAlso: EndUserAttributes
public enum EndUser {
/// - See: EndUserAttributes
/// - SeeAlso: EndUserAttributes
public static let id = "enduser.id"
/// - See: EndUserAttributes
/// - SeeAlso: EndUserAttributes
public static let role = "enduser.role"
/// - See: EndUserAttributes
/// - SeeAlso: EndUserAttributes
public static let scope = "enduser.scope"
}
}
Expand All @@ -40,7 +40,7 @@ extension SpanAttributes {

/// End-user-related semantic conventions as defined in the OpenTelemetry spec.
///
/// - SeeAlso: [OpenTelemetry: General identity attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/b70565d5a8a13d26c91fb692879dc874d22c3ac8/specification/trace/semantic_conventions/span-general.md#general-identity-attributes) (as of August 2020)
/// - SeeAlso: [OpenTelemetry: General identity attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/v0.7.0/specification/trace/semantic_conventions/span-general.md#general-identity-attributes)
@dynamicMemberLookup
public struct EndUserAttributes: SpanAttributeNamespace {
public var attributes: SpanAttributes
Expand Down
Loading

0 comments on commit fce1f7c

Please sign in to comment.