public void TrackRequestSendsGivenRequestTelemetryToTelemetryChannel()
        {
            var             sentTelemetry = new List <ITelemetry>();
            TelemetryClient client        = this.InitializeTelemetryClient(sentTelemetry);

            var clientRequest = new RequestTelemetry();

            client.TrackRequest(clientRequest);

            var channelRequest = (RequestTelemetry)sentTelemetry.Single();

            Assert.Same(clientRequest, channelRequest);
        }
        public void TrackRequestSendsRequestTelemetryWithGivenNameTimestampDurationAndSuccessToTelemetryChannel()
        {
            var             sentTelemetry = new List <ITelemetry>();
            TelemetryClient client        = this.InitializeTelemetryClient(sentTelemetry);

            var timestamp = DateTimeOffset.Now;

            client.TrackRequest("name", timestamp, TimeSpan.FromSeconds(42), "500", false);

            var request = (RequestTelemetry)sentTelemetry.Single();

            Assert.Equal("name", request.Name);
            Assert.Equal(timestamp, request.Timestamp);
            Assert.Equal("500", request.ResponseCode);
            Assert.Equal(TimeSpan.FromSeconds(42), request.Duration);
            Assert.Equal(false, request.Success);
        }
        public void AllTelemetryIsSentWithDefaultSamplingRate()
        {
            var sentTelemetry = new List<ITelemetry>();
            var channel = new StubTelemetryChannel { OnSend = t => sentTelemetry.Add(t) };
            var configuration = new TelemetryConfiguration { InstrumentationKey = "Test key", TelemetryChannel = channel };

            var client = new TelemetryClient(configuration);

            const int ItemsToGenerate = 100;

            for (int i = 0; i < ItemsToGenerate; i++)
            {
                client.TrackRequest(new RequestTelemetry());
            }

            Assert.Equal(ItemsToGenerate, sentTelemetry.Count);
        }
        public void AllTelemetryIsSentWithDefaultSamplingRate()
        {
            var sentTelemetry = new List <ITelemetry>();
            var channel       = new StubTelemetryChannel {
                OnSend = t => sentTelemetry.Add(t)
            };
            var configuration = new TelemetryConfiguration("Test key", channel);
            var client        = new TelemetryClient(configuration);

            const int ItemsToGenerate = 100;

            for (int i = 0; i < ItemsToGenerate; i++)
            {
                client.TrackRequest(new RequestTelemetry());
            }

            Assert.Equal(ItemsToGenerate, sentTelemetry.Count);
        }
Пример #5
0
        static void Main(string[] args)
        {
            var config = TelemetryConfiguration.CreateDefault();
            config.TelemetryInitializers.Add(new EventSourceForAI.EventSourceTelemetryInitializer());
            config.InstrumentationKey = "Foo";

            TelemetryClient client = new TelemetryClient(config);

            var request = new RequestTelemetry("TestRequest", DateTimeOffset.Now, TimeSpan.FromMilliseconds(10), "200", true);
            request.Context.Properties.Add("property1", "value1");
            request.Context.User.Id = "userId";

            request.Context.Operation.Id = Guid.NewGuid().ToString();

            client.TrackRequest(request);

            Thread.Sleep(100);
        }
Пример #6
0
        public async Task Invoke(IOwinContext context)
        {
            var stopwatch = Stopwatch.StartNew();
            bool success = false;

            try
            {
                switch (context.Request.Path.Value.ToLowerInvariant())
                {
                    case "/":
                        JObject response = new JObject();
                        response.Add("name", ServiceName);
                        JObject resources = new JObject();
                        response.Add("resources", resources);
                        resources.Add("range", MakeUri(context, "/search/range"));
                        resources.Add("fields", MakeUri(context, "/search/fields"));
                        resources.Add("console", MakeUri(context, "/console"));
                        resources.Add("diagnostics", MakeUri(context, "/search/diag"));
                        resources.Add("segments", MakeUri(context, "/search/segments"));
                        resources.Add("query", MakeUri(context, "/search/query"));
                        context.Response.StatusCode = (int) HttpStatusCode.OK;
                        context.Response.ContentType = "application/json";
                        await context.Response.WriteAsync(response.ToString());
                        break;
                    case "/search/query":
                        await QueryMiddleware.Execute(context, _searcherManager);
                        break;
                    case "/search/range":
                        await RangeMiddleware.Execute(context, _searcherManager);
                        break;
                    case "/search/diag":
                        await DiagMiddleware.Execute(context, _searcherManager);
                        break;
                    case "/search/segments":
                        await SegmentsMiddleware.Execute(context, _searcherManager);
                        break;
                    case "/search/fields":
                        await FieldsMiddleware.Execute(context, _searcherManager);
                        break;
                    default:
                        await context.Response.WriteAsync("unrecognized");
                        context.Response.StatusCode = (int) HttpStatusCode.NotFound;
                        break;
                }

                success = true;
            }
            catch
            {
                success = false;
            }
            finally
            {
                stopwatch.Stop();
                var telemetryClient = new TelemetryClient();
                var requestTelemetry = new RequestTelemetry(context.Request.Path.Value, DateTimeOffset.UtcNow, stopwatch.Elapsed, context.Response.StatusCode.ToString(CultureInfo.InvariantCulture), success: success);
                telemetryClient.TrackRequest(requestTelemetry);
            }
        }
Пример #7
-1
        public async Task<HttpResponseMessage> Post(string eventId)
        {
            var userToken = Request.Headers.SingleOrDefault(x => x.Key == "Authorization").Value.First();
            if (string.IsNullOrWhiteSpace(userToken))
                return Request.CreateResponse(HttpStatusCode.Forbidden);
            if (userToken != ConfigurationManager.AppSettings["UserToken"])
                return Request.CreateResponse(HttpStatusCode.Unauthorized);

            var telemetry = new TelemetryClient();
            var stopwatch = Stopwatch.StartNew();

            try
            {
                telemetry.Context.Operation.Name = "Reward";
                telemetry.Context.Operation.Id = eventId;

                // support simply float and complex JSON outcomes
                var rewardStr = await Request.Content.ReadAsStringAsync();
                var rewardObj = JToken.Parse(rewardStr);

                // parse input
                var guid = Guid.ParseExact(eventId, "N");

                var url = ConfigurationManager.AppSettings["DecisionServiceSettingsUrl"];
                var eventUploader = DecisionServiceStaticClient.AddOrGetExisting("uploader" + url,
                    _ =>
                    {
                        telemetry.TrackEvent("EventUploader creation");
                                               
                        var metaData = ApplicationMetadataUtil.DownloadMetadata<ApplicationClientMetadata>(url);
                        return new EventUploaderASA(
                            metaData.EventHubObservationConnectionString,
                            new BatchingConfiguration
                            {
                            // TODO: these are not production ready configurations. do we need to move those to C&C as well?
                            MaxBufferSizeInBytes = 1,
                                MaxDuration = TimeSpan.FromSeconds(1),
                                MaxEventCount = 1,
                                MaxUploadQueueCapacity = 1,
                                UploadRetryPolicy = BatchUploadRetryPolicy.ExponentialRetry
                            });
                    });

                eventUploader.Upload(new Observation
                {
                    Key = guid.ToString("N", CultureInfo.InvariantCulture),
                    Value = rewardObj
                });

                stopwatch.Stop();
                telemetry.TrackRequest("ReportReward", DateTime.Now, stopwatch.Elapsed, "200", true);

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch(Exception e)
            {
                telemetry.TrackException(e);
                return Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message);
            }
        }