Beispiel #1
0
        public void TryCorrelate_WithIncorrectOperationParentId_DoesntSetExpectedOperationId(string prefix, string postfix)
        {
            // Arrange
            var operationId = $"operation-{Guid.NewGuid()}";
            var headers     = new Dictionary <string, StringValues>
            {
                ["Request-Id"] = prefix + operationId + postfix
            };

            HttpContext context         = CreateHttpContext(headers);
            var         contextAccessor = new Mock <IHttpContextAccessor>();

            contextAccessor.Setup(accessor => accessor.HttpContext).Returns(context);
            var correlationAccessor = new DefaultCorrelationInfoAccessor();

            var options     = Options.Create(new HttpCorrelationInfoOptions());
            var correlation = new HttpCorrelation(options, contextAccessor.Object, correlationAccessor, NullLogger <HttpCorrelation> .Instance);

            // Act / Assert
            Assert.True(correlation.TryHttpCorrelate(out string errorMessage), errorMessage);
            Assert.Null(errorMessage);
            var correlationInfo = context.Features.Get <CorrelationInfo>();

            Assert.NotEqual(operationId, correlationInfo.OperationId);
            Assert.Null(correlationInfo.OperationParentId);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpBasedAzureFunction"/> class.
        /// </summary>
        /// <param name="httpCorrelation">The correlation service to provide information of related requests.</param>
        /// <param name="logger">The logger instance to write diagnostic messages throughout the execution of the HTTP trigger.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="httpCorrelation"/> is <c>null</c></exception>
        protected HttpBasedAzureFunction(HttpCorrelation httpCorrelation, ILogger logger)
        {
            Guard.NotNull(httpCorrelation, nameof(httpCorrelation), "Requires an HTTP correlation instance");
            _httpCorrelation = httpCorrelation;

            Logger = logger ?? NullLogger.Instance;

            var jsonSettings = new JsonSerializerSettings
            {
                MaxDepth          = 10,
                NullValueHandling = NullValueHandling.Ignore,
                TypeNameHandling  = TypeNameHandling.None
            };

            jsonSettings.Converters.Add(new StringEnumConverter());
            JsonSerializer = JsonSerializer.Create(jsonSettings);

            var jsonOptions = new JsonSerializerOptions
            {
                MaxDepth         = 10,
                IgnoreNullValues = true
            };

            jsonOptions.Converters.Add(new JsonStringEnumConverter());
            OutputFormatters = new FormatterCollection <IOutputFormatter> {
                new SystemTextJsonOutputFormatter(jsonOptions)
            };
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="HealthFunction" /> class.
 /// </summary>
 /// <param name="healthCheckService">The service to check the current health of the running Azure Function.</param>
 /// <param name="httpCorrelation">The instance to handle the HTTP request correlation.</param>
 /// <param name="logger">The logger instance to write diagnostic trace messages while handling the HTTP request.</param>
 /// <exception cref="ArgumentNullException">Thrown when the <paramref name="httpCorrelation"/> is <c>null</c>.</exception>
 public HealthFunction(
     HealthCheckService healthCheckService,
     HttpCorrelation httpCorrelation,
     ILogger <HealthFunction> logger)
     : base(httpCorrelation, logger)
 {
     Guard.NotNull(healthCheckService, nameof(healthCheckService), "Requires a health check service to check the current health of the running Azure Function");
     _healthCheckService = healthCheckService;
 }
Beispiel #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpBasedAzureFunction"/> class.
        /// </summary>
        /// <param name="httpCorrelation">The correlation service to provide information of related requests.</param>
        /// <param name="logger">The logger instance to write diagnostic messages throughout the execution of the HTTP trigger.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="httpCorrelation"/> is <c>null</c></exception>
        protected HttpBasedAzureFunction(HttpCorrelation httpCorrelation, ILogger logger)
        {
            Guard.NotNull(httpCorrelation, nameof(httpCorrelation), "Requires an HTTP correlation instance");
            _httpCorrelation = httpCorrelation;

            Logger = logger ?? NullLogger.Instance;

            var options = new JsonSerializerOptions();

            options.IgnoreNullValues     = true;
            options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
            options.Converters.Add(new JsonStringEnumConverter());
            JsonOptions      = options;
            OutputFormatters = new FormatterCollection <IOutputFormatter> {
                new SystemTextJsonOutputFormatter(JsonOptions)
            };
        }
Beispiel #5
0
        public void Correlation_GetCorrelationInfo_UsesStubbedCorrelation()
        {
            // Arrange
            var expected            = new CorrelationInfo($"operation-{Guid.NewGuid()}", $"transaction-{Guid.NewGuid()}");
            var correlationAccessor = new DefaultCorrelationInfoAccessor();

            correlationAccessor.SetCorrelationInfo(expected);

            IOptions <HttpCorrelationInfoOptions> options = Options.Create(new HttpCorrelationInfoOptions());
            IHttpContextAccessor      contextAccessor     = Mock.Of <IHttpContextAccessor>();
            ILogger <HttpCorrelation> logger = NullLogger <HttpCorrelation> .Instance;
            var correlation = new HttpCorrelation(options, contextAccessor, correlationAccessor, logger);

            // Act
            CorrelationInfo actual = correlation.GetCorrelationInfo();

            // Assert
            Assert.Same(expected, actual);
        }
Beispiel #6
0
        public void TryCorrelate_WithCorrectOperationParentIdWithDisabledUpstreamExtraction_DoesntSetExpectedOperationId()
        {
            // Arrange
            var    operationIdFromGeneration = $"operation-{Guid.NewGuid()}";
            var    operationIdFromUpstream   = $"operation-{Guid.NewGuid()}";
            string operationParentId         = $"|{operationIdFromUpstream}.";
            var    headers = new Dictionary <string, StringValues>
            {
                ["Request-Id"] = operationParentId
            };

            HttpContext context         = CreateHttpContext(headers);
            var         contextAccessor = new Mock <IHttpContextAccessor>();

            contextAccessor.Setup(accessor => accessor.HttpContext).Returns(context);
            var correlationAccessor = new DefaultCorrelationInfoAccessor();

            var options = Options.Create(new HttpCorrelationInfoOptions
            {
                Operation       = { GenerateId = () => operationIdFromGeneration },
                UpstreamService = { ExtractFromRequest = false }
            });
            var correlation = new HttpCorrelation(options, contextAccessor.Object, correlationAccessor, NullLogger <HttpCorrelation> .Instance);

            // Act
            bool isCorrelated = correlation.TryHttpCorrelate(out string errorMessage);

            // Assert
            Assert.True(isCorrelated, errorMessage);
            Assert.Null(errorMessage);
            var correlationInfo = context.Features.Get <CorrelationInfo>();

            Assert.NotEqual(operationIdFromUpstream, correlationInfo.OperationId);
            Assert.Equal(operationIdFromGeneration, correlationInfo.OperationId);
            Assert.Null(correlationInfo.OperationParentId);
        }
Beispiel #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HttpTriggerFunction"/> class.
 /// </summary>
 public HttpTriggerFunction(HttpCorrelation correlationService, ILogger <HttpTriggerFunction> logger)
 {
     _correlationService = correlationService;
     _logger             = logger;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="OrderFunction"/> class.
 /// </summary>
 /// <param name="secretProvider">The instance that provides secrets to the HTTP trigger.</param>
 /// <param name="httpCorrelation">The instance to handle the HTTP request correlation.</param>
 /// <param name="logger">The logger instance to write diagnostic trace messages while handling the HTTP request.</param>
 /// <exception cref="ArgumentNullException">Thrown when the <paramref name="secretProvider"/> or <paramref name="httpCorrelation"/> is <c>null</c>.</exception>
 public OrderFunction(ISecretProvider secretProvider, HttpCorrelation httpCorrelation, ILogger <OrderFunction> logger) : base(httpCorrelation, logger)
 {
     Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider instance");
     _secretProvider = secretProvider;
 }