kubesense_dio 1.0.0
kubesense_dio: ^1.0.0 copied to clipboard
Support tracking and distributed tracing of Dio network requests in Kubesense.
example/lib/main.dart
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025-Present Datadog, Inc.
// Modifications copyright 2026 KubeSense Technologies.
import 'dart:io';
import 'package:kubesense_common_test/kubesense_common_test.dart';
import 'package:kubesense_dio/kubesense_dio.dart';
import 'package:kubesense_flutter_plugin/kubesense_flutter_plugin.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'screens/instrumentation_scenario.dart';
// import 'scenario_config.dart';
// import 'scenario_select_screen.dart';
// This file sets up a different application for testing auto instrumentation
// Using the widgets in this library by themselves won't send any RUM events
// but, by utilizing this entry point instead, we can test that
// auto-instrumentation added to an existing app gives us the expected results.
TestingConfiguration? testingConfiguration;
Future<void> main() async {
final merge = kIsWeb ? <String, String>{} : Platform.environment;
await dotenv.load(mergeWith: merge);
var clientToken = dotenv.get('KUBESENSE_CLIENT_TOKEN', fallback: '');
var applicationId = dotenv.maybeGet('KUBESENSE_APPLICATION_ID');
String? customEndpoint = dotenv.maybeGet('KUBESENSE_RUM_ENDPOINT');
if (testingConfiguration != null) {
if (testingConfiguration!.customEndpoint != null) {
customEndpoint = testingConfiguration!.customEndpoint;
}
if (testingConfiguration!.clientToken != null) {
clientToken = testingConfiguration!.clientToken!;
}
if (testingConfiguration!.applicationId != null) {
applicationId = testingConfiguration!.applicationId;
}
}
final firstPartyHosts = ['kubesense.ai'];
if (testingConfiguration != null) {
firstPartyHosts.addAll(testingConfiguration!.firstPartyHosts);
}
KubesenseSdk.instance.sdkVerbosity = CoreLoggerLevel.debug;
final configuration = KubesenseConfiguration(
kubesenseRumEndpoint: customEndpoint ?? 'us2.kubesense.ai',
clientToken: clientToken,
env: dotenv.get('KUBESENSE_ENV', fallback: ''),
uploadFrequency: UploadFrequency.frequent,
batchSize: BatchSize.small,
nativeCrashReportEnabled: true,
firstPartyHosts: firstPartyHosts,
loggingConfiguration: KubesenseLoggingConfiguration(),
rumConfiguration: applicationId != null
? KubesenseRumConfiguration(
detectLongTasks: false,
applicationId: applicationId,
traceSampleRate: 100,
trackResourceHeaders: ResourceHeadersExtractor(
captureHeaders: [
'accept-ranges',
'content-disposition',
'server',
'user-agent',
'via',
'x-cache-hits',
'x-served-by',
'x-kubesense-trace-id',
'x-kubesense-parent-id',
'x-kubesense-origin',
'traceparent',
],
),
)
: null,
);
if (testingConfiguration != null) {
// Add clear text if we're running an actual test.
configuration.additionalConfig['_kubesense.needsClearTextHttp'] = true;
}
await KubesenseSdk.runApp(configuration, TrackingConsent.granted, () async {
final dio = Dio(BaseOptions(
validateStatus: (status) => status != null && status < 300,
))
..addKubesenseInterceptor(
KubesenseSdk.instance,
);
// User for testing baggage headers
KubesenseSdk.instance.setUserInfo(id: 'integration_test_user');
KubesenseSdk.instance.setAccountInfo(id: 'integration_test_account');
runApp(KubesenseAutoIntegrationTestApp(dio: dio));
});
}
class KubesenseAutoIntegrationTestApp extends StatelessWidget {
final Dio dio;
const KubesenseAutoIntegrationTestApp({super.key, required this.dio});
@override
Widget build(BuildContext context) {
final navObserver =
KubesenseNavigationObserver(kubesenseSdk: KubesenseSdk.instance);
return KubesenseNavigationObserverProvider(
navObserver: navObserver,
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
navigatorObservers: [
navObserver,
],
home: InstrumentationScenario(dio: dio),
),
);
}
}