public async Task <IHttpActionResult> PostBasic(Guid telemetryKey, BasicTelemetryItem request) { if (request == null) { request = new BasicTelemetryItem(); } TelemetryRootObject program = await this.work.GetMonitoredProgram(telemetryKey).ConfigureAwait(false); if (program == null) { throw new BadRequestException($"Program with telemetry key [{telemetryKey}] does not exist"); } try { TelemetryItem item = TelemetryMapper.Map(telemetryKey, request); string ip = this.Request.GetClientIp(); await this.InsertDataInternal(new[] { item }, program, ip).ConfigureAwait(false); return(await Task.FromResult(this.StatusCode(HttpStatusCode.OK)).ConfigureAwait(false)); } catch (Exception ex) { return(await Task.FromResult(this.InternalServerError(new InvalidOperationException($"Error while inserting entry: {ex}"))).ConfigureAwait(false)); } }
public async Task <IHttpActionResult> Post(Guid telemetryKey) { try { TelemetryRootObject program = await this.work.GetMonitoredProgram(telemetryKey).ConfigureAwait(false); if (program == null) { throw new BadRequestException($"Program with telemetry key [{telemetryKey}] does not exist"); } IEnumerable <AppInsightsTelemetry> appInsightsTelemetries = AppInsightsDeserializer.Deserialize(await this.Request.Content.ReadAsByteArrayAsync().ConfigureAwait(false), true); List <TelemetryItem> telemetryItems = TelemetryMapper.Map(appInsightsTelemetries).ToList(); string ip = "0.0.0.0"; await this.InsertDataInternal(telemetryItems, program, ip).ConfigureAwait(false); return(await Task.FromResult(this.StatusCode(HttpStatusCode.Accepted)).ConfigureAwait(false)); } catch (Exception e) { this.telemetryClient.TrackException(e, new Dictionary <string, string>() { { $"Method", Routes.Post }, { $"TelemetryKey", telemetryKey.ToString() } }); throw; } }
private static List <TelemetryItem> DoTheMapping(List <ITelemetry> sentTelemetry) { byte[] serialized = JsonSerializer.Serialize(sentTelemetry, false); var items = AppInsightsDeserializer.Deserialize(serialized, false).ToList(); var mapped = TelemetryMapper.Map(items).ToList(); return(mapped); }
public void TestException() { var basicItem = new BasicTelemetryItem() { TelemetryItemType = TelemetryItemTypes.Exception, Timestamp = new DateTimeOffset(1988, 02, 28, 0, 0, 0, new TimeSpan(0)), ErrorMessage = "An error message" }; var item = TelemetryMapper.Map(this.testTelemetryKey, basicItem); Assert.AreEqual(TelemetryItemTypes.Exception, item.TelemetryItemType); Assert.AreEqual("An error message", item.Exceptions.Single().Message); }
public void TestDefault() { var basicItem = new BasicTelemetryItem(); var item = TelemetryMapper.Map(this.testTelemetryKey, basicItem); Assert.IsNotNull(item.EntryKey); Assert.IsNotNull(item.UserIdentifier); Assert.AreEqual("0.0.0.0", item.VersionData.FileVersion); Assert.AreEqual("0.0.0.0", item.VersionData.AssemblyVersion); Assert.AreNotEqual(Guid.Empty, item.Id); Assert.AreEqual(this.testTelemetryKey, item.TelemetryKey); Assert.AreEqual(TelemetryItemTypes.Event, item.TelemetryItemType); Assert.AreNotEqual(default(DateTimeOffset), item.Timestamp); Assert.IsTrue(!string.IsNullOrEmpty(basicItem.EventName)); Assert.AreEqual(basicItem.EventName, item.EntryKey); }
public void CheckListOfItems() { List <ITelemetry> sentTelemetry = new List <ITelemetry>(); var teli = Helpers.GetCrackedTelimena(sentTelemetry, this.testTelemetryKey, "ASD", true); teli.Track.Event("TestEvent", new Dictionary <string, string>() { { "AKey", $"AValue" } }); teli.Track.View("TestView"); teli.Track.Log(LogLevel.Warn, "A log message"); teli.Track.Exception(new Exception("An error that happened")); teli.Track.Exception(new Exception("An error that happened with note"), "A note for error"); teli.Track.SendAllDataNow(); byte[] serialized = JsonSerializer.Serialize(sentTelemetry, true); var items = AppInsightsDeserializer.Deserialize(serialized, true).ToList(); var mapped = TelemetryMapper.Map(items).ToList(); for (int index = 0; index < sentTelemetry.Count; index++) { ITelemetry appInsightsItem = sentTelemetry[index]; TelemetryItem telimenaItem = mapped[index]; Assert.AreEqual(appInsightsItem.Timestamp, telimenaItem.Timestamp); Assert.AreEqual(appInsightsItem.Sequence, telimenaItem.Sequence); Assert.AreEqual(appInsightsItem.Context.User.Id, telimenaItem.UserIdentifier); if (telimenaItem.TelemetryItemType != TelemetryItemTypes.LogMessage && telimenaItem.TelemetryItemType != TelemetryItemTypes.Exception) { Assert.AreEqual(appInsightsItem.GetPropertyValue <string>("Name"), telimenaItem.EntryKey); } foreach (KeyValuePair <string, string> keyValuePair in appInsightsItem.GetPropertyValue <ConcurrentDictionary <string, string> >("Properties")) { var props = typeof(TelimenaContextPropertyKeys).GetProperties().Select(x => x.Name); if (!props.Contains(keyValuePair.Key)) { Assert.AreEqual(keyValuePair.Value, telimenaItem.Properties[keyValuePair.Key]); } } } }
public async Task <IHttpActionResult> Post(Guid telemetryKey) { TelemetryRootObject program = await this.work.GetMonitoredProgram(telemetryKey).ConfigureAwait(false); if (program == null) { throw new BadRequestException($"Program with telemetry key [{telemetryKey}] does not exist"); } IEnumerable <AppInsightsTelemetry> appInsightsTelemetries = AppInsightsDeserializer.Deserialize(await this.Request.Content.ReadAsByteArrayAsync().ConfigureAwait(false), true); IEnumerable <TelemetryItem> telemetryItems = TelemetryMapper.Map(appInsightsTelemetries); string ip = this.Request.GetClientIp(); await this.InsertDataInternal(telemetryItems, program, ip).ConfigureAwait(false); return(await Task.FromResult(this.StatusCode(HttpStatusCode.Accepted)).ConfigureAwait(false)); }
public async Task <IHttpActionResult> PostWithVariousKeys() { try { IEnumerable <AppInsightsTelemetry> appInsightsTelemetries = AppInsightsDeserializer.Deserialize(await this.Request.Content.ReadAsByteArrayAsync().ConfigureAwait(false), true); List <TelemetryItem> telemetryItems = TelemetryMapper.Map(appInsightsTelemetries).ToList(); // string ip = this.Request.GetClientIp(); string ip = "0.0.0.0"; //items might come with various telemetry key in one request (when there are multiple instances of telemetry client) IEnumerable <IGrouping <Guid, TelemetryItem> > groups = telemetryItems.GroupBy(telemetry => telemetry.TelemetryKey); //todo - parallelism? foreach (IGrouping <Guid, TelemetryItem> grouping in groups) { TelemetryRootObject program = await this.work.GetMonitoredProgram(grouping.Key).ConfigureAwait(false); if (program != null) { await this.InsertDataInternal(grouping.ToList(), program, ip).ConfigureAwait(false); } else { this.telemetryClient.TrackException(new InvalidOperationException($"Program with telemetry key [{grouping.Key}] does not exist"), new Dictionary <string, string>() { { $"Method", Routes.PostWithVariousKeys }, { $"TelemetryKey", grouping.Key.ToString() }, }); } } return(await Task.FromResult(this.StatusCode(HttpStatusCode.Accepted)).ConfigureAwait(false)); } catch (Exception e) { this.telemetryClient.TrackException(e, new Dictionary <string, string>() { { $"Method", Routes.PostWithVariousKeys }, }); throw; } }
public async Task <IHttpActionResult> PostWithVariousKeys() { IEnumerable <AppInsightsTelemetry> appInsightsTelemetries = AppInsightsDeserializer.Deserialize(await this.Request.Content.ReadAsByteArrayAsync().ConfigureAwait(false), true); IEnumerable <TelemetryItem> telemetryItems = TelemetryMapper.Map(appInsightsTelemetries); string ip = this.Request.GetClientIp(); //items might come with various telemetry key in one request (when there are multiple instances of telemetry client) IEnumerable <IGrouping <Guid, TelemetryItem> > groups = telemetryItems.GroupBy(telemetry => telemetry.TelemetryKey); //todo - parallelism? foreach (IGrouping <Guid, TelemetryItem> grouping in groups) { TelemetryRootObject program = await this.work.GetMonitoredProgram(grouping.Key).ConfigureAwait(false); await this.InsertDataInternal(grouping, program, ip).ConfigureAwait(false); } return(await Task.FromResult(this.StatusCode(HttpStatusCode.Accepted)).ConfigureAwait(false)); }
public void TestLogMessage() { var basicItem = new BasicTelemetryItem() { LogMessage = "Boo", TelemetryItemType = TelemetryItemTypes.LogMessage, EventName = "LogMsg", Timestamp = new DateTimeOffset(1988, 02, 28, 0, 0, 0, new TimeSpan(0)), ProgramVersion = "3.4", UserIdentifier = "JimBeam" }; var item = TelemetryMapper.Map(this.testTelemetryKey, basicItem); Assert.AreEqual(TelemetryItemTypes.LogMessage, item.TelemetryItemType); Assert.AreEqual("Boo", item.LogMessage); Assert.AreEqual(new DateTimeOffset(1988, 02, 28, 0, 0, 0, new TimeSpan(0)), item.Timestamp); Assert.AreEqual("JimBeam", item.UserIdentifier); Assert.AreEqual("3.4", item.VersionData.FileVersion); Assert.AreEqual("3.4", item.VersionData.AssemblyVersion); Assert.IsTrue(!string.IsNullOrEmpty(basicItem.EventName)); Assert.AreEqual(basicItem.EventName, item.EntryKey); }