Skip to content

Commit

Permalink
Merge pull request #464 from nachoBonafonte/463-nsurlsesssion-instrum…
Browse files Browse the repository at this point in the history
…entation-should-capture-httpresponsebodysize

Include response length by default in the network spans
  • Loading branch information
bryce-b authored Sep 29, 2023
2 parents 8d6fa74 + 21d5b8c commit 30c2a5a
Show file tree
Hide file tree
Showing 5 changed files with 936 additions and 361 deletions.
18 changes: 9 additions & 9 deletions Scripts/semantic-convention/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ ROOT_DIR="${SCRIPT_DIR}/../../"

# freeze the spec & generator tools versions to make SemanticAttributes generation reproducible

# repository: https://github.com/open-telemetry/opentelemetry-specification
SEMCONV_VERSION=1.20.0
# repository: https://github.com/open-telemetry/semantic-conventions
SEMCONV_VERSION=1.21.0
SPEC_VERSION=v$SEMCONV_VERSION

# repository: https://github.com/open-telemetry/build-tools
GENERATOR_VERSION=0.18.0
GENERATOR_VERSION=0.21.0

cd ${SCRIPT_DIR}

rm -rf opentelemetry-specification || true
mkdir opentelemetry-specification
cd opentelemetry-specification
rm -rf semantic-conventions || true
mkdir semantic-conventions
cd semantic-conventions

git init
git remote add origin https://github.com/open-telemetry/opentelemetry-specification.git
git remote add origin https://github.com/open-telemetry/semantic-conventions.git
git fetch origin "$SPEC_VERSION"
git reset --hard FETCH_HEAD
cd ${SCRIPT_DIR}

docker run --rm \
-v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions:/source \
-v ${SCRIPT_DIR}/semantic-conventions/model:/source \
-v ${SCRIPT_DIR}/templates:/templates \
-v ${ROOT_DIR}/Sources/OpenTelemetryApi/Trace/:/output \
otel/semconvgen:$GENERATOR_VERSION \
Expand All @@ -37,7 +37,7 @@ docker run --rm \
-Denum=SemanticAttributes

docker run --rm \
-v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions:/source \
-v ${SCRIPT_DIR}/semantic-conventions/model:/source \
-v ${SCRIPT_DIR}/templates:/templates \
-v ${ROOT_DIR}/Sources/OpenTelemetrySdk/Resources/:/output \
otel/semconvgen:$GENERATOR_VERSION \
Expand Down
13 changes: 12 additions & 1 deletion Sources/Instrumentation/URLSession/URLSessionLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class URLSessionLogger {
if let port = request.url?.port {
attributes[SemanticAttributes.netPeerPort.rawValue] = AttributeValue.int(port)
}

if let bodySize = request.httpBody?.count {
attributes[SemanticAttributes.httpRequestBodySize.rawValue] = AttributeValue.int(bodySize)
}

var spanName = "HTTP " + (request.httpMethod ?? "")
if let customSpanName = instrumentation.configuration.nameSpan?(request) {
Expand Down Expand Up @@ -107,9 +111,16 @@ class URLSessionLogger {
}

let statusCode = httpResponse.statusCode
span.setAttribute(key: SemanticAttributes.httpStatusCode.rawValue, value: AttributeValue.int(statusCode))
span.setAttribute(key: SemanticAttributes.httpStatusCode.rawValue,
value: AttributeValue.int(statusCode))
span.status = statusForStatusCode(code: statusCode)

if let contentLengthHeader = httpResponse.allHeaderFields["Content-Length"] as? String,
let contentLength = Int(contentLengthHeader) {
span.setAttribute(key: SemanticAttributes.httpResponseBodySize.rawValue,
value: AttributeValue.int(contentLength))
}

instrumentation.configuration.receivedResponse?(response, dataOrFile, span)
span.end()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenTelemetryApi/OpenTelemetry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
/// The telemetry objects are lazy-loaded singletons resolved via ServiceLoader mechanism.
public struct OpenTelemetry {

public static var version = "v1.20.0"
public static var version = "v1.21.0"

public static var instance = OpenTelemetry()

Expand Down
Loading

0 comments on commit 30c2a5a

Please sign in to comment.