private async Task PostMetric2Async(YEngine engine, YMetricPayload2 payload, string instrumentationKey)
        {
            using TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();

            configuration.InstrumentationKey = instrumentationKey;

            var telemetryClient = new TelemetryClient(configuration);

            var telemetry = new MetricTelemetry(payload.Namespace, payload.Name, payload.Count, payload.Sum, payload.Min, payload.Max, 0);

            foreach (var d in payload.Dimensions)
            {
                telemetry.Properties.Add(d.Name, d.Value);
            }

            telemetryClient.TrackMetric(telemetry);

            telemetryClient.Flush();
        }
        public async Task <ActionResult <bool> > AddMetricFromDaemonAsync(Guid engineId, [FromBody] YMetricPayload2 payload)
        {
            var userObjectId = this.User.GetObjectId();

            if (string.IsNullOrEmpty(userObjectId))
            {
                return(new UnauthorizedObjectResult("Daemon id unknown"));
            }

            if (userObjectId != this.options.ClientObjectId)
            {
                return(new UnauthorizedObjectResult("This web api should be called only from a daemon application using the correct Client Id / Client Secret"));
            }

            var engine = await this.engineProvider.GetEngineAsync(engineId).ConfigureAwait(false);

            if (engine == null)
            {
                throw new Exception("Engine does not exists");
            }

            // Get connection string
            var instrumentationKeySecret = await keyVaultsController.GetKeyVaultSecret(engine.Id, engine.AppInsightsName);

            string instrumentationKey = instrumentationKeySecret?.Value;

            if (string.IsNullOrEmpty(instrumentationKey))
            {
                throw new Exception("Can't find the App Insights Instrumentation Key in the Key Vault");
            }

            await PostMetric2Async(engine, payload, instrumentationKey);

            return(true);
        }