コード例 #1
0
        private void ProcessEndCommand()
        {
            Entity entity = null;

            try
            {
                entity = _recorder.GetEntity();
            }
            catch (EntityNotAvailableException e)
            {
                _recorder.TraceContext.HandleEntityMissing(_recorder, e, "Cannot get entity while processing end of Entity Framework command.");
                return;
            }

            _recorder.EndSubsegment();
        }
コード例 #2
0
        /// <summary>
        /// Processes End request by ending subsegment.
        /// </summary>
        private void ProcessEndRequest(IExecutionContext executionContext)
        {
            var subsegment      = AWSXRayRecorder.Instance.GetEntity();
            var responseContext = executionContext.ResponseContext;
            var requestContext  = executionContext.RequestContext;

            if (responseContext == null)
            {
                _logger.DebugFormat("Failed to handle AfterResponseEvent, because response is null.");
                return;
            }

            var client = executionContext.RequestContext.ClientConfig;

            if (client == null)
            {
                _logger.DebugFormat("Failed to handle AfterResponseEvent, because client from the Response Context is null");
                return;
            }

            var serviceName = RemoveAmazonPrefixFromServiceName(requestContext.Request.ServiceName);
            var operation   = RemoveSuffix(requestContext.OriginalRequest.GetType().Name, "Request");

            subsegment.Aws["region"]    = client.RegionEndpoint?.SystemName;
            subsegment.Aws["operation"] = operation;
            if (responseContext.Response == null)
            {
                if (requestContext.Request.Headers.TryGetValue("x-amzn-RequestId", out string requestId))
                {
                    subsegment.Aws["request_id"] = requestId;
                }
                // s3 doesn't follow request header id convention
                else
                {
                    if (requestContext.Request.Headers.TryGetValue(S3RequestIdHeaderKey, out requestId))
                    {
                        subsegment.Aws["request_id"] = requestId;
                    }

                    if (requestContext.Request.Headers.TryGetValue(S3ExtendedRequestIdHeaderKey, out requestId))
                    {
                        subsegment.Aws[ExtendedRquestIdSegmentKey] = requestId;
                    }
                }
            }
            else
            {
                subsegment.Aws["request_id"] = responseContext.Response.ResponseMetadata.RequestId;
                AddResponseSpecificInformation(serviceName, operation, responseContext.Response, subsegment.Aws);
            }

            if (responseContext.HttpResponse != null)
            {
                AddHttpInformation(responseContext.HttpResponse);
            }

            AddRequestSpecificInformation(serviceName, operation, requestContext.OriginalRequest, subsegment.Aws);
            _recorder.EndSubsegment();
        }
コード例 #3
0
        public void TestSubsegment()
        {
            _recorder.BeginSubsegment("subsegment1");
            Subsegment    subsegment1   = (Subsegment)AWSXRayRecorder.Instance.TraceContext.GetEntity();
            FacadeSegment facadeSegment = (FacadeSegment)subsegment1.RootSegment;

            _recorder.EndSubsegment();
            Assert.AreEqual(typeof(LambdaContextContainer), AWSXRayRecorder.Instance.TraceContext.GetType());
            Assert.AreEqual(facadeSegment.GetType(), typeof(FacadeSegment));
            Assert.IsFalse(facadeSegment.Subsegments.Contains(subsegment1));         // only subsegment is streamed
            Assert.IsFalse(AWSXRayRecorder.Instance.TraceContext.IsEntityPresent()); // facade segment is cleared from AWSXRayRecorder.Instance.TraceContext
        }
コード例 #4
0
        private void AfterResponseEventHandler(object sender, ResponseEventArgs e)
        {
            if (AppSettings.IsXRayTracingDisabled)
            {
                _logger.DebugFormat("X-Ray tracing is disabled, do not handle AWSSDK response.");
                return;
            }

            var args = e as WebServiceResponseEventArgs;

            if (args == null)
            {
                _logger.DebugFormat("Failed to handle AfterResponseEvent, because e can't be converted to WebServiceResponseEventArgs.");
                return;
            }

            var client = sender as AmazonServiceClient;

            if (client == null)
            {
                _logger.DebugFormat("Failed to handle AfterResponseEvent, because sender can't be converted to AmazonServiceClient.");
                return;
            }

            var serviceName = RemoveAmazonPrefixFromServiceName(args.ServiceName);
            var operation   = RemoveSuffix(args.Request.GetType().Name, "Request");

            if (AWSXRaySDKUtils.IsBlacklistedOperation(serviceName, operation))
            {
                return;
            }

            var subsegment = AWSXRayRecorder.Instance.GetEntity();

            subsegment.Aws["region"]     = client.Config.RegionEndpoint?.SystemName;
            subsegment.Aws["operation"]  = operation;
            subsegment.Aws["request_id"] = args.Response.ResponseMetadata.RequestId;

            AddRequestSpecificInformation(serviceName, operation, args.Request, subsegment.Aws);
            AddResponseSpecificInformation(serviceName, operation, args.Response, subsegment.Aws);

            _recorder.EndSubsegment();
        }
コード例 #5
0
        public async Task <NumberPlateTrigger> FunctionHandler(S3Event evnt, ILambdaContext context)
        {
            var s3Event = evnt.Records?[0].S3;

            context.Logger.LogLine("EVENT Received: " + JsonConvert.SerializeObject(s3Event));

            if (regExNumberPlate == null)
            {
                context.Logger.LogLine("regExNumberPlate is not yet populated. Calling getNumberPlateFromSecretsManager()...");
                regExNumberPlate = await getNumberPlateFromSecretsManager(context);

                context.Logger.LogLine("regExNumberPlate is " + regExNumberPlate);
            }

            NumberPlateTrigger result = new NumberPlateTrigger
            {
                bucket        = s3Event.Bucket.Name,
                key           = s3Event.Object.Key,
                contentType   = "",
                contentLength = s3Event.Object.Size,
                charge        = int.Parse(Environment.GetEnvironmentVariable("TollgateCharge")),
                numberPlate   = new NumberPlate()
                {
                    numberPlateRegEx = this.regExNumberPlate,
                    detected         = false
                }
            };

            AWSXRayRecorder recorder = AWSXRayRecorder.Instance;

            recorder.BeginSubsegment("TollGantry::Detect Number Plate in Captured Image");
            recorder.AddMetadata("bucket", s3Event.Bucket.Name);
            recorder.AddMetadata("key", s3Event.Object.Key);
            recorder.AddMetadata("regex", this.regExNumberPlate);
            //
            // TODO: Call Rekognition to detect text in the captured image
            //
            recorder.EndSubsegment();

            //
            // Kick off the step function
            //
            context.Logger.LogLine("Starting the state machine");
            IAmazonStepFunctions stepFunctionsClient = new AmazonStepFunctionsClient();
            await stepFunctionsClient.StartExecutionAsync(new StartExecutionRequest()
            {
                StateMachineArn = Environment.GetEnvironmentVariable("NumberPlateProcessStateMachine"), Input = JsonConvert.SerializeObject(result)
            });

            context.Logger.LogLine("State machine started");
            return(result);
        }
コード例 #6
0
        /// <inheritdoc />
        public async Task <TResult> InterceptAsync <TResult>(Func <Task <TResult> > method, DbCommand command)
        {
            _recorder.BeginSubsegment(BuildSubsegmentName(command));
            try
            {
                _recorder.SetNamespace("remote");
                var ret = await method();

                CollectSqlInformation(command);

                return(ret);
            }
            catch (Exception e)
            {
                _recorder.AddException(e);
                throw;
            }
            finally
            {
                _recorder.EndSubsegment();
            }
        }
コード例 #7
0
        public void TestTryParseSubsegment()
        {
            using (var recorder = new AWSXRayRecorder())
            {
                recorder.BeginSegment("TraceHeaderTest", TraceId);
                recorder.BeginSubsegment("subjob");
                var subsegment = TraceContext.GetEntity();

                TraceHeader header;
                Assert.IsTrue(TraceHeader.TryParse(subsegment, out header));

                Assert.AreEqual(TraceId, header.RootTraceId);
                Assert.AreEqual(subsegment.Id, header.ParentId);
                Assert.AreEqual(SampleDecision.Sampled, header.Sampled);

                recorder.EndSubsegment();
                recorder.EndSegment();
            }
        }
コード例 #8
0
        /// <summary>
        /// Process http response.
        /// </summary>
        internal static void ProcessResponse(HttpContext httpContext)
        {
            HttpResponse response = httpContext.Response;

            if (!AWSXRayRecorder.Instance.IsTracingDisabled())
            {
                var responseAttributes = PopulateResponseAttributes(response);
                _recorder.AddHttpInformation("response", responseAttributes);
            }

            if (AWSXRayRecorder.IsLambda())
            {
                _recorder.EndSubsegment();
            }
            else
            {
                _recorder.EndSegment();
            }
        }
コード例 #9
0
        /// <summary>
        /// Processes HTTP response
        /// </summary>
        private void ProcessHTTPResponse(HttpContext context)
        {
            HttpResponse response = context.Response;

            if (!AWSXRayRecorder.Instance.IsTracingDisabled())
            {
                var responseAttributes = new Dictionary <string, object>();
                PopulateResponseAttributes(response, responseAttributes);
                _recorder.AddHttpInformation("response", responseAttributes);
            }

            if (AWSXRayRecorder.Instance.IsLambda())
            {
                _recorder.EndSubsegment();
            }
            else
            {
                _recorder.EndSegment();
            }
        }
コード例 #10
0
        public void TestLogErrorModeForContextMissingStrategy()
        {
            using (var recorder = new AWSXRayRecorder())
            {
                recorder.ContextMissingStrategy = ContextMissingStrategy.LOG_ERROR;

                recorder.EndSegment();
                recorder.BeginSubsegment("no segment");
                recorder.EndSubsegment();
                recorder.SetNamespace("dummy namespace");
                recorder.AddAnnotation("key", "value");
                recorder.AddHttpInformation("key", "value");
                recorder.MarkError();
                recorder.MarkFault();
                recorder.MarkThrottle();
                recorder.AddException(new ArgumentNullException());
                recorder.AddPrecursorId(Entity.GenerateId());
                recorder.AddSqlInformation("sqlKey", "value");
                recorder.AddMetadata("key", "value");
            }
        }
コード例 #11
0
        private TResult Intercept <TResult>(Func <TResult> method)
        {
            AWSXRayRecorder recorder = AWSXRayRecorder.Instance;

            recorder.BeginSubsegment(Connection.Database + "@" + SqlUtil.RemovePortNumberFromDataSource(Connection.DataSource));
            try
            {
                recorder.SetNamespace("remote");
                var ret = method();
                CollectSqlInformation();

                return(ret);
            }
            catch (Exception e)
            {
                recorder.AddException(e);
                throw;
            }
            finally
            {
                recorder.EndSubsegment();
            }
        }
コード例 #12
0
        public void TestSyncCreateSegmentAndSubsegments()
        {
            _recorder.BeginSegment("parent", TraceId);

            Segment parent = (Segment)AWSXRayRecorder.Instance.TraceContext.GetEntity();

            _recorder.BeginSubsegment("child");
            Subsegment child = (Subsegment)AWSXRayRecorder.Instance.TraceContext.GetEntity();

            _recorder.EndSubsegment();
            Assert.ReferenceEquals(AWSXRayRecorder.Instance.TraceContext.GetEntity(), parent);

            _recorder.EndSegment();

            Assert.ReferenceEquals(parent, child.Parent);
            Assert.IsTrue(parent.Subsegments.Contains(child));
        }
コード例 #13
0
        /// <summary>
        /// Processes End request by ending subsegment.
        /// </summary>
        private void ProcessEndRequest(IExecutionContext executionContext)
        {
            Entity subsegment;

            try
            {
                subsegment = _recorder.GetEntity();
            }
            catch (EntityNotAvailableException e)
            {
                _recorder.TraceContext.HandleEntityMissing(_recorder, e, "Cannot get entity from the trace context while processing response of AWS SDK request.");
                return;
            }

            var responseContext = executionContext.ResponseContext;
            var requestContext  = executionContext.RequestContext;

            if (responseContext == null)
            {
                _logger.DebugFormat("Failed to handle AfterResponseEvent, because response is null.");
                return;
            }

            var client = executionContext.RequestContext.ClientConfig;

            if (client == null)
            {
                _logger.DebugFormat("Failed to handle AfterResponseEvent, because client from the Response Context is null");
                return;
            }

            var serviceName = RemoveAmazonPrefixFromServiceName(requestContext.Request.ServiceName);
            var operation   = RemoveSuffix(requestContext.OriginalRequest.GetType().Name, "Request");

            subsegment.Aws["region"]    = client.RegionEndpoint?.SystemName;
            subsegment.Aws["operation"] = operation;
            if (responseContext.Response == null)
            {
                if (requestContext.Request.Headers.TryGetValue("x-amzn-RequestId", out string requestId))
                {
                    subsegment.Aws["request_id"] = requestId;
                }
                // s3 doesn't follow request header id convention
                else
                {
                    if (requestContext.Request.Headers.TryGetValue("x-amz-request-id", out requestId))
                    {
                        subsegment.Aws["request_id"] = requestId;
                    }

                    if (requestContext.Request.Headers.TryGetValue("x-amz-id-2", out requestId))
                    {
                        subsegment.Aws["id_2"] = requestId;
                    }
                }
            }
            else
            {
                subsegment.Aws["request_id"] = responseContext.Response.ResponseMetadata.RequestId;

                // try getting x-amz-id-2 if dealing with s3 request
                if (responseContext.Response.ResponseMetadata.Metadata.TryGetValue("x-amz-id-2", out string extendedRequestId))
                {
                    subsegment.Aws["id_2"] = extendedRequestId;
                }

                AddResponseSpecificInformation(serviceName, operation, responseContext.Response, subsegment.Aws);
            }

            if (responseContext.HttpResponse != null)
            {
                AddHttpInformation(responseContext.HttpResponse);
            }

            AddRequestSpecificInformation(serviceName, operation, requestContext.OriginalRequest, subsegment.Aws);
            _recorder.EndSubsegment();
        }
コード例 #14
0
        public async Task <NumberPlateTrigger> FunctionHandler(S3Event evnt, ILambdaContext context)
        {
            var s3Event = evnt.Records?[0].S3;

            context.Logger.LogLine("EVENT Received: " + JsonConvert.SerializeObject(s3Event));

            if (regExNumberPlate == null)
            {
                context.Logger.LogLine("regExNumberPlate is not yet populated. Calling getNumberPlateFromSecretsManager()...");
                regExNumberPlate = await getNumberPlateFromSecretsManager(context);

                context.Logger.LogLine("regExNumberPlate is " + regExNumberPlate);
            }

            //var response = await this.S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key);
            //return response.Headers.ContentType;

            NumberPlateTrigger result = new NumberPlateTrigger
            {
                bucket        = s3Event.Bucket.Name,
                key           = s3Event.Object.Key,
                contentType   = "",
                contentLength = s3Event.Object.Size,
                charge        = int.Parse(Environment.GetEnvironmentVariable("TollgateCharge")),
                numberPlate   = new NumberPlate()
                {
                    numberPlateRegEx = this.regExNumberPlate,
                    detected         = false
                }
            };

            recorder.BeginSubsegment("TollGantry::Detect Number Plate in Captured Image");
            recorder.AddMetadata("bucket", s3Event.Bucket.Name);
            recorder.AddMetadata("key", s3Event.Object.Key);
            recorder.AddMetadata("regex", this.regExNumberPlate);

            S3Object s3Object = new S3Object();

            s3Object.Bucket = s3Event.Bucket.Name;
            s3Object.Name   = s3Event.Object.Key;
            DetectTextRequest detectTextReq = new DetectTextRequest {
                Image = new Image {
                    S3Object = s3Object
                }
            };

            context.Logger.LogLine("Calling Rekognition ... ");
            DetectTextResponse detectTextResponse = await rekognitionClient.DetectTextAsync(detectTextReq);

            context.Logger.LogLine($"Response from Rekognition: {JsonConvert.SerializeObject(detectTextResponse)}");

            // Check if the a valid number was detected...
            foreach (var textItem in detectTextResponse.TextDetections)
            {
                if (!result.numberPlate.detected && textItem.Type.Value == "LINE" && textItem.Confidence > float.Parse(Environment.GetEnvironmentVariable("RekognitionTextMinConfidence")))
                {
                    Regex           regex   = new Regex(regExNumberPlate);
                    MatchCollection matches = regex.Matches(textItem.DetectedText);
                    context.Logger.LogLine($"Matches collection: {matches.Count}");
                    string plateNumber = "";
                    foreach (Match match in matches)
                    {
                        plateNumber += (match.Groups[1].Value + match.Groups[2].Value);
                    }
                    if (!string.IsNullOrEmpty(plateNumber))
                    {
                        result.numberPlate.detected          = true;
                        result.numberPlate.confidence        = textItem.Confidence;
                        result.numberPlate.numberPlateString = plateNumber;
                        context.Logger.LogLine($"A valid plate number was detected ({result.numberPlate.numberPlateString})");
                    }
                }
            }

            recorder.EndSubsegment();

            //
            // At this point, we either know it is a valid number plate
            // or it couldn't be determined with adequate confidence
            // so we need manual intervention
            //

            //
            // Kick off the step function
            //

            context.Logger.LogLine("Starting the state machine");
            //TODO: add code to start the state machine
            context.Logger.LogLine("State machine started");

            return(result);
        }
コード例 #15
0
        private void ProcessEndRequest(IExecutionContext executionContext)
        {
            Entity subSegment;

            try
            {
                subSegment = _recorder.GetEntity();
            }
            catch (EntityNotAvailableException e)
            {
                _recorder.TraceContext.HandleEntityMissing(_recorder, e, "Cannot get entity from the trace context while processing response of AWS SDK request.");
                return;
            }

            var responseContext = executionContext.ResponseContext;
            var requestContext  = executionContext.RequestContext;

            if (responseContext == null)
            {
                return;
            }

            var client = executionContext.RequestContext.ClientConfig;

            if (client == null)
            {
                return;
            }

            var serviceName = RemoveAmazonPrefixFromServiceName(requestContext.Request.ServiceName);
            var operation   = RemoveSuffix(requestContext.OriginalRequest.GetType().Name, "Request");

            subSegment.Aws["region"]    = client.RegionEndpoint?.SystemName;
            subSegment.Aws["operation"] = operation;

            if (responseContext.Response == null)
            {
                if (requestContext.Request.Headers.TryGetValue("x-amzn-RequestId", out var requestId))
                {
                    subSegment.Aws["request_id"] = requestId;
                }
                // s3 doesn't follow request header id convention
                else
                {
                    if (requestContext.Request.Headers.TryGetValue(S3RequestIdHeaderKey, out requestId))
                    {
                        subSegment.Aws["request_id"] = requestId;
                    }

                    if (requestContext.Request.Headers.TryGetValue(S3ExtendedRequestIdHeaderKey, out requestId))
                    {
                        subSegment.Aws[ExtendedRequestIdSegmentKey] = requestId;
                    }
                }
            }
            else
            {
                subSegment.Aws["request_id"] = responseContext.Response.ResponseMetadata.RequestId;
                AddResponseSpecificInformation(serviceName, operation, responseContext.Response, subSegment.Aws);
            }

            if (responseContext.HttpResponse != null)
            {
                AddHttpInformation(responseContext.HttpResponse);
            }

            AddRequestSpecificInformation(serviceName, operation, requestContext.OriginalRequest, subSegment.Aws);

            _recorder.EndSubsegment();
        }
コード例 #16
0
 /// <summary>
 /// Process to end subsegment
 /// </summary>
 internal static void ProcessEndCommand()
 {
     _recorder.EndSubsegment();
 }