Skip to content

Commit 9f704b5

Browse files
feat : add w3c feature
1 parent 6d6b022 commit 9f704b5

File tree

8 files changed

+105
-71
lines changed

8 files changed

+105
-71
lines changed

melos.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ scripts:
5959
flutter: true
6060
dependsOn: pana
6161
private: false
62+
pods:
63+
run: cd ios && pod install --repo-update
64+
description: running pod install
65+
exec:
66+
concurrency: 1
67+
orderDependents: true
68+
packageFilters:
69+
fileExists: 'ios/PodFile'
6270
score:
6371
run: dart run pana --no-warning --exit-code-threshold 0
6472
exec:

packages/instabug_http_client/CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Changelog
22

3-
## Unreleased
3+
## [2.5.0] - 18/11/2024
44

5-
- Enables `InstabugHttpClient` to wrap an internal `http` client.
6-
- Add support for `http` v1 ([#20](https://github.com/Instabug/Instabug-Dart-http-Adapter/pull/20)).
5+
### Added
6+
7+
- Add support for tracing network requests from Instabug to services like Datadog and New Relic ([#21](https://github.com/Instabug/Instabug-Dart-http-Adapter/pull/21)).
78

89
## [2.4.0] - 7/05/2024
910

packages/instabug_http_client/example/android/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
<application
44
android:label="example"
55
android:name="${applicationName}"
6-
android:icon="@mipmap/ic_launcher">
6+
android:icon="@mipmap/ic_launcher"
7+
android:networkSecurityConfig="@xml/network_security_config"
8+
android:usesCleartextTraffic="true">
79
<activity
810
android:name=".MainActivity"
911
android:exported="true"

packages/instabug_http_client/example/pubspec.lock

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ packages:
7979
dependency: transitive
8080
description:
8181
name: http
82-
sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010
82+
sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2"
8383
url: "https://pub.dev"
8484
source: hosted
85-
version: "1.2.2"
85+
version: "0.13.6"
8686
http_parser:
8787
dependency: transitive
8888
description:
@@ -97,7 +97,7 @@ packages:
9797
path: "../../instabug_flutter"
9898
relative: true
9999
source: path
100-
version: "13.4.0"
100+
version: "14.0.0"
101101
instabug_http_client:
102102
dependency: "direct main"
103103
description:
@@ -246,14 +246,6 @@ packages:
246246
url: "https://pub.dev"
247247
source: hosted
248248
version: "14.2.5"
249-
web:
250-
dependency: transitive
251-
description:
252-
name: web
253-
sha256: cd3543bd5798f6ad290ea73d210f423502e71900302dde696f8bff84bf89a1cb
254-
url: "https://pub.dev"
255-
source: hosted
256-
version: "1.1.0"
257249
sdks:
258250
dart: ">=3.5.0 <4.0.0"
259251
flutter: ">=3.18.0-18.0.pre.54"

packages/instabug_http_client/lib/instabug_http_client.dart

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: invalid_use_of_internal_member
2+
13
library instabug_http_client;
24

35
import 'dart:convert';
@@ -7,19 +9,16 @@ import 'dart:convert';
79
import 'dart:typed_data';
810

911
import 'package:http/http.dart' as http;
12+
import 'package:instabug_flutter/instabug_flutter.dart';
1013
import 'package:instabug_http_client/instabug_http_logger.dart';
1114
import 'package:meta/meta.dart';
1215

1316
class InstabugHttpClient extends InstabugHttpLogger implements http.Client {
14-
/// Constructs a new [InstabugHttpClient].
15-
///
16-
/// Provide a value for [client] in order to override the internal client used
17-
/// by this class. This can be useful if you are working with other libraries
18-
/// that require other custom client implementations
19-
InstabugHttpClient({http.Client? client}) : client = client ?? http.Client() {
17+
InstabugHttpClient() : client = http.Client() {
2018
logger = this;
2119
}
2220

21+
final NetworkLogger _networklogger = NetworkLogger();
2322
@visibleForTesting
2423
http.Client client;
2524

@@ -31,66 +30,92 @@ class InstabugHttpClient extends InstabugHttpLogger implements http.Client {
3130

3231
@override
3332
Future<http.Response> delete(Uri url,
34-
{Map<String, String>? headers, Object? body, Encoding? encoding}) {
33+
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
3534
final DateTime startTime = DateTime.now();
35+
final Map<String, String> requestHeader = headers ?? <String, String>{};
36+
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
3637
return client
37-
.delete(url, body: body, headers: headers, encoding: encoding)
38+
.delete(url, body: body, headers: requestHeader, encoding: encoding)
3839
.then((http.Response response) {
39-
logger.onLogger(response, startTime: startTime);
40+
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
4041
return response;
4142
});
4243
}
4344

45+
Future<W3CHeader?> getW3cHeader(Map<String, String> requestHeader, DateTime startTime) async {
46+
final W3CHeader? w3cHeader = await _networklogger.getW3CHeader(
47+
requestHeader, startTime.millisecondsSinceEpoch);
48+
if (w3cHeader?.isW3cHeaderFound == false &&
49+
w3cHeader?.w3CGeneratedHeader != null) {
50+
requestHeader['traceparent'] = w3cHeader!.w3CGeneratedHeader!;
51+
}
52+
return w3cHeader;
53+
}
54+
4455
@override
45-
Future<http.Response> get(Uri url, {Map<String, String>? headers}) {
56+
Future<http.Response> get(Uri url, {Map<String, String>? headers}) async {
4657
final DateTime startTime = DateTime.now();
47-
return client.get(url, headers: headers).then((http.Response response) {
48-
logger.onLogger(response, startTime: startTime);
58+
final Map<String, String> requestHeader = headers ?? <String, String>{};
59+
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
60+
return client
61+
.get(url, headers: requestHeader)
62+
.then((http.Response response) {
63+
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
4964
return response;
5065
});
5166
}
5267

5368
@override
54-
Future<http.Response> head(Uri url, {Map<String, String>? headers}) {
69+
Future<http.Response> head(Uri url, {Map<String, String>? headers}) async {
5570
final DateTime startTime = DateTime.now();
56-
return client.head(url, headers: headers).then((http.Response response) {
57-
logger.onLogger(response, startTime: startTime);
71+
final Map<String, String> requestHeader = headers ?? <String, String>{};
72+
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
73+
return client
74+
.head(url, headers: requestHeader)
75+
.then((http.Response response) {
76+
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
5877
return response;
5978
});
6079
}
6180

6281
@override
6382
Future<http.Response> patch(Uri url,
64-
{Map<String, String>? headers, Object? body, Encoding? encoding}) {
83+
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
6584
final DateTime startTime = DateTime.now();
85+
final Map<String, String> requestHeader = headers ?? <String, String>{};
86+
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
6687
return client
67-
.patch(url, headers: headers, body: body, encoding: encoding)
88+
.patch(url, headers: requestHeader, body: body, encoding: encoding)
6889
.then((http.Response response) {
69-
logger.onLogger(response, startTime: startTime);
90+
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
7091
return response;
7192
});
7293
}
7394

7495
@override
7596
Future<http.Response> post(Uri url,
76-
{Map<String, String>? headers, Object? body, Encoding? encoding}) {
97+
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
7798
final DateTime startTime = DateTime.now();
99+
final Map<String, String> requestHeader = headers ?? <String, String>{};
100+
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
78101
return client
79-
.post(url, headers: headers, body: body, encoding: encoding)
102+
.post(url, headers: requestHeader, body: body, encoding: encoding)
80103
.then((http.Response response) {
81-
logger.onLogger(response, startTime: startTime);
104+
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
82105
return response;
83106
});
84107
}
85108

86109
@override
87110
Future<http.Response> put(Uri url,
88-
{Map<String, String>? headers, Object? body, Encoding? encoding}) {
111+
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
89112
final DateTime startTime = DateTime.now();
113+
final Map<String, String> requestHeader = headers ?? <String, String>{};
114+
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
90115
return client
91-
.put(url, headers: headers, body: body, encoding: encoding)
116+
.put(url, headers: requestHeader, body: body, encoding: encoding)
92117
.then((http.Response response) {
93-
logger.onLogger(response, startTime: startTime);
118+
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
94119
return response;
95120
});
96121
}
@@ -104,12 +129,14 @@ class InstabugHttpClient extends InstabugHttpLogger implements http.Client {
104129
client.readBytes(url, headers: headers);
105130

106131
@override
107-
Future<http.StreamedResponse> send(http.BaseRequest request) {
132+
Future<http.StreamedResponse> send(http.BaseRequest request) async {
108133
final DateTime startTime = DateTime.now();
134+
final Map<String, String> requestHeader = request.headers;
135+
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
109136
return client.send(request).then((http.StreamedResponse streamedResponse) =>
110137
http.Response.fromStream(streamedResponse)
111138
.then((http.Response response) {
112-
logger.onLogger(response, startTime: startTime);
139+
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
113140
// Need to return new StreamedResponse, as body only can be listened once
114141
return http.StreamedResponse(
115142
Stream<List<int>>.value(response.bodyBytes),

packages/instabug_http_client/lib/instabug_http_logger.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:http/http.dart' as http;
44
import 'package:instabug_flutter/instabug_flutter.dart';
55

66
class InstabugHttpLogger {
7-
void onLogger(http.Response response, {DateTime? startTime}) {
7+
void onLogger(http.Response response, {DateTime? startTime,W3CHeader? w3CHeader}) {
88
final NetworkLogger networkLogger = NetworkLogger();
99

1010
final Map<String, dynamic> requestHeaders = <String, dynamic>{};
@@ -29,6 +29,7 @@ class InstabugHttpLogger {
2929
url: request.url.toString(),
3030
requestHeaders: requestHeaders,
3131
requestBody: requestBody,
32+
w3cHeader: w3CHeader
3233
);
3334

3435
final DateTime endTime = DateTime.now();

packages/instabug_http_client/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ packages:
294294
path: "../instabug_flutter"
295295
relative: true
296296
source: path
297-
version: "13.4.0"
297+
version: "14.0.0"
298298
io:
299299
dependency: transitive
300300
description:

0 commit comments

Comments
 (0)