Skip to content

Commit

Permalink
feat(logging): default remote config (#3643)
Browse files Browse the repository at this point in the history
* feat(logging): add cloudwatch logger plugin

* feat(logging): default remote config

chore: moved declarations to improve dart syntax

chore: use logger instead of print statement

chore: added sigv4signer to sign the http request

chore: switched Future.delayed to Timer and implemented jsonserializable

chore: roll back unnecessary changes from cloudwatch_logger_plugin

chore: removed an extra line of fetchAndCacheConstraintFromEndpoint

chore: moved saveConstraintLocally out of fetchConstraintFromEndpoint and moved into fetchAndCacheFromEndpoint

fix: seeing if moving the queued_item_store_test.dart file into main test directory will fix testing issue

Revert "fix: seeing if moving the queued_item_store_test.dart file into main test directory will fix testing issue"

This reverts commit f033713.

chore: added ignore line for try catch block and removed unnecessary method

chore: added web implementation of remote_constraint_provider

fix: try changing path provider dependency to fix test

fix: added flutter sdk to dependencies

chore: generated workflows after adding dependencies

fix: fixed indentation issue in pubspec

chore: generate new workflows

chore: changed file and folder names for file_storage

chore: fix import for renamed files, added region as a required field to DefaultRemoteConfiguration and fixed scope variable to adjust for that. Also moved scope variable to inside fetchConstraintFromEndpoint method

replaced AmplifyLogger with AWSLoggerMixin instead.

chore: made signer private and used AWSHttpRequest.get method directly rather than calling it through AWSHttpRequest

use stoppable timer instead of while loop

chore: updated LoggingConstraint class to match the remote config file

chore: added AWSHeaders.accept to the headers for the request

chore: added public documentation and trailing comma

chore: removed unnecessary time checks in the getter if statement

chore: removed initialize() method and put it into the constructor

chore: removed unnecessary lastUpdate variable now that it is no longer checked in the getter

chore: added a comment and removed flutter plugin files

chore: fixed some formatting

chore: removed the InSeconds part of the refreshIntervalInSeconds property because it's a Duration, which isn't specific to seconds

chore: fixed comment to reflect change in fetchInterval and flushInterval property name change

chore: make isRunning variable privatge

chore: corrected error handling in the fetchAndCacheConstraintFromEndpoint method

chore: add license headers to storage implementations

chore: added path for join() method to ensure windows compatibility

chore: removed dart:io dependency, changed HttpHeaders.acceptHeader to AWSHeaders.accept, changed getter to just retrieve from cache

chore: remove flutter dependency

chore: removed http and replaced with AWSHttpClient

chore: removed unnecessary stop method

chore: remove .flutter-plugins files

chore: moved constructor initializers into a separate init function so that I can use async await

chore: added local storage check before fetching in the initialize function

chore: refactored DefaultRemoteLoggingConstraintProvider to separate createRequest from the rest of the class, allowing for signed and unsigned requests to be sent

chore: used private global variable for local storage key

chore: refacted fileStore dart implementation

* chore: dart FileStorage refactor and unit tests added

* chore: made the Future.delayed more explicit

* chore: added flutter path provider to amplify_logging_cloudwatch

* chore: removed fluter dependency

* chore: remove flutter plugin files from git ignore now that flutter dependency is removed

* chore: changed names of file storage functions, logger level, and removed flutter plugin gitignore

* chore: remove flutter plugin .gitignore

* chore: Added completer, removed conditional import, added closeable, removed credentials provider from base class and only added to defaultconstraintprovider, changed fileStorage to use FileStorage instead of just FileStorageImpl, removed isRunning since it is never false after the first run, added completer so that delay isn't needed for tests, removed the catching of errors, added credentials provider parameter to awssigv4signer,

* chore: updated aft workflows

* Update packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/plugin_config.dart

Co-authored-by: Dillon Nys <24740863+dnys1@users.noreply.github.com>

* Update packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/plugin_config.dart

Co-authored-by: Dillon Nys <24740863+dnys1@users.noreply.github.com>

* chore: removed manual toJson and replace jsonSerializable with zAmplifySerializable

* Update packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/file_storage/file_storage.dart

Co-authored-by: Dillon Nys <24740863+dnys1@users.noreply.github.com>

* Update packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/file_storage/file_storage.dart

Co-authored-by: Dillon Nys <24740863+dnys1@users.noreply.github.com>

* chore: use factory constructor and _fileStorage might be null in remoteConstraint

* chore: fixed local storage variable and method name and also added and fixed tests

* Update packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/remote_constraint_provider.dart

Co-authored-by: Dillon Nys <24740863+dnys1@users.noreply.github.com>

* Update packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/remote_constraint_provider.dart

Co-authored-by: Dillon Nys <24740863+dnys1@users.noreply.github.com>

* Update packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/remote_constraint_provider.dart

Co-authored-by: Dillon Nys <24740863+dnys1@users.noreply.github.com>

* Update packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/remote_constraint_provider.dart

Co-authored-by: Dillon Nys <24740863+dnys1@users.noreply.github.com>

* chore: add AWSSerializable and added comment to test to address Future.delayed

* chore: fixed some formatting and removed todo comment

* chore: removed unnecessary comments

* Update packages/logging_cloudwatch/aws_logging_cloudwatch/test/remote_constraint_provider_test.dart

Co-authored-by: NikaHsn <nika.hasani@gmail.com>

* Update packages/logging_cloudwatch/aws_logging_cloudwatch/test/remote_constraint_provider_test.dart

Co-authored-by: NikaHsn <nika.hasani@gmail.com>

* Update packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/file_storage/file_storage.dart

Co-authored-by: NikaHsn <nika.hasani@gmail.com>

---------

Co-authored-by: Nika Hassani <nikaws@amazon.com>
Co-authored-by: Dillon Nys <24740863+dnys1@users.noreply.github.com>
Co-authored-by: NikaHsn <nika.hasani@gmail.com>
  • Loading branch information
4 people authored Sep 20, 2023
1 parent ee143fe commit 79725fb
Show file tree
Hide file tree
Showing 11 changed files with 665 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';
import 'package:path_provider/path_provider.dart';

/// {@template flutter_path_provider}
/// A [AppPathProvider] that uses the [path_provider](https://pub.dev/packages/path_provider)
/// package to get the application support and temporary paths.
/// {@endtemplate}
class FlutterPathProvider implements AppPathProvider {
@override
Future<String> getApplicationSupportPath() async {
final directory = await getApplicationSupportDirectory();
return directory.path;
}

@override
Future<String> getTemporaryPath() async {
final directory = await getTemporaryDirectory();
return directory.path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import 'dart:async';
import 'dart:math';

import 'package:aws_common/aws_common.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:aws_logging_cloudwatch/aws_logging_cloudwatch.dart';
import 'package:aws_logging_cloudwatch/src/sdk/cloud_watch_logs.dart';
import 'package:aws_logging_cloudwatch/src/stoppable_timer.dart';
Expand Down Expand Up @@ -76,9 +76,9 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin
region: pluginConfig.region,
credentialsProvider: credentialsProvider,
) {
_timer = pluginConfig.flushIntervalInSeconds > Duration.zero
_timer = pluginConfig.flushInterval > Duration.zero
? StoppableTimer(
duration: pluginConfig.flushIntervalInSeconds,
duration: pluginConfig.flushInterval,
callback: _startSyncingIfNotInProgress,
onError: _onTimerError,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';
import 'package:aws_logging_cloudwatch/src/file_storage/file_storage_stub.dart'
if (dart.library.io) 'file_storage_vm.dart'
if (dart.library.html) 'file_storage_web.dart';

/// File storage interface for saving and loading constraint locally
abstract interface class FileStorage {
/// Default Constructor or FileStorage
factory FileStorage(AppPathProvider pathProvider) = FileStorageImpl;

/// Save constraint locally to file
Future<void> save(String fileName, String data);

/// Load constraint from file
Future<String?> load(String fileName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';
import 'package:aws_logging_cloudwatch/src/file_storage/file_storage.dart';

/// File storage implementation for saving and loading constraint locally
class FileStorageImpl implements FileStorage {
/// File storage implementation for saving and loading constraint locally
FileStorageImpl(this.pathProvider);

/// Path provider to get the application support path
final AppPathProvider pathProvider;

@override
Future<String?> load(String fileName) async {
throw UnimplementedError();
}

@override
Future<void> save(String fileName, String content) async {
throw UnimplementedError();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'dart:io';

import 'package:amplify_core/amplify_core.dart';
import 'package:aws_logging_cloudwatch/src/file_storage/file_storage.dart';
import 'package:path/path.dart' as p;

/// File storage implementation for saving and loading constraint locally
class FileStorageImpl implements FileStorage {
/// File storage implementation for saving and loading constraint locally
FileStorageImpl(this.pathProvider);

/// Path provider to get the application support path
final AppPathProvider pathProvider;

@override
Future<String?> load(String fileName) async {
final file =
File(p.join(await pathProvider.getApplicationSupportPath(), fileName));
if (await file.exists()) {
return file.readAsString();
}
return null;
}

@override
Future<void> save(String fileName, String content) async {
final file =
File(p.join(await pathProvider.getApplicationSupportPath(), fileName));
await file.writeAsString(content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'dart:html';

import 'package:amplify_core/amplify_core.dart';
import 'package:aws_logging_cloudwatch/src/file_storage/file_storage.dart';

/// File storage implementation for saving and loading constraint locally
class FileStorageImpl implements FileStorage {
/// File storage implementation for saving and loading constraint locally
// ignore: avoid_unused_constructor_parameters
FileStorageImpl(AppPathProvider pathProvider);

static const _prefix = 'aws.cloudwatch';

@override
Future<String?> load(String fileName) async {
return window.localStorage['$_prefix.$fileName'];
}

@override
Future<void> save(String fileName, String content) async {
window.localStorage['$_prefix.$fileName'] = content;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:aws_common/aws_common.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:aws_logging_cloudwatch/aws_logging_cloudwatch.dart';

part 'plugin_config.g.dart';

/// {@template aws_logging_cloudwatch.cloudwatch_logger_plugin_configuration}
/// The configuration for `CloudWatchLoggerPlugin`.
/// {@endtemplate}
Expand All @@ -15,7 +17,7 @@ class CloudWatchLoggerPluginConfiguration with AWSDebuggable {
required this.localLoggingConstraint,
this.enable = true,
this.localStoreMaxSizeInMB = 5,
this.flushIntervalInSeconds = const Duration(seconds: 60),
this.flushInterval = const Duration(seconds: 60),
this.defaultRemoteConfiguration,
});

Expand All @@ -31,8 +33,8 @@ class CloudWatchLoggerPluginConfiguration with AWSDebuggable {
/// The max size of the local store in MB to be used for storing logs locally.
final int localStoreMaxSizeInMB;

/// The duration in seconds for sending locally stored logs to CloudWatch.
final Duration flushIntervalInSeconds;
/// The duration for sending locally stored logs to CloudWatch.
final Duration flushInterval;

/// {@macro aws_logging_cloudwatch.logging_constraint}
final LoggingConstraint localLoggingConstraint;
Expand All @@ -47,13 +49,59 @@ class CloudWatchLoggerPluginConfiguration with AWSDebuggable {
/// {@template aws_logging_cloudwatch.logging_constraint}
/// The logging constraint for sending logs to CloudWatch.
/// {@endtemplate}
class LoggingConstraint with AWSDebuggable {
@zAmplifySerializable
class LoggingConstraint with AWSDebuggable, AWSSerializable {
/// {@macro aws_logging_cloudwatch.logging_constraint}
const LoggingConstraint({this.defaultLogLevel = LogLevel.error});
const LoggingConstraint({
this.defaultLogLevel = LogLevel.error,
this.categoryLogLevel,
this.userLogLevel,
});

/// Converts a [Map] to an [LoggingConstraint] instance.
factory LoggingConstraint.fromJson(Map<String, dynamic> json) =>
_$LoggingConstraintFromJson(json);

/// Converts an [LoggingConstraint] instance to a [Map].
@override
Map<String, dynamic> toJson() => _$LoggingConstraintToJson(this);

/// The default [LogLevel] for sending logs to CloudWatch.
final LogLevel defaultLogLevel;

/// The [LogLevel] for different categories.
final Map<String, LogLevel>? categoryLogLevel;

/// The [LogLevel] for different users.
final Map<String, UserLogLevel>? userLogLevel;

@override
String get runtimeTypeName => 'LoggingConstraint';
}

/// The logging constraint for user specific log level.
@zAmplifySerializable
class UserLogLevel with AWSDebuggable, AWSSerializable {
/// The logging constraint for user specific log level.
const UserLogLevel({
this.defaultLogLevel,
this.categoryLogLevel,
});

///Converts a [Map] to a [UserLogLevel] instance.
factory UserLogLevel.fromJson(Map<String, dynamic> json) =>
_$UserLogLevelFromJson(json);

/// Converts a [UserLogLevel] instance to a [Map].
@override
Map<String, dynamic> toJson() => _$UserLogLevelToJson(this);

/// The default [LogLevel] for sending logs to CloudWatch.
final LogLevel? defaultLogLevel;

/// The [LogLevel] for different categories.
final Map<String, LogLevel>? categoryLogLevel;

@override
String get runtimeTypeName => 'UserLogLevel';
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 79725fb

Please sign in to comment.