|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.ApplicationInsights; |
| 10 | +using Microsoft.ApplicationInsights.Channel; |
| 11 | +using Microsoft.ApplicationInsights.DataContracts; |
| 12 | +using Microsoft.ApplicationInsights.Extensibility; |
| 13 | +using Microsoft.Azure.WebJobs.Script.Config; |
| 14 | +using Microsoft.Azure.WebJobs.Script.WebHost; |
| 15 | +using Microsoft.Azure.WebJobs.Script.Workers.Rpc; |
| 16 | +using Microsoft.Extensions.Hosting; |
| 17 | +using Microsoft.Extensions.Options; |
| 18 | +using Xunit; |
| 19 | + |
| 20 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Configuration |
| 21 | +{ |
| 22 | + public class ScriptTelemetryProcessorTests |
| 23 | + { |
| 24 | + [Fact] |
| 25 | + public async Task Test_TelemetryProcessor_AppInsights() |
| 26 | + { |
| 27 | + var rpcEx = new RpcException("failed", "user message", "user stack", "user exception type"); |
| 28 | + rpcEx.IsUserException = true; |
| 29 | + |
| 30 | + TelemetryConfiguration config = new TelemetryConfiguration("instrumentation key"); |
| 31 | + ExceptionTelemetry oldEt = new ExceptionTelemetry(rpcEx); |
| 32 | + config.TelemetryProcessorChainBuilder.Use(next => new MyCustomTelemetryProcessor(next)); |
| 33 | + TelemetryClient client = new TelemetryClient(config); |
| 34 | + client.TrackException(oldEt); |
| 35 | + await client.FlushAsync(CancellationToken.None); |
| 36 | + } |
| 37 | + |
| 38 | + public class MyCustomTelemetryProcessor : ITelemetryProcessor |
| 39 | + { |
| 40 | + public MyCustomTelemetryProcessor(ITelemetryProcessor item) |
| 41 | + { |
| 42 | + this.Next = item; |
| 43 | + } |
| 44 | + |
| 45 | + private ITelemetryProcessor Next { get; set; } |
| 46 | + |
| 47 | + public void Process(ITelemetry item) |
| 48 | + { |
| 49 | + if (item is ExceptionTelemetry exceptionTelemetry |
| 50 | + && exceptionTelemetry.Exception is RpcException rpcException |
| 51 | + && rpcException.IsUserException) |
| 52 | + { |
| 53 | + item = ToUserException(rpcException, item); |
| 54 | + } |
| 55 | + this.Next.Process(item); |
| 56 | + } |
| 57 | + |
| 58 | + private ITelemetry ToUserException(RpcException rpcException, ITelemetry originalItem) |
| 59 | + { |
| 60 | + rpcException.RemoteTypeName = "test user exception type"; |
| 61 | + |
| 62 | + string typeName = string.IsNullOrEmpty(rpcException.RemoteTypeName) ? rpcException.GetType().ToString() : rpcException.RemoteTypeName; |
| 63 | + |
| 64 | + var userExceptionDetails = new ExceptionDetailsInfo(1, -1, typeName, rpcException.RemoteMessage, true, rpcException.RemoteStackTrace, new StackFrame[] { }); |
| 65 | + |
| 66 | + ExceptionTelemetry newET = new ExceptionTelemetry(new[] { userExceptionDetails }, |
| 67 | + SeverityLevel.Error, "ProblemId", |
| 68 | + new Dictionary<string, string>() { }, |
| 69 | + new Dictionary<string, double>() { }); |
| 70 | + |
| 71 | + newET.Context.InstrumentationKey = originalItem.Context.InstrumentationKey; |
| 72 | + newET.Timestamp = originalItem.Timestamp; |
| 73 | + |
| 74 | + return newET; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments