Skip to main content

kubesense_tracking_http_client 1.0.0 copy "kubesense_tracking_http_client: ^1.0.0" to clipboard
kubesense_tracking_http_client: ^1.0.0 copied to clipboard

A wrapping implementation of HttpClient for tracking resources with 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 2016-Present Datadog, Inc.
// Modifications copyright 2026 KubeSense Technologies.

import 'dart:io';

import 'package:kubesense_common_test/kubesense_common_test.dart';
import 'package:kubesense_flutter_plugin/kubesense_flutter_plugin.dart';
import 'package:kubesense_tracking_http_client/kubesense_tracking_http_client.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.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;

class ClientListener extends KubesenseTrackingHttpClientListener {
  @override
  void requestStarted(
      {required Object resourceKey,
      required HttpClientRequest request,
      required Map<String, Object?> userAttributes}) {
    if (kDebugMode) {
      print('($resourceKey) Request started');
    }
  }

  @override
  void responseFinished(
      {required Object resourceKey,
      required HttpClientResponse response,
      required Map<String, Object?> userAttributes,
      Object? error}) {
    if (kDebugMode) {
      print('($resourceKey) Request finished');
    }
  }
}

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;
  }

  if (RumAutoInstrumentationScenarioConfig.instance.enableIoHttpTracking) {
    configuration.enableHttpTracking(
      clientListener: ClientListener(),
    );
  }

  await KubesenseSdk.runApp(configuration, TrackingConsent.granted, () async {
    // User for testing baggage headers
    KubesenseSdk.instance.setUserInfo(id: 'integration_test_user');
    KubesenseSdk.instance.setAccountInfo(id: 'integration_test_account');

    runApp(const KubesenseAutoIntegrationTestApp());
  });
}

class KubesenseAutoIntegrationTestApp extends StatelessWidget {
  const KubesenseAutoIntegrationTestApp({Key? key}) : super(key: key);

  @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: const ScenarioSelectScreen()),
    );
  }
}
0
likes
140
points
42
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A wrapping implementation of HttpClient for tracking resources with Kubesense

Homepage

License

unknown (license)

Dependencies

flutter, http, kubesense_flutter_plugin, uuid

More

Packages that depend on kubesense_tracking_http_client