|
7 | 7 |
|
8 | 8 | package io.bitdrift.capture.network
|
9 | 9 |
|
10 |
| -import io.bitdrift.capture.CaptureJniLibrary |
11 | 10 | import io.bitdrift.capture.InternalFieldsMap
|
12 | 11 | import io.bitdrift.capture.events.span.SpanField
|
13 | 12 | import io.bitdrift.capture.providers.FieldValue
|
@@ -36,60 +35,78 @@ data class HttpResponseInfo @JvmOverloads constructor(
|
36 | 35 | ) {
|
37 | 36 | internal val name: String = "HTTPResponse"
|
38 | 37 |
|
39 |
| - // Lazy since the creation of the `HTTPResponseInfo` can happen before the logger is initialized |
40 |
| - // and trying to call a native `CaptureJniLibrary.normalizeUrlPath` method before the `Capture` |
41 |
| - // library is loaded leads to unsatisfied linking error. |
42 |
| - internal val fields: InternalFieldsMap by lazy { |
43 |
| - val fields = buildMap { |
44 |
| - put(SpanField.Key.TYPE, FieldValue.StringField(SpanField.Value.TYPE_END)) |
45 |
| - put(SpanField.Key.DURATION, FieldValue.StringField(durationMs.toString())) |
46 |
| - put(SpanField.Key.RESULT, FieldValue.StringField(response.result.name.lowercase())) |
47 |
| - putOptional("_status_code", response.statusCode) |
48 |
| - putOptional("_error_type", response.error) { it::javaClass.get().simpleName } |
49 |
| - putOptional("_error_message", response.error) { it.message.orEmpty() } |
50 |
| - putOptional(HttpFieldKey.HOST, response.host) |
51 |
| - putOptional(HttpFieldKey.PATH, response.path?.value) |
52 |
| - putOptional(HttpFieldKey.QUERY, response.query) |
| 38 | + internal val fields: InternalFieldsMap = |
| 39 | + run { |
| 40 | + // Collect out-of-the-box fields specific to HTTPResponse logs. The list consists of |
| 41 | + // HTTP specific fields such as host, path, or query and HTTP request performance |
| 42 | + // metrics such as DNS resolution time. |
| 43 | + val fields = buildMap { |
| 44 | + this.put(SpanField.Key.TYPE, FieldValue.StringField(SpanField.Value.TYPE_END)) |
| 45 | + this.put( |
| 46 | + SpanField.Key.DURATION, |
| 47 | + FieldValue.StringField(durationMs.toString()), |
| 48 | + ) |
| 49 | + this.put( |
| 50 | + SpanField.Key.RESULT, |
| 51 | + FieldValue.StringField(response.result.name.lowercase()), |
| 52 | + ) |
| 53 | + putOptional("_status_code", response.statusCode) |
| 54 | + putOptional( |
| 55 | + "_error_type", |
| 56 | + response.error, |
| 57 | + ) { it::javaClass.get().simpleName } |
| 58 | + putOptional( |
| 59 | + "_error_message", |
| 60 | + response.error, |
| 61 | + ) { it.message.orEmpty() } |
| 62 | + putOptional(HttpFieldKey.HOST, response.host) |
| 63 | + putOptional(HttpFieldKey.PATH, response.path?.value) |
| 64 | + putOptional(HttpFieldKey.QUERY, response.query) |
53 | 65 |
|
54 |
| - response.path?.let { |
55 |
| - val requestPathTemplate = if (request.path?.value == it.value) { |
56 |
| - // If the path between request and response did not change and an explicit path |
57 |
| - // template was provided as part of a request use it as path template on a response. |
58 |
| - request.path.template |
59 |
| - } else { |
60 |
| - null |
61 |
| - } |
| 66 | + response.path?.let { |
| 67 | + val requestPathTemplate = |
| 68 | + if (request.path?.value == it.value) { |
| 69 | + // If the path between request and response did not change and an explicit path |
| 70 | + // template was provided as part of a request use it as path template on a response. |
| 71 | + request.path.template |
| 72 | + } else { |
| 73 | + null |
| 74 | + } |
62 | 75 |
|
63 |
| - @Suppress("SwallowedException") |
64 |
| - val normalized: String? = requestPathTemplate?.let { it } |
65 |
| - ?: it.template?.let { it } |
66 |
| - ?: try { |
67 |
| - CaptureJniLibrary.normalizeUrlPath(it.value) |
68 |
| - } catch (e: Throwable) { |
69 |
| - null |
70 |
| - } |
| 76 | + putOptional( |
| 77 | + HttpFieldKey.PATH_TEMPLATE, |
| 78 | + requestPathTemplate?.let { it } ?: it.template?.let { it }, |
| 79 | + ) |
| 80 | + } |
71 | 81 |
|
72 |
| - putOptional(HttpFieldKey.PATH_TEMPLATE, normalized) |
| 82 | + metrics?.let<HttpRequestMetrics, Unit> { |
| 83 | + this.put( |
| 84 | + "_request_body_bytes_sent_count", |
| 85 | + FieldValue.StringField(it.requestBodyBytesSentCount.toString()), |
| 86 | + ) |
| 87 | + this.put( |
| 88 | + "_response_body_bytes_received_count", |
| 89 | + FieldValue.StringField(it.responseBodyBytesReceivedCount.toString()), |
| 90 | + ) |
| 91 | + this.put( |
| 92 | + "_request_headers_bytes_count", |
| 93 | + FieldValue.StringField(it.requestHeadersBytesCount.toString()), |
| 94 | + ) |
| 95 | + this.put( |
| 96 | + "_response_headers_bytes_count", |
| 97 | + FieldValue.StringField(it.responseHeadersBytesCount.toString()), |
| 98 | + ) |
| 99 | + putOptional("_dns_resolution_duration_ms", it.dnsResolutionDurationMs) |
| 100 | + } |
73 | 101 | }
|
74 | 102 |
|
75 |
| - metrics?.let { |
76 |
| - put("_request_body_bytes_sent_count", FieldValue.StringField(it.requestBodyBytesSentCount.toString())) |
77 |
| - put("_response_body_bytes_received_count", FieldValue.StringField(it.responseBodyBytesReceivedCount.toString())) |
78 |
| - put("_request_headers_bytes_count", FieldValue.StringField(it.requestHeadersBytesCount.toString())) |
79 |
| - put("_response_headers_bytes_count", FieldValue.StringField(it.responseHeadersBytesCount.toString())) |
80 |
| - putOptional("_dns_resolution_duration_ms", it.dnsResolutionDurationMs) |
81 |
| - } |
| 103 | + // Combine fields in the increasing order of their priority as the latter fields |
| 104 | + // override the former ones in the case of field name conflicts. |
| 105 | + extraFields.toFields() + request.commonFields + fields |
82 | 106 | }
|
83 | 107 |
|
84 |
| - extraFields.toFields() + request.commonFields + fields |
85 |
| - } |
86 |
| - |
87 |
| - // Lazy since the creation of the `HTTPResponseInfo` can happen before the logger is initialized |
88 |
| - // and trying to call a native `CaptureJniLibrary.normalizeUrlPath` method before the `Capture` |
89 |
| - // library is loaded leads to unsatisfied linking error. |
90 |
| - internal val matchingFields: InternalFieldsMap by lazy { |
| 108 | + internal val matchingFields: InternalFieldsMap = |
91 | 109 | request.fields.mapKeys { "_request.${it.key}" } +
|
92 | 110 | request.matchingFields.mapKeys { "_request.${it.key}" } +
|
93 | 111 | response.headers?.let { HTTPHeaders.normalizeHeaders(it) }.toFields()
|
94 |
| - } |
95 | 112 | }
|
0 commit comments