public NancyAdapterModuleMetricsTests()
        {
            this.config = new MetricsConfig(this.context);

            this.browser = new Browser(with =>
            {
                with.ApplicationStartup((c, p) =>
                {
                    this.config.WithNancy(p);
                    with.Module(new TestModule(this.context.Clock));
                });
            });
        }
        public NancyAdapterGlobalMetrics()
        {
            this.config = new MetricsConfig(this.context);
            this.browser = new Browser(with =>
            {
                with.ApplicationStartup((c, p) =>
                {
                    this.config.WithNancy(p);
                });

                with.Module(new TestModule(this.context.Clock));
                with.Module(new ActiveRequestsModule(this.requestTrigger.Task, result1, result2));
            });
        }
예제 #3
0
        public OwinMiddlewareTests()
        {
            this.config = new MetricsConfig(this.context);

            this.server = TestServer.Create(app =>
            {
                this.config.WithOwin(m => app.Use(m));

                app.Run(ctx =>
                {
                    this.context.Clock.Advance(TimeUnit.Milliseconds, timePerRequest);
                    if (ctx.Request.Path.ToString() == "/test/action")
                    {
                        return ctx.Response.WriteAsync("response");
                    }

                    if (ctx.Request.Path.ToString() == "/test/error")
                    {
                        ctx.Response.StatusCode = 500;
                        return ctx.Response.WriteAsync("response");
                    }

                    if (ctx.Request.Path.ToString() == "/test/size")
                    {
                        return ctx.Response.WriteAsync("response");
                    }

                    if (ctx.Request.Path.ToString() == "/test/post")
                    {
                        return ctx.Response.WriteAsync("response");
                    }

                    ctx.Response.StatusCode = 404;
                    return ctx.Response.WriteAsync("not found");
                });

            });
        }
예제 #4
0
 /// <summary>
 /// Add Metrics Middleware to the Owin pipeline.
 /// Sample: Metric.Config.WithOwin( m => app.Use(m))
 /// Where app is the IAppBuilder
 /// </summary>
 /// <param name="config">Chainable configuration object.</param>
 /// <param name="middlewareRegistration">Action used to register middleware. This should generally be app.Use(middleware)</param>
 /// <returns>Chainable configuration object.</returns>
 public static MetricsConfig WithOwin(this MetricsConfig config, Action <object> middlewareRegistration)
 {
     return(config.WithOwin(middlewareRegistration, owin =>
                            owin.WithRequestMetricsConfig()
                            .WithMetricsEndpoint()));
 }
예제 #5
0
        void RegisterCommonModule(
            ContainerBuilder builder,
            bool optimizeForPerformance,
            StoreAndForward storeAndForward,
            MetricsConfig metricsConfig,
            ExperimentalFeatures experimentalFeatures)
        {
            bool            cacheTokens        = this.configuration.GetValue("CacheTokens", false);
            Option <string> workloadUri        = this.GetConfigurationValueIfExists <string>(Constants.ConfigKey.WorkloadUri);
            Option <string> workloadApiVersion = this.GetConfigurationValueIfExists <string>(Constants.ConfigKey.WorkloadAPiVersion);
            Option <string> moduleGenerationId = this.GetConfigurationValueIfExists <string>(Constants.ConfigKey.ModuleGenerationId);
            bool            hasParentEdge      = this.GetConfigurationValueIfExists <string>(Constants.ConfigKey.GatewayHostname).HasValue;

            if (!Enum.TryParse(this.configuration.GetValue("AuthenticationMode", string.Empty), true, out AuthenticationMode authenticationMode))
            {
                if (!hasParentEdge)
                {
                    // Default setting should be local auth with fallback to cloud auth
                    authenticationMode = AuthenticationMode.CloudAndScope;
                }
                else
                {
                    // If the Edge is nested and connects to a parent Edge, then we
                    // should only allow local authentication as we don't expect to
                    // have internet connectivity.
                    authenticationMode = AuthenticationMode.Scope;
                }
            }

            int      scopeCacheRefreshRateSecs = this.configuration.GetValue("DeviceScopeCacheRefreshRateSecs", 3600);
            TimeSpan scopeCacheRefreshRate     = TimeSpan.FromSeconds(scopeCacheRefreshRateSecs);

            int      scopeCacheRefreshDelaySecs = this.configuration.GetValue("DeviceScopeCacheRefreshDelaySecs", 120);
            TimeSpan scopeCacheRefreshDelay     = TimeSpan.FromSeconds(scopeCacheRefreshDelaySecs);

            string proxy       = this.configuration.GetValue("https_proxy", string.Empty);
            string productInfo = GetProductInfo();

            // Register modules
            builder.RegisterModule(
                new CommonModule(
                    productInfo,
                    this.iotHubHostname,
                    this.gatewayHostname,
                    this.edgeDeviceId,
                    this.edgeModuleId,
                    this.edgeDeviceHostName,
                    moduleGenerationId,
                    authenticationMode,
                    this.connectionString,
                    optimizeForPerformance,
                    storeAndForward.UsePersistentStorage,
                    storeAndForward.StoragePath,
                    workloadUri,
                    workloadApiVersion,
                    scopeCacheRefreshRate,
                    scopeCacheRefreshDelay,
                    cacheTokens,
                    this.trustBundle,
                    proxy,
                    metricsConfig,
                    storeAndForward.UseBackupAndRestore,
                    storeAndForward.StorageBackupPath,
                    storeAndForward.StorageMaxTotalWalSize,
                    storeAndForward.StorageMaxOpenFiles,
                    storeAndForward.StorageLogLevel,
                    experimentalFeatures.EnableNestedEdge));
        }
예제 #6
0
        public static async Task <int> MainAsync(IConfiguration configuration)
        {
            // Bring up the logger before anything else so we can log errors ASAP
            ILogger logger = SetupLogger(configuration);

            logger.LogInformation("Initializing Edge Agent.");

            VersionInfo versionInfo = VersionInfo.Get(VersionInfoFileName);

            if (versionInfo != VersionInfo.Empty)
            {
                logger.LogInformation($"Version - {versionInfo.ToString(true)}");
            }

            LogLogo(logger);

            string mode;

            string                      configSourceConfig;
            string                      backupConfigFilePath;
            int                         maxRestartCount;
            TimeSpan                    intensiveCareTime;
            int                         coolOffTimeUnitInSeconds;
            bool                        usePersistentStorage;
            string                      storagePath;
            bool                        enableNonPersistentStorageBackup;
            Option <string>             storageBackupPath = Option.None <string>();
            string                      edgeDeviceHostName;
            string                      dockerLoggingDriver;
            Dictionary <string, string> dockerLoggingOptions;
            IEnumerable <global::Docker.DotNet.Models.AuthConfig> dockerAuthConfig;
            int configRefreshFrequencySecs;
            ExperimentalFeatures experimentalFeatures;
            MetricsConfig        metricsConfig;
            DiagnosticConfig     diagnosticConfig;

            try
            {
                mode = configuration.GetValue(Constants.ModeKey, "docker");
                configSourceConfig       = configuration.GetValue <string>("ConfigSource");
                backupConfigFilePath     = configuration.GetValue <string>("BackupConfigFilePath");
                maxRestartCount          = configuration.GetValue <int>("MaxRestartCount");
                intensiveCareTime        = TimeSpan.FromMinutes(configuration.GetValue <int>("IntensiveCareTimeInMinutes"));
                coolOffTimeUnitInSeconds = configuration.GetValue("CoolOffTimeUnitInSeconds", 10);
                usePersistentStorage     = configuration.GetValue("UsePersistentStorage", true);

                // Note: Keep in sync with iotedge-check's edge-agent-storage-mounted-from-host check (edgelet/iotedge/src/check/checks/storage_mounted_from_host.rs)
                storagePath = GetOrCreateDirectoryPath(configuration.GetValue <string>("StorageFolder"), EdgeAgentStorageFolder);
                enableNonPersistentStorageBackup = configuration.GetValue("EnableNonPersistentStorageBackup", false);

                if (enableNonPersistentStorageBackup)
                {
                    storageBackupPath = Option.Some(GetOrCreateDirectoryPath(configuration.GetValue <string>("BackupFolder"), EdgeAgentStorageBackupFolder));
                }

                edgeDeviceHostName         = configuration.GetValue <string>(Constants.EdgeDeviceHostNameKey);
                dockerLoggingDriver        = configuration.GetValue <string>("DockerLoggingDriver");
                dockerLoggingOptions       = configuration.GetSection("DockerLoggingOptions").Get <Dictionary <string, string> >() ?? new Dictionary <string, string>();
                dockerAuthConfig           = configuration.GetSection("DockerRegistryAuth").Get <List <global::Docker.DotNet.Models.AuthConfig> >() ?? new List <global::Docker.DotNet.Models.AuthConfig>();
                configRefreshFrequencySecs = configuration.GetValue("ConfigRefreshFrequencySecs", 3600);
            }
            catch (Exception ex)
            {
                logger.LogCritical(AgentEventIds.Agent, ex, "Fatal error reading the Agent's configuration.");
                return(1);
            }

            IContainer container;

            try
            {
                var builder = new ContainerBuilder();
                builder.RegisterModule(new LoggingModule(dockerLoggingDriver, dockerLoggingOptions));
                string productInfo = versionInfo != VersionInfo.Empty ? $"{Constants.IoTEdgeAgentProductInfoIdentifier}/{versionInfo}" : Constants.IoTEdgeAgentProductInfoIdentifier;
                Option <UpstreamProtocol> upstreamProtocol = configuration.GetValue <string>(Constants.UpstreamProtocolKey).ToUpstreamProtocol();
                Option <IWebProxy>        proxy            = Proxy.Parse(configuration.GetValue <string>("https_proxy"), logger);
                bool     closeOnIdleTimeout = configuration.GetValue(Constants.CloseOnIdleTimeout, false);
                int      idleTimeoutSecs    = configuration.GetValue(Constants.IdleTimeoutSecs, 300);
                TimeSpan idleTimeout        = TimeSpan.FromSeconds(idleTimeoutSecs);
                experimentalFeatures = ExperimentalFeatures.Create(configuration.GetSection("experimentalFeatures"), logger);
                Option <ulong> storageTotalMaxWalSize = GetStorageMaxTotalWalSizeIfExists(configuration);
                string         iothubHostname;
                string         deviceId;
                string         apiVersion = "2018-06-28";

                switch (mode.ToLowerInvariant())
                {
                case Constants.DockerMode:
                    var    dockerUri = new Uri(configuration.GetValue <string>("DockerUri"));
                    string deviceConnectionString = configuration.GetValue <string>("DeviceConnectionString");
                    IotHubConnectionStringBuilder connectionStringParser = IotHubConnectionStringBuilder.Create(deviceConnectionString);
                    deviceId       = connectionStringParser.DeviceId;
                    iothubHostname = connectionStringParser.HostName;
                    builder.RegisterModule(new AgentModule(maxRestartCount, intensiveCareTime, coolOffTimeUnitInSeconds, usePersistentStorage, storagePath, enableNonPersistentStorageBackup, storageBackupPath, storageTotalMaxWalSize));
                    builder.RegisterModule(new DockerModule(deviceConnectionString, edgeDeviceHostName, dockerUri, dockerAuthConfig, upstreamProtocol, proxy, productInfo, closeOnIdleTimeout, idleTimeout));
                    break;

                case Constants.IotedgedMode:
                    string managementUri = configuration.GetValue <string>(Constants.EdgeletManagementUriVariableName);
                    string workloadUri   = configuration.GetValue <string>(Constants.EdgeletWorkloadUriVariableName);
                    iothubHostname = configuration.GetValue <string>(Constants.IotHubHostnameVariableName);
                    deviceId       = configuration.GetValue <string>(Constants.DeviceIdVariableName);
                    string moduleId           = configuration.GetValue(Constants.ModuleIdVariableName, Constants.EdgeAgentModuleIdentityName);
                    string moduleGenerationId = configuration.GetValue <string>(Constants.EdgeletModuleGenerationIdVariableName);
                    apiVersion = configuration.GetValue <string>(Constants.EdgeletApiVersionVariableName);
                    TimeSpan performanceMetricsUpdateFrequency = configuration.GetValue("PerformanceMetricsUpdateFrequency", TimeSpan.FromSeconds(30));
                    builder.RegisterModule(new AgentModule(maxRestartCount, intensiveCareTime, coolOffTimeUnitInSeconds, usePersistentStorage, storagePath, Option.Some(new Uri(workloadUri)), Option.Some(apiVersion), moduleId, Option.Some(moduleGenerationId), enableNonPersistentStorageBackup, storageBackupPath, storageTotalMaxWalSize));
                    builder.RegisterModule(new EdgeletModule(iothubHostname, edgeDeviceHostName, deviceId, new Uri(managementUri), new Uri(workloadUri), apiVersion, dockerAuthConfig, upstreamProtocol, proxy, productInfo, closeOnIdleTimeout, idleTimeout, performanceMetricsUpdateFrequency));

                    IEnumerable <X509Certificate2> trustBundle = await CertificateHelper.GetTrustBundleFromEdgelet(new Uri(workloadUri), apiVersion, Constants.WorkloadApiVersion, moduleId, moduleGenerationId);

                    CertificateHelper.InstallCertificates(trustBundle, logger);

                    break;

                case Constants.KubernetesMode:
                    managementUri      = configuration.GetValue <string>(Constants.EdgeletManagementUriVariableName);
                    workloadUri        = configuration.GetValue <string>(Constants.EdgeletWorkloadUriVariableName);
                    moduleId           = configuration.GetValue(Constants.ModuleIdVariableName, Constants.EdgeAgentModuleIdentityName);
                    moduleGenerationId = configuration.GetValue <string>(Constants.EdgeletModuleGenerationIdVariableName);
                    apiVersion         = configuration.GetValue <string>(Constants.EdgeletApiVersionVariableName);
                    iothubHostname     = configuration.GetValue <string>(Constants.IotHubHostnameVariableName);
                    deviceId           = configuration.GetValue <string>(Constants.DeviceIdVariableName);
                    string             proxyImage = configuration.GetValue <string>(K8sConstants.ProxyImageEnvKey);
                    Option <string>    proxyImagePullSecretName      = Option.Maybe(configuration.GetValue <string>(K8sConstants.ProxyImagePullSecretNameEnvKey));
                    string             proxyConfigPath               = configuration.GetValue <string>(K8sConstants.ProxyConfigPathEnvKey);
                    string             proxyConfigVolumeName         = configuration.GetValue <string>(K8sConstants.ProxyConfigVolumeEnvKey);
                    string             proxyConfigMapName            = configuration.GetValue <string>(K8sConstants.ProxyConfigMapNameEnvKey);
                    string             proxyTrustBundlePath          = configuration.GetValue <string>(K8sConstants.ProxyTrustBundlePathEnvKey);
                    string             proxyTrustBundleVolumeName    = configuration.GetValue <string>(K8sConstants.ProxyTrustBundleVolumeEnvKey);
                    string             proxyTrustBundleConfigMapName = configuration.GetValue <string>(K8sConstants.ProxyTrustBundleConfigMapEnvKey);
                    PortMapServiceType mappedServiceDefault          = GetDefaultServiceType(configuration);
                    bool          enableServiceCallTracing           = configuration.GetValue <bool>(K8sConstants.EnableK8sServiceCallTracingName);
                    string        persistentVolumeName               = configuration.GetValue <string>(K8sConstants.PersistentVolumeNameKey);
                    string        storageClassName = configuration.GetValue <string>(K8sConstants.StorageClassNameKey);
                    Option <uint> persistentVolumeClaimDefaultSizeMb = Option.Maybe(configuration.GetValue <uint?>(K8sConstants.PersistentVolumeClaimDefaultSizeInMbKey));
                    string        deviceNamespace = configuration.GetValue <string>(K8sConstants.K8sNamespaceKey);
                    var           kubernetesExperimentalFeatures = KubernetesExperimentalFeatures.Create(configuration.GetSection("experimentalFeatures"), logger);
                    var           moduleOwner = new KubernetesModuleOwner(
                        configuration.GetValue <string>(K8sConstants.EdgeK8sObjectOwnerApiVersionKey),
                        configuration.GetValue <string>(K8sConstants.EdgeK8sObjectOwnerKindKey),
                        configuration.GetValue <string>(K8sConstants.EdgeK8sObjectOwnerNameKey),
                        configuration.GetValue <string>(K8sConstants.EdgeK8sObjectOwnerUidKey));
                    bool runAsNonRoot = configuration.GetValue <bool>(K8sConstants.RunAsNonRootKey);

                    builder.RegisterModule(new AgentModule(maxRestartCount, intensiveCareTime, coolOffTimeUnitInSeconds, usePersistentStorage, storagePath, Option.Some(new Uri(workloadUri)), Option.Some(apiVersion), moduleId, Option.Some(moduleGenerationId), enableNonPersistentStorageBackup, storageBackupPath, storageTotalMaxWalSize));
                    builder.RegisterModule(new KubernetesModule(
                                               iothubHostname,
                                               deviceId,
                                               edgeDeviceHostName,
                                               proxyImage,
                                               proxyImagePullSecretName,
                                               proxyConfigPath,
                                               proxyConfigVolumeName,
                                               proxyConfigMapName,
                                               proxyTrustBundlePath,
                                               proxyTrustBundleVolumeName,
                                               proxyTrustBundleConfigMapName,
                                               apiVersion,
                                               deviceNamespace,
                                               new Uri(managementUri),
                                               new Uri(workloadUri),
                                               dockerAuthConfig,
                                               upstreamProtocol,
                                               Option.Some(productInfo),
                                               mappedServiceDefault,
                                               enableServiceCallTracing,
                                               persistentVolumeName,
                                               storageClassName,
                                               persistentVolumeClaimDefaultSizeMb,
                                               proxy,
                                               closeOnIdleTimeout,
                                               idleTimeout,
                                               kubernetesExperimentalFeatures,
                                               moduleOwner,
                                               runAsNonRoot));

                    break;

                default:
                    throw new InvalidOperationException($"Mode '{mode}' not supported.");
                }

                switch (configSourceConfig.ToLowerInvariant())
                {
                case "twin":
                    bool enableStreams      = configuration.GetValue(Constants.EnableStreams, false);
                    int  requestTimeoutSecs = configuration.GetValue(Constants.RequestTimeoutSecs, 600);
                    builder.RegisterModule(
                        new TwinConfigSourceModule(
                            iothubHostname,
                            deviceId,
                            backupConfigFilePath,
                            configuration,
                            versionInfo,
                            TimeSpan.FromSeconds(configRefreshFrequencySecs),
                            enableStreams,
                            TimeSpan.FromSeconds(requestTimeoutSecs),
                            experimentalFeatures));
                    break;

                case "local":
                    string localConfigFilePath = GetLocalConfigFilePath(configuration, logger);
                    builder.RegisterModule(new FileConfigSourceModule(localConfigFilePath, configuration, versionInfo));
                    break;

                default:
                    throw new InvalidOperationException($"ConfigSource '{configSourceConfig}' not supported.");
                }

                metricsConfig = new MetricsConfig(experimentalFeatures.EnableMetrics, MetricsListenerConfig.Create(configuration));
                builder.RegisterModule(new MetricsModule(metricsConfig, iothubHostname, deviceId));

                diagnosticConfig = new DiagnosticConfig(experimentalFeatures.EnableMetricsUpload, storagePath, configuration);
                builder.RegisterModule(new DiagnosticsModule(diagnosticConfig));

                container = builder.Build();
            }
            catch (Exception ex)
            {
                logger.LogCritical(AgentEventIds.Agent, ex, "Fatal error building application.");
                return(1);
            }

            // Initialize metrics
            if (metricsConfig.Enabled)
            {
                container.Resolve <IMetricsListener>().Start(logger);
                container.Resolve <SystemResourcesMetrics>().Start(logger);
            }

            // Initialize metric uploading
            if (diagnosticConfig.Enabled)
            {
                MetricsWorker worker = await container.Resolve <Task <MetricsWorker> >();

                worker.Start(diagnosticConfig.ScrapeInterval, diagnosticConfig.UploadInterval);
                Console.WriteLine($"Scraping frequency: {diagnosticConfig.ScrapeInterval}\nUpload Frequency: {diagnosticConfig.UploadInterval}");
            }

            // TODO move this code to Agent
            if (mode.ToLowerInvariant().Equals(Constants.KubernetesMode))
            {
                // Start environment operator
                IKubernetesEnvironmentOperator environmentOperator = container.Resolve <IKubernetesEnvironmentOperator>();
                environmentOperator.Start();

                // Start the edge deployment operator
                IEdgeDeploymentOperator edgeDeploymentOperator = container.Resolve <IEdgeDeploymentOperator>();
                edgeDeploymentOperator.Start();
            }

            (CancellationTokenSource cts, ManualResetEventSlim completed, Option <object> handler)
                = ShutdownHandler.Init(ShutdownWaitPeriod, logger);

            // Register request handlers
            await RegisterRequestHandlers(container);

            // Initialize stream request listener
            IStreamRequestListener streamRequestListener = await container.Resolve <Task <IStreamRequestListener> >();

            streamRequestListener.InitPump();

            int returnCode;

            using (IConfigSource unused = await container.Resolve <Task <IConfigSource> >())
            {
                Option <Agent> agentOption = Option.None <Agent>();

                try
                {
                    Agent agent = await container.Resolve <Task <Agent> >();

                    agentOption = Option.Some(agent);
                    while (!cts.Token.IsCancellationRequested)
                    {
                        try
                        {
                            await agent.ReconcileAsync(cts.Token).TimeoutAfter(ReconcileTimeout);
                        }
                        catch (Exception ex) when(!ex.IsFatal())
                        {
                            logger.LogWarning(AgentEventIds.Agent, ex, "Agent reconcile concluded with errors.");
                        }

                        await Task.Delay(TimeSpan.FromSeconds(5), cts.Token);
                    }

                    logger.LogInformation("Closing module management agent.");

                    returnCode = 0;
                }
                catch (OperationCanceledException)
                {
                    logger.LogInformation("Main thread terminated");
                    returnCode = 0;
                }
                catch (Exception ex)
                {
                    logger.LogCritical(AgentEventIds.Agent, ex, "Fatal error starting Agent.");
                    returnCode = 1;
                }

                // Attempt to report shutdown of Agent
                await Cleanup(agentOption, logger);
                await CloseDbStoreProviderAsync(container);

                if (metricsConfig.Enabled && returnCode == 0)
                {
                    container.Resolve <IAvailabilityMetric>().IndicateCleanShutdown();
                }

                completed.Set();
            }

            handler.ForEach(h => GC.KeepAlive(h));
            return(returnCode);
        }
예제 #7
0
 public async Task <Result> UpdateMetricsForFlow(string flowName, MetricsConfig metrics)
 {
     return(await this.Storage.UpdatePartialByName(metrics.ToString(), FlowConfig.JsonFieldName_Metrics, flowName, DataCollectionName));
 }
예제 #8
0
        public void Register(ContainerBuilder builder)
        {
            const int ConnectionPoolSize = 10;

            string edgeHubConnectionString = $"{this.configuration[EdgeHubConstants.ConfigKey.IotHubConnectionString]};ModuleId=$edgeHub";
            IotHubConnectionStringBuilder iotHubConnectionStringBuilder = IotHubConnectionStringBuilder.Create(edgeHubConnectionString);
            var topics = new MessageAddressConversionConfiguration(this.inboundTemplates, this.outboundTemplates);

            builder.RegisterModule(new LoggingModule());

            var mqttSettingsConfiguration = new Mock <IConfiguration>();

            mqttSettingsConfiguration.Setup(c => c.GetSection(It.IsAny <string>())).Returns(Mock.Of <IConfigurationSection>(s => s.Value == null));

            var experimentalFeatures = new ExperimentalFeatures(true, false, false);

            builder.RegisterBuildCallback(
                c =>
            {
                // set up loggers for dotnetty
                var loggerFactory = c.Resolve <ILoggerFactory>();
                InternalLoggerFactory.DefaultFactory = loggerFactory;

                var eventListener = new LoggerEventListener(loggerFactory.CreateLogger("ProtocolGateway"));
                eventListener.EnableEvents(CommonEventSource.Log, EventLevel.Informational);
            });

            var versionInfo   = new VersionInfo("v1", "b1", "c1");
            var metricsConfig = new MetricsConfig(mqttSettingsConfiguration.Object);
            var backupFolder  = Option.None <string>();

            string      storageFolder = string.Empty;
            StoreLimits storeLimits   = null;

            if (!int.TryParse(this.configuration["TimeToLiveSecs"], out int timeToLiveSecs))
            {
                timeToLiveSecs = -1;
            }

            if (long.TryParse(this.configuration["MaxStorageBytes"], out long maxStorageBytes))
            {
                storeLimits = new StoreLimits(maxStorageBytes);
            }

            var storeAndForwardConfiguration = new StoreAndForwardConfiguration(timeToLiveSecs, storeLimits);

            if (bool.TryParse(this.configuration["UsePersistentStorage"], out bool usePersistentStorage) && usePersistentStorage)
            {
                storageFolder = GetOrCreateDirectoryPath(this.configuration["StorageFolder"], EdgeHubConstants.EdgeHubStorageFolder);
            }

            if (bool.TryParse(this.configuration["EnableNonPersistentStorageBackup"], out bool enableNonPersistentStorageBackup))
            {
                backupFolder = Option.Some(this.configuration["BackupFolder"]);
            }

            var    testRoutes   = this.routes;
            string customRoutes = this.configuration["Routes"];

            if (!string.IsNullOrWhiteSpace(customRoutes))
            {
                testRoutes = JsonConvert.DeserializeObject <IDictionary <string, string> >(customRoutes);
            }

            builder.RegisterModule(
                new CommonModule(
                    string.Empty,
                    iotHubConnectionStringBuilder.HostName,
                    iotHubConnectionStringBuilder.DeviceId,
                    iotHubConnectionStringBuilder.ModuleId,
                    string.Empty,
                    Option.None <string>(),
                    AuthenticationMode.CloudAndScope,
                    Option.Some(edgeHubConnectionString),
                    false,
                    usePersistentStorage,
                    storageFolder,
                    Option.None <string>(),
                    Option.None <string>(),
                    TimeSpan.FromHours(1),
                    false,
                    this.trustBundle,
                    string.Empty,
                    metricsConfig,
                    enableNonPersistentStorageBackup,
                    backupFolder,
                    Option.None <ulong>(),
                    Option.None <StorageLogLevel>()));

            builder.RegisterModule(
                new RoutingModule(
                    iotHubConnectionStringBuilder.HostName,
                    iotHubConnectionStringBuilder.DeviceId,
                    iotHubConnectionStringBuilder.ModuleId,
                    Option.Some(edgeHubConnectionString),
                    testRoutes,
                    true,
                    storeAndForwardConfiguration,
                    ConnectionPoolSize,
                    false,
                    versionInfo,
                    Option.Some(UpstreamProtocol.Amqp),
                    TimeSpan.FromSeconds(5),
                    101,
                    TimeSpan.FromSeconds(30),
                    TimeSpan.FromSeconds(3600),
                    true,
                    TimeSpan.FromSeconds(20),
                    false,
                    Option.None <TimeSpan>(),
                    Option.None <TimeSpan>(),
                    false,
                    10,
                    10,
                    false,
                    TimeSpan.FromHours(1),
                    experimentalFeatures));

            builder.RegisterModule(new HttpModule());
            builder.RegisterModule(new MqttModule(mqttSettingsConfiguration.Object, topics, this.serverCertificate, false, false, false, this.sslProtocols));
            builder.RegisterModule(new AmqpModule("amqps", 5671, this.serverCertificate, iotHubConnectionStringBuilder.HostName, true, this.sslProtocols));
        }
예제 #9
0
 public MetricsModule(MetricsConfig metricsConfig, string iothubHostname, string deviceId)
 {
     this.metricsConfig  = Preconditions.CheckNotNull(metricsConfig, nameof(metricsConfig));
     this.iothubHostname = Preconditions.CheckNonWhiteSpace(iothubHostname, nameof(iothubHostname));
     this.deviceId       = Preconditions.CheckNonWhiteSpace(deviceId, nameof(deviceId));
 }
예제 #10
0
        public void Register(ContainerBuilder builder)
        {
            const int ConnectionPoolSize = 10;

            string edgeHubConnectionString = $"{this.configuration[Constants.ConfigKey.IotHubConnectionString]};ModuleId=$edgeHub";
            IotHubConnectionStringBuilder iotHubConnectionStringBuilder = IotHubConnectionStringBuilder.Create(edgeHubConnectionString);
            var topics = new MessageAddressConversionConfiguration(this.inboundTemplates, this.outboundTemplates);

            builder.RegisterModule(new LoggingModule());

            var mqttSettingsConfiguration = new Mock <IConfiguration>();

            mqttSettingsConfiguration.Setup(c => c.GetSection(It.IsAny <string>())).Returns(Mock.Of <IConfigurationSection>(s => s.Value == null));

            var experimentalFeatures = new ExperimentalFeatures(true, false, false);

            builder.RegisterBuildCallback(
                c =>
            {
                // set up loggers for dotnetty
                var loggerFactory = c.Resolve <ILoggerFactory>();
                InternalLoggerFactory.DefaultFactory = loggerFactory;

                var eventListener = new LoggerEventListener(loggerFactory.CreateLogger("ProtocolGateway"));
                eventListener.EnableEvents(CommonEventSource.Log, EventLevel.Informational);
            });

            var versionInfo = new VersionInfo("v1", "b1", "c1");
            var storeAndForwardConfiguration = new StoreAndForwardConfiguration(-1);
            var metricsConfig = new MetricsConfig(false, new MetricsListenerConfig());

            builder.RegisterModule(
                new CommonModule(
                    string.Empty,
                    iotHubConnectionStringBuilder.HostName,
                    iotHubConnectionStringBuilder.DeviceId,
                    iotHubConnectionStringBuilder.ModuleId,
                    string.Empty,
                    Option.None <string>(),
                    AuthenticationMode.CloudAndScope,
                    Option.Some(edgeHubConnectionString),
                    false,
                    false,
                    string.Empty,
                    Option.None <string>(),
                    Option.None <string>(),
                    TimeSpan.FromHours(1),
                    false,
                    this.trustBundle,
                    string.Empty,
                    metricsConfig));

            builder.RegisterModule(
                new RoutingModule(
                    iotHubConnectionStringBuilder.HostName,
                    iotHubConnectionStringBuilder.DeviceId,
                    iotHubConnectionStringBuilder.ModuleId,
                    Option.Some(edgeHubConnectionString),
                    this.routes,
                    true,
                    storeAndForwardConfiguration,
                    ConnectionPoolSize,
                    false,
                    versionInfo,
                    Option.Some(UpstreamProtocol.Amqp),
                    TimeSpan.FromSeconds(5),
                    101,
                    TimeSpan.FromSeconds(3600),
                    true,
                    TimeSpan.FromSeconds(20),
                    Option.None <TimeSpan>(),
                    Option.None <TimeSpan>(),
                    false,
                    10,
                    10,
                    false,
                    TimeSpan.FromHours(1),
                    experimentalFeatures));

            builder.RegisterModule(new HttpModule());
            builder.RegisterModule(new MqttModule(mqttSettingsConfiguration.Object, topics, this.serverCertificate, false, false, false));
            builder.RegisterModule(new AmqpModule("amqps", 5671, this.serverCertificate, iotHubConnectionStringBuilder.HostName, true));
        }
        public static IMetricsBuilder AddReporter(this IMetricsBuilder builder,
                                                  Action <MetricsReports> reportsAction, MetricsConfig config)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            config.WithReporting(reportsAction);

            return(builder);
        }