Skip to content

Commit ea35739

Browse files
feat: add w3c header
1 parent c76508f commit ea35739

File tree

10 files changed

+111
-45
lines changed

10 files changed

+111
-45
lines changed

packages/instabug_dio_interceptor/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Changelog
22

3-
## [2.3.0] - 7/05/2024
3+
## [2.4.0] (2024-11-18)
4+
5+
### Added
6+
7+
- Add support for tracing network requests from Instabug to services like Datadog and New Relic ([#30](https://github.com/Instabug/Instabug-Dio-Interceptor/pull/30)).
8+
9+
## [2.3.0] - (2024-05-07)
410

511
### Added
612

packages/instabug_dio_interceptor/example/android/app/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ android {
4444
defaultConfig {
4545
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
4646
applicationId "com.example.example"
47-
minSdkVersion 21
47+
minSdkVersion flutter.minSdkVersion
4848
targetSdkVersion flutter.targetSdkVersion
4949
versionCode flutterVersionCode.toInteger()
5050
versionName flutterVersionName
@@ -65,6 +65,4 @@ flutter {
6565

6666
dependencies {
6767
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
68-
implementation 'com.android.support:multidex:1.0.3'
69-
7068
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.example.example">
3-
<application
4-
android:label="example"
3+
4+
<application
55
android:name="${applicationName}"
6-
android:icon="@mipmap/ic_launcher">
6+
android:icon="@mipmap/ic_launcher"
7+
android:label="example"
8+
android:networkSecurityConfig="@xml/network_security_config"
9+
android:usesCleartextTraffic="true">
710
<activity
811
android:name=".MainActivity"
12+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
913
android:exported="true"
14+
android:hardwareAccelerated="true"
1015
android:launchMode="singleTop"
1116
android:theme="@style/LaunchTheme"
12-
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
13-
android:hardwareAccelerated="true"
1417
android:windowSoftInputMode="adjustResize">
1518
<!-- Specifies an Android theme to apply to this Activity as soon as
1619
the Android process has started. This theme is visible to the user
1720
while the Flutter UI initializes. After that, this theme continues
1821
to determine the Window background behind the Flutter UI. -->
1922
<meta-data
20-
android:name="io.flutter.embedding.android.NormalTheme"
21-
android:resource="@style/NormalTheme"
22-
/>
23+
android:name="io.flutter.embedding.android.NormalTheme"
24+
android:resource="@style/NormalTheme" />
2325
<intent-filter>
24-
<action android:name="android.intent.action.MAIN"/>
25-
<category android:name="android.intent.category.LAUNCHER"/>
26+
<action android:name="android.intent.action.MAIN" />
27+
<category android:name="android.intent.category.LAUNCHER" />
2628
</intent-filter>
2729
</activity>
2830
<!-- Don't delete the meta-data below.

packages/instabug_dio_interceptor/example/android/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
buildscript {
2-
ext.kotlin_version = '1.7.10'
2+
ext.kotlin_version = '1.6.10'
33
repositories {
44
google()
55
mavenCentral()
66
}
77

88
dependencies {
9-
classpath 'com.android.tools.build:gradle:7.2.0'
9+
classpath 'com.android.tools.build:gradle:4.1.0'
1010
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1111
}
1212
}
@@ -26,6 +26,6 @@ subprojects {
2626
project.evaluationDependsOn(':app')
2727
}
2828

29-
tasks.register("clean", Delete) {
29+
task clean(type: Delete) {
3030
delete rootProject.buildDir
3131
}

packages/instabug_dio_interceptor/example/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip

packages/instabug_dio_interceptor/example/pubspec.lock

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ packages:
5353
dependency: "direct main"
5454
description:
5555
name: dio
56-
sha256: e17f6b3097b8c51b72c74c9f071a605c47bcc8893839bd66732457a5ebe73714
56+
sha256: "5598aa796bbf4699afd5c67c0f5f6e2ed542afc956884b9cd58c306966efc260"
5757
url: "https://pub.dev"
5858
source: hosted
59-
version: "5.5.0+1"
59+
version: "5.7.0"
6060
dio_web_adapter:
6161
dependency: transitive
6262
description:
6363
name: dio_web_adapter
64-
sha256: "36c5b2d79eb17cdae41e974b7a8284fec631651d2a6f39a8a2ff22327e90aeac"
64+
sha256: "33259a9276d6cea88774a0000cfae0d861003497755969c92faa223108620dc8"
6565
url: "https://pub.dev"
6666
source: hosted
67-
version: "1.0.1"
67+
version: "2.0.0"
6868
fake_async:
6969
dependency: transitive
7070
description:
@@ -234,10 +234,10 @@ packages:
234234
dependency: transitive
235235
description:
236236
name: typed_data
237-
sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c
237+
sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006
238238
url: "https://pub.dev"
239239
source: hosted
240-
version: "1.3.2"
240+
version: "1.4.0"
241241
vector_math:
242242
dependency: transitive
243243
description:
@@ -254,6 +254,14 @@ packages:
254254
url: "https://pub.dev"
255255
source: hosted
256256
version: "14.2.5"
257+
web:
258+
dependency: transitive
259+
description:
260+
name: web
261+
sha256: cd3543bd5798f6ad290ea73d210f423502e71900302dde696f8bff84bf89a1cb
262+
url: "https://pub.dev"
263+
source: hosted
264+
version: "1.1.0"
257265
sdks:
258-
dart: ">=3.3.0 <4.0.0"
266+
dart: ">=3.5.0 <4.0.0"
259267
flutter: ">=3.18.0-18.0.pre.54"

packages/instabug_dio_interceptor/lib/instabug_dio_interceptor.dart

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,30 @@ class InstabugDioInterceptor extends Interceptor {
66
static final NetworkLogger _networklogger = NetworkLogger();
77

88
@override
9-
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
9+
Future<void> onRequest(
10+
RequestOptions options, RequestInterceptorHandler handler,) async {
11+
final headers = options.headers;
12+
final startTime = DateTime.now();
13+
// ignore: invalid_use_of_internal_member
14+
final w3Header = await _networklogger.getW3CHeader(
15+
headers, startTime.millisecondsSinceEpoch,);
16+
if (w3Header?.isW3cHeaderFound == false &&
17+
w3Header?.w3CGeneratedHeader != null) {
18+
headers['traceparent'] = w3Header?.w3CGeneratedHeader;
19+
}
20+
options.headers = headers;
1021
final data = NetworkData(
11-
startTime: DateTime.now(),
12-
url: options.uri.toString(),
13-
method: options.method,
14-
);
22+
startTime: startTime,
23+
url: options.uri.toString(),
24+
w3cHeader: w3Header,
25+
method: options.method,);
1526
_requests[options.hashCode] = data;
1627
handler.next(options);
1728
}
1829

1930
@override
2031
void onResponse(
21-
Response<dynamic> response,
22-
ResponseInterceptorHandler handler,
23-
) {
32+
Response<dynamic> response, ResponseInterceptorHandler handler,) {
2433
final data = _map(response);
2534
_networklogger.networkLog(data);
2635
handler.next(response);
@@ -67,9 +76,7 @@ class InstabugDioInterceptor extends Interceptor {
6776

6877
var responseBodySize = 0;
6978
if (responseHeaders.containsKey('content-length')) {
70-
try {
71-
responseBodySize = int.parse(responseHeaders['content-length'] ?? '0');
72-
} catch (_) {}
79+
responseBodySize = int.parse(responseHeaders['content-length'][0] ?? '0');
7380
} else if (response.data != null) {
7481
responseBodySize = response.data.toString().length;
7582
}

packages/instabug_dio_interceptor/test/instabug_dio_interceptor_test.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:instabug_dio_interceptor/instabug_dio_interceptor.dart';
55
import 'package:instabug_flutter/instabug_flutter.dart';
66
import 'package:instabug_flutter/src/generated/instabug.api.g.dart';
77
import 'package:mockito/annotations.dart';
8+
import 'package:mockito/mockito.dart';
89

910
import 'instabug_dio_interceptor_test.mocks.dart';
1011
import 'mock_adapter.dart';
@@ -15,16 +16,15 @@ class MyInterceptor extends InstabugDioInterceptor {
1516
int errorCount = 0;
1617

1718
@override
18-
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
19+
Future<void> onRequest(
20+
RequestOptions options, RequestInterceptorHandler handler,) async {
1921
requestCount++;
2022
super.onRequest(options, handler);
2123
}
2224

2325
@override
2426
void onResponse(
25-
Response<dynamic> response,
26-
ResponseInterceptorHandler handler,
27-
) {
27+
Response<dynamic> response, ResponseInterceptorHandler handler,) {
2828
resposneCount++;
2929
super.onResponse(response, handler);
3030
}
@@ -53,6 +53,11 @@ void main() {
5353
setUpAll(() {
5454
Instabug.$setHostApi(mHost);
5555
NetworkLogger.$setHostApi(mHost);
56+
when(mHost.isW3CFeatureFlagsEnabled()).thenAnswer((_)=>Future<Map<String,bool>>.value(<String, bool>{
57+
'isW3cCaughtHeaderEnabled': true,
58+
'isW3cExternalGeneratedHeaderEnabled': true,
59+
'isW3cExternalTraceIDEnabled': true,
60+
}),);
5661
});
5762

5863
setUp(() {

packages/instabug_dio_interceptor/test/mock_adapter.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ class MockAdapter implements HttpClientAdapter {
1111
final IOHttpClientAdapter _adapter = IOHttpClientAdapter();
1212

1313
@override
14-
Future<ResponseBody> fetch(
15-
RequestOptions options,
16-
Stream<Uint8List>? requestStream,
17-
Future<dynamic>? cancelFuture,
18-
) async {
14+
Future<ResponseBody> fetch(RequestOptions options,
15+
Stream<Uint8List>? requestStream, Future<dynamic>? cancelFuture,) async {
1916
final uri = options.uri;
2017

2118
if (uri.host == mockHost) {

scripts/init.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
if [ -d "android" ]; then
4+
rm -rf "android"
5+
echo "Folder android and its contents removed"
6+
fi
7+
8+
if [ -d "ios" ]; then
9+
rm -rf "ios"
10+
echo "Folder ios and its contents removed"
11+
fi
12+
13+
14+
if [ -d "build" ]; then
15+
rm -rf "build"
16+
echo "Folder build and its contents removed"
17+
fi
18+
19+
if [ -d "lib" ]; then
20+
rm -rf "lib"
21+
echo "Folder lib and its contents removed"
22+
fi
23+
24+
if [ -d "test" ]; then
25+
rm -rf "test"
26+
echo "Folder test and its contents removed"
27+
fi
28+
29+
30+
if command -v melos &> /dev/null
31+
then
32+
echo "Melos found"
33+
else
34+
echo "Melos not found"
35+
dart pub global activate melos && echo 'export PATH="$PATH:$HOME/.pub-cache/bin"' >> $BASH_ENV
36+
fi
37+
38+
39+
melos bootstrap
40+
melos dart_bootstrap
41+
melos pigeon --no-select
42+
melos generate --no-select
43+
melos pods --no-select

0 commit comments

Comments
 (0)