api and log refactor

This commit is contained in:
John Smith
2022-07-01 12:13:52 -04:00
parent f409c84778
commit c106d324c8
25 changed files with 662 additions and 501 deletions

View File

@@ -7,7 +7,6 @@ Future<VeilidConfig> getDefaultVeilidConfig() async {
return VeilidConfig(
programName: "Veilid Plugin Test",
namespace: "",
apiLogLevel: VeilidConfigLogLevel.info,
capabilities: VeilidConfigCapabilities(
protocolUDP: !kIsWeb,
protocolConnectTCP: !kIsWeb,

View File

@@ -59,7 +59,7 @@ VeilidConfigLogLevel convertToVeilidConfigLogLevel(LogLevel? level) {
void setRootLogLevel(LogLevel? level) {
Loggy('').level = getLogOptions(level);
Veilid.instance.changeApiLogLevel(convertToVeilidConfigLogLevel(level));
Veilid.instance.changeLogLevel("all", convertToVeilidConfigLogLevel(level));
}
void initLoggy() {
@@ -81,22 +81,26 @@ void main() {
logging: VeilidWASMConfigLogging(
performance: VeilidWASMConfigLoggingPerformance(
enabled: true,
level: VeilidLogLevel.trace,
level: VeilidConfigLogLevel.debug,
logsInTimings: true,
logsInConsole: false)));
logsInConsole: true),
api: VeilidWASMConfigLoggingApi(
enabled: true, level: VeilidConfigLogLevel.info)));
Veilid.instance.configureVeilidPlatform(platformConfig.json);
} else {
var platformConfig = VeilidFFIConfig(
logging: VeilidFFIConfigLogging(
terminal: VeilidFFIConfigLoggingTerminal(
enabled: false,
level: VeilidLogLevel.trace,
level: VeilidConfigLogLevel.debug,
),
otlp: VeilidFFIConfigLoggingOtlp(
enabled: false,
level: VeilidLogLevel.trace,
level: VeilidConfigLogLevel.trace,
grpcEndpoint: "localhost:4317",
serviceName: "VeilidExample")));
serviceName: "VeilidExample"),
api: VeilidFFIConfigLoggingApi(
enabled: true, level: VeilidConfigLogLevel.info)));
Veilid.instance.configureVeilidPlatform(platformConfig.json);
}

View File

@@ -13,7 +13,7 @@ import 'veilid_stub.dart'
class VeilidFFIConfigLoggingTerminal {
bool enabled;
VeilidLogLevel level;
VeilidConfigLogLevel level;
VeilidFFIConfigLoggingTerminal({
required this.enabled,
@@ -29,12 +29,12 @@ class VeilidFFIConfigLoggingTerminal {
VeilidFFIConfigLoggingTerminal.fromJson(Map<String, dynamic> json)
: enabled = json['enabled'],
level = veilidLogLevelFromJson(json['level']);
level = veilidConfigLogLevelFromJson(json['level']);
}
class VeilidFFIConfigLoggingOtlp {
bool enabled;
VeilidLogLevel level;
VeilidConfigLogLevel level;
String grpcEndpoint;
String serviceName;
@@ -56,27 +56,52 @@ class VeilidFFIConfigLoggingOtlp {
VeilidFFIConfigLoggingOtlp.fromJson(Map<String, dynamic> json)
: enabled = json['enabled'],
level = veilidLogLevelFromJson(json['level']),
level = veilidConfigLogLevelFromJson(json['level']),
grpcEndpoint = json['grpc_endpoint'],
serviceName = json['service_name'];
}
class VeilidFFIConfigLoggingApi {
bool enabled;
VeilidConfigLogLevel level;
VeilidFFIConfigLoggingApi({
required this.enabled,
required this.level,
});
Map<String, dynamic> get json {
return {
'enabled': enabled,
'level': level.json,
};
}
VeilidFFIConfigLoggingApi.fromJson(Map<String, dynamic> json)
: enabled = json['enabled'],
level = veilidConfigLogLevelFromJson(json['level']);
}
class VeilidFFIConfigLogging {
VeilidFFIConfigLoggingTerminal terminal;
VeilidFFIConfigLoggingOtlp otlp;
VeilidFFIConfigLoggingApi api;
VeilidFFIConfigLogging({required this.terminal, required this.otlp});
VeilidFFIConfigLogging(
{required this.terminal, required this.otlp, required this.api});
Map<String, dynamic> get json {
return {
'terminal': terminal.json,
'otlp': otlp.json,
'api': api.json,
};
}
VeilidFFIConfigLogging.fromJson(Map<String, dynamic> json)
: terminal = VeilidFFIConfigLoggingTerminal.fromJson(json['terminal']),
otlp = VeilidFFIConfigLoggingOtlp.fromJson(json['otlp']);
otlp = VeilidFFIConfigLoggingOtlp.fromJson(json['otlp']),
api = VeilidFFIConfigLoggingApi.fromJson(json['api']);
}
class VeilidFFIConfig {
@@ -101,7 +126,7 @@ class VeilidFFIConfig {
class VeilidWASMConfigLoggingPerformance {
bool enabled;
VeilidLogLevel level;
VeilidConfigLogLevel level;
bool logsInTimings;
bool logsInConsole;
@@ -123,25 +148,49 @@ class VeilidWASMConfigLoggingPerformance {
VeilidWASMConfigLoggingPerformance.fromJson(Map<String, dynamic> json)
: enabled = json['enabled'],
level = veilidLogLevelFromJson(json['level']),
level = veilidConfigLogLevelFromJson(json['level']),
logsInTimings = json['logs_in_timings'],
logsInConsole = json['logs_in_console'];
}
class VeilidWASMConfigLoggingApi {
bool enabled;
VeilidConfigLogLevel level;
VeilidWASMConfigLoggingApi({
required this.enabled,
required this.level,
});
Map<String, dynamic> get json {
return {
'enabled': enabled,
'level': level.json,
};
}
VeilidWASMConfigLoggingApi.fromJson(Map<String, dynamic> json)
: enabled = json['enabled'],
level = veilidConfigLogLevelFromJson(json['level']);
}
class VeilidWASMConfigLogging {
VeilidWASMConfigLoggingPerformance performance;
VeilidWASMConfigLoggingApi api;
VeilidWASMConfigLogging({required this.performance});
VeilidWASMConfigLogging({required this.performance, required this.api});
Map<String, dynamic> get json {
return {
'performance': performance.json,
'api': api.json,
};
}
VeilidWASMConfigLogging.fromJson(Map<String, dynamic> json)
: performance =
VeilidWASMConfigLoggingPerformance.fromJson(json['performance']);
VeilidWASMConfigLoggingPerformance.fromJson(json['performance']),
api = VeilidWASMConfigLoggingApi.fromJson(json['api']);
}
class VeilidWASMConfig {
@@ -899,7 +948,6 @@ class VeilidConfigCapabilities {
class VeilidConfig {
String programName;
String namespace;
VeilidConfigLogLevel apiLogLevel;
VeilidConfigCapabilities capabilities;
VeilidConfigProtectedStore protectedStore;
VeilidConfigTableStore tableStore;
@@ -909,7 +957,6 @@ class VeilidConfig {
VeilidConfig({
required this.programName,
required this.namespace,
required this.apiLogLevel,
required this.capabilities,
required this.protectedStore,
required this.tableStore,
@@ -921,7 +968,6 @@ class VeilidConfig {
return {
'program_name': programName,
'namespace': namespace,
'api_log_level': apiLogLevel.json,
'capabilities': capabilities.json,
'protected_store': protectedStore.json,
'table_store': tableStore.json,
@@ -933,7 +979,6 @@ class VeilidConfig {
VeilidConfig.fromJson(Map<String, dynamic> json)
: programName = json['program_name'],
namespace = json['namespace'],
apiLogLevel = json['api_log_level'],
capabilities = VeilidConfigCapabilities.fromJson(json['capabilities']),
protectedStore =
VeilidConfigProtectedStore.fromJson(json['protected_store']),
@@ -1253,9 +1298,9 @@ abstract class Veilid {
static late Veilid instance = getVeilid();
void configureVeilidPlatform(Map<String, dynamic> platformConfigJson);
void changeLogLevel(String layer, VeilidConfigLogLevel logLevel);
Stream<VeilidUpdate> startupVeilidCore(VeilidConfig config);
Future<VeilidState> getVeilidState();
Future<void> changeApiLogLevel(VeilidConfigLogLevel logLevel);
Future<void> shutdownVeilidCore();
Future<String> debug(String command);
String veilidVersionString();

View File

@@ -32,15 +32,15 @@ typedef _InitializeVeilidFlutterDart = void Function(Pointer<_DartPostCObject>);
// fn configure_veilid_platform(platform_config: FfiStr)
typedef _ConfigureVeilidPlatformC = Void Function(Pointer<Utf8>);
typedef _ConfigureVeilidPlatformDart = void Function(Pointer<Utf8>);
// fn change_log_level(layer: FfiStr, log_level: FfiStr)
typedef _ChangeLogLevelC = Void Function(Pointer<Utf8>, Pointer<Utf8>);
typedef _ChangeLogLevelDart = void Function(Pointer<Utf8>, Pointer<Utf8>);
// fn startup_veilid_core(port: i64, config: FfiStr)
typedef _StartupVeilidCoreC = Void Function(Int64, Pointer<Utf8>);
typedef _StartupVeilidCoreDart = void Function(int, Pointer<Utf8>);
// fn get_veilid_state(port: i64)
typedef _GetVeilidStateC = Void Function(Int64);
typedef _GetVeilidStateDart = void Function(int);
// fn change_api_log_level(port: i64, log_level: FfiStr)
typedef _ChangeApiLogLevelC = Void Function(Int64, Pointer<Utf8>);
typedef _ChangeApiLogLevelDart = void Function(int, Pointer<Utf8>);
// fn debug(port: i64, log_level: FfiStr)
typedef _DebugC = Void Function(Int64, Pointer<Utf8>);
typedef _DebugDart = void Function(int, Pointer<Utf8>);
@@ -246,9 +246,9 @@ class VeilidFFI implements Veilid {
// Shared library functions
final _FreeStringDart _freeString;
final _ConfigureVeilidPlatformDart _configureVeilidPlatform;
final _ChangeLogLevelDart _changeLogLevel;
final _StartupVeilidCoreDart _startupVeilidCore;
final _GetVeilidStateDart _getVeilidState;
final _ChangeApiLogLevelDart _changeApiLogLevel;
final _ShutdownVeilidCoreDart _shutdownVeilidCore;
final _DebugDart _debug;
final _VeilidVersionStringDart _veilidVersionString;
@@ -261,15 +261,15 @@ class VeilidFFI implements Veilid {
_configureVeilidPlatform = dylib.lookupFunction<
_ConfigureVeilidPlatformC,
_ConfigureVeilidPlatformDart>('configure_veilid_platform'),
_changeLogLevel =
dylib.lookupFunction<_ChangeLogLevelC, _ChangeLogLevelDart>(
'change_log_level'),
_startupVeilidCore =
dylib.lookupFunction<_StartupVeilidCoreC, _StartupVeilidCoreDart>(
'startup_veilid_core'),
_getVeilidState =
dylib.lookupFunction<_GetVeilidStateC, _GetVeilidStateDart>(
'get_veilid_state'),
_changeApiLogLevel =
dylib.lookupFunction<_ChangeApiLogLevelC, _ChangeApiLogLevelDart>(
'change_api_log_level'),
_shutdownVeilidCore =
dylib.lookupFunction<_ShutdownVeilidCoreC, _ShutdownVeilidCoreDart>(
'shutdown_veilid_core'),
@@ -297,6 +297,17 @@ class VeilidFFI implements Veilid {
malloc.free(nativePlatformConfig);
}
@override
void changeLogLevel(String layer, VeilidConfigLogLevel logLevel) {
var nativeLogLevel =
jsonEncode(logLevel.json, toEncodable: veilidApiToEncodable)
.toNativeUtf8();
var nativeLayer = layer.toNativeUtf8();
_changeLogLevel(nativeLayer, nativeLogLevel);
malloc.free(nativeLayer);
malloc.free(nativeLogLevel);
}
@override
Stream<VeilidUpdate> startupVeilidCore(VeilidConfig config) {
var nativeConfig =
@@ -317,18 +328,6 @@ class VeilidFFI implements Veilid {
return processFutureJson(VeilidState.fromJson, recvPort.first);
}
@override
Future<void> changeApiLogLevel(VeilidConfigLogLevel logLevel) async {
var nativeLogLevel =
jsonEncode(logLevel.json, toEncodable: veilidApiToEncodable)
.toNativeUtf8();
final recvPort = ReceivePort("change_api_log_level");
final sendPort = recvPort.sendPort;
_changeApiLogLevel(sendPort.nativePort, nativeLogLevel);
malloc.free(nativeLogLevel);
return processFutureVoid(recvPort.first);
}
@override
Future<void> shutdownVeilidCore() async {
final recvPort = ReceivePort("shutdown_veilid_core");

View File

@@ -27,6 +27,13 @@ class VeilidJS implements Veilid {
wasm, "configure_veilid_platform", [platformConfigJsonString]);
}
@override
void changeLogLevel(String layer, VeilidConfigLogLevel logLevel) {
var logLevelJsonString =
jsonEncode(logLevel.json, toEncodable: veilidApiToEncodable);
js_util.callMethod(wasm, "change_log_level", [layer, logLevelJsonString]);
}
@override
Stream<VeilidUpdate> startupVeilidCore(VeilidConfig config) async* {
var streamController = StreamController<VeilidUpdate>();
@@ -53,12 +60,6 @@ class VeilidJS implements Veilid {
js_util.callMethod(wasm, "get_veilid_state", []))));
}
@override
Future<void> changeApiLogLevel(VeilidConfigLogLevel logLevel) {
return _wrapApiPromise(js_util.callMethod(wasm, "change_api_log_level",
[jsonEncode(logLevel.json, toEncodable: veilidApiToEncodable)]));
}
@override
Future<void> shutdownVeilidCore() {
return _wrapApiPromise(

View File

@@ -7,7 +7,9 @@ use lazy_static::*;
use opentelemetry::sdk::*;
use opentelemetry::*;
use opentelemetry_otlp::WithExportConfig;
use parking_lot::Mutex;
use serde::*;
use std::collections::BTreeMap;
use std::os::raw::c_char;
use std::sync::Arc;
use tracing::*;
@@ -15,9 +17,10 @@ use tracing_subscriber::prelude::*;
use tracing_subscriber::*;
// Globals
lazy_static! {
static ref VEILID_API: AsyncMutex<Option<veilid_core::VeilidAPI>> = AsyncMutex::new(None);
static ref FILTERS: Mutex<BTreeMap<&'static str, veilid_core::VeilidLayerFilter>> =
Mutex::new(BTreeMap::new());
}
async fn get_veilid_api() -> Result<veilid_core::VeilidAPI, veilid_core::VeilidAPIError> {
@@ -35,17 +38,6 @@ async fn take_veilid_api() -> Result<veilid_core::VeilidAPI, veilid_core::Veilid
.ok_or(veilid_core::VeilidAPIError::NotInitialized)
}
fn logfilter<T: AsRef<str>, V: AsRef<[T]>>(metadata: &Metadata, ignore_list: V) -> bool {
// Skip filtered targets
!match (metadata.target(), ignore_list.as_ref()) {
(path, ignore) if !ignore.is_empty() => {
// Check that the module path does not match any ignore filters
ignore.iter().any(|v| path.starts_with(v.as_ref()))
}
_ => false,
}
}
/////////////////////////////////////////
// FFI Helpers
@@ -75,21 +67,28 @@ macro_rules! check_err_json {
#[derive(Debug, Deserialize, Serialize)]
pub struct VeilidFFIConfigLoggingTerminal {
pub enabled: bool,
pub level: veilid_core::VeilidLogLevel,
pub level: veilid_core::VeilidConfigLogLevel,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct VeilidFFIConfigLoggingOtlp {
pub enabled: bool,
pub level: veilid_core::VeilidLogLevel,
pub level: veilid_core::VeilidConfigLogLevel,
pub grpc_endpoint: String,
pub service_name: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct VeilidFFIConfigLoggingApi {
pub enabled: bool,
pub level: veilid_core::VeilidConfigLogLevel,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct VeilidFFIConfigLogging {
pub terminal: VeilidFFIConfigLoggingTerminal,
pub otlp: VeilidFFIConfigLoggingOtlp,
pub api: VeilidFFIConfigLoggingApi,
}
#[derive(Debug, Deserialize, Serialize)]
@@ -149,40 +148,24 @@ pub extern "C" fn configure_veilid_platform(platform_config: FfiStr) {
.expect("failed to deserialize plaform config json");
// Set up subscriber and layers
let mut ignore_list = Vec::<String>::new();
for ig in veilid_core::DEFAULT_LOG_IGNORE_LIST {
ignore_list.push(ig.to_owned());
}
let subscriber = Registry::default();
let mut layers = Vec::new();
let mut filters = (*FILTERS).lock();
// Terminal logger
let subscriber = subscriber.with(if platform_config.logging.terminal.enabled {
let terminal_max_log_level: level_filters::LevelFilter = platform_config
.logging
.terminal
.level
.to_tracing_level()
.into();
let ignore_list = ignore_list.clone();
Some(
fmt::Layer::new()
.compact()
.with_writer(std::io::stdout)
.with_filter(terminal_max_log_level)
.with_filter(filter::FilterFn::new(move |metadata| {
logfilter(metadata, &ignore_list)
})),
)
} else {
None
});
if platform_config.logging.terminal.enabled {
let filter =
veilid_core::VeilidLayerFilter::new(platform_config.logging.terminal.level, None);
let layer = fmt::Layer::new()
.compact()
.with_writer(std::io::stdout)
.with_filter(filter.clone());
filters.insert("terminal", filter);
layers.push(layer.boxed());
};
// OpenTelemetry logger
let subscriber = subscriber.with(if platform_config.logging.otlp.enabled {
let otlp_max_log_level: level_filters::LevelFilter =
platform_config.logging.otlp.level.to_tracing_level().into();
if platform_config.logging.otlp.enabled {
let grpc_endpoint = platform_config.logging.otlp.grpc_endpoint.clone();
cfg_if! {
@@ -218,21 +201,23 @@ pub extern "C" fn configure_veilid_platform(platform_config: FfiStr) {
.map_err(|e| format!("failed to install OpenTelemetry tracer: {}", e))
.unwrap();
let ignore_list = ignore_list.clone();
Some(
tracing_opentelemetry::layer()
.with_tracer(tracer)
.with_filter(otlp_max_log_level)
.with_filter(filter::FilterFn::new(move |metadata| {
logfilter(metadata, &ignore_list)
})),
)
} else {
None
});
let filter = veilid_core::VeilidLayerFilter::new(platform_config.logging.otlp.level, None);
let layer = tracing_opentelemetry::layer()
.with_tracer(tracer)
.with_filter(filter.clone());
filters.insert("otlp", filter);
layers.push(layer.boxed());
}
// API logger (always add layer, startup will init this if it is enabled in settings)
let subscriber = subscriber.with(veilid_core::ApiTracingLayer::get());
// API logger
if platform_config.logging.api.enabled {
let filter = veilid_core::VeilidLayerFilter::new(platform_config.logging.api.level, None);
let layer = veilid_core::ApiTracingLayer::get().with_filter(filter.clone());
filters.insert("api", filter);
layers.push(layer.boxed());
}
let subscriber = subscriber.with(layers);
subscriber
.try_init()
@@ -240,6 +225,32 @@ pub extern "C" fn configure_veilid_platform(platform_config: FfiStr) {
.expect("failed to initalize ffi platform");
}
#[no_mangle]
#[instrument(level = "debug")]
pub extern "C" fn change_log_level(layer: FfiStr, log_level: FfiStr) {
// get layer to change level on
let layer = layer.into_opt_string().unwrap_or("all".to_owned());
let layer = if layer == "all" { "".to_owned() } else { layer };
// get log level to change layer to
let log_level = log_level.into_opt_string();
let log_level: veilid_core::VeilidConfigLogLevel =
veilid_core::deserialize_opt_json(log_level).unwrap();
// change log level on appropriate layer
let filters = (*FILTERS).lock();
if layer.is_empty() {
// Change all layers
for f in filters.values() {
f.set_max_level(log_level);
}
} else {
// Change a specific layer
let f = filters.get(layer.as_str()).unwrap();
f.set_max_level(log_level);
}
}
#[no_mangle]
#[instrument]
pub extern "C" fn startup_veilid_core(port: i64, config: FfiStr) {
@@ -291,20 +302,6 @@ pub extern "C" fn get_veilid_state(port: i64) {
});
}
#[no_mangle]
#[instrument(level = "debug")]
pub extern "C" fn change_api_log_level(port: i64, log_level: FfiStr) {
let log_level = log_level.into_opt_string();
DartIsolateWrapper::new(port).spawn_result_json(async move {
let log_level: veilid_core::VeilidConfigLogLevel =
veilid_core::deserialize_opt_json(log_level)?;
//let veilid_api = get_veilid_api().await?;
//veilid_api.change_api_log_level(log_level).await;
veilid_core::ApiTracingLayer::change_api_log_level(log_level.to_veilid_log_level());
APIRESULT_VOID
});
}
#[no_mangle]
#[instrument]
pub extern "C" fn shutdown_veilid_core(port: i64) {