//-----------------------------------------------------------------------------
//
// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// A copy of the License is located at
//
// http://aws.amazon.com/apache2.0
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//
//-----------------------------------------------------------------------------
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.RegularExpressions;
using Amazon.Runtime.Internal.Util;
using Amazon.XRay.Recorder.Core.Internal.Utils;
[module: SuppressMessage("Microsoft.Design", "CA1036:OverrideMethodsOnComparableTypes", Scope = "type", Target = "Amazon.XRay.Recorder.Core.Sampling.Local.SamplingRule", Justification = "Only used for sorting")]
namespace Amazon.XRay.Recorder.Core.Sampling.Local
{
///
/// It represents the Rules used for sampling.
///
public class SamplingRule
{
private static readonly Logger _logger = Logger.GetLogger(typeof(SamplingRule));
private int _fixedTarget;
///
/// Initializes a new instance of the class.
///
public SamplingRule()
{
_fixedTarget = -1;
this.Rate = -1d;
}
///
/// Initializes a new instance of the class.
///
/// Name of the host. The value can include a multi-character match wildcard(*) or a single-character match wildcard (?) anywhere in the string.
/// The URL path. The value can include a multi-character match wildcard(*) or a single-character match wildcard (?) anywhere in the string.
/// Http method. The value can be a multi-character match wildcard(*) to match any method.
/// It defines a trace collection target for a rule with no sampling in the unit of traces per second. Before the threshold is met, all request will be traced. After the threshold is met, sampling rate is triggered.
/// The rate at which request will be sampled. E.g. with 5% sampling rate, average 5 request out of 100 will be traced.
/// Description of the sampling rule.
public SamplingRule(string host, string urlPath, string httpMethod, int fixedTarget, double rate, string description = null)
{
this.Host = host;
this.HttpMethod = httpMethod;
this.UrlPath = urlPath;
this.FixedTarget = fixedTarget;
this.Rate = rate;
this.Description = description;
}
///
/// Gets or sets the host of the rule
///
public string Host { get; set; }
///
/// Gets or sets the service name from V1 sampling rule json file.
///
public string ServiceName { get; set; }
///
/// Gets or sets the http method of the rule
///
public string HttpMethod { get; set; }
///
/// Gets or sets the url path of the rule
///
public string UrlPath { get; set; }
///
/// Gets or sets the fixed target rate of the rule in the unit of traces/second
///
public int FixedTarget
{
get
{
return _fixedTarget;
}
set
{
_fixedTarget = value;
RateLimiter = new RateLimiter(value);
}
}
///
/// Gets the rate limiter which had the limit set to fixed target rate
///
public RateLimiter RateLimiter { get; private set; }
///
/// Gets or sets the sampling rate
///
public double Rate { get; set; }
///
/// Gets or sets the description.
///
public string Description { get; set; }
///
/// Given the service name, http method and url path of a http request, check whether the rule matches the request
///
/// host name of the request
/// url path of the request
/// http method of the request
/// It returns true if the rule matches the request, otherwise it returns false.
public bool IsMatch(string hostToMatch, string urlPathToMatch, string httpMethodToMatch)
{
try
{
return StringExtension.IsMatch(hostToMatch, Host) && StringExtension.IsMatch(urlPathToMatch, UrlPath) && StringExtension.IsMatch(httpMethodToMatch, HttpMethod);
}
catch (RegexMatchTimeoutException e)
{
_logger.Error(e, "Match rule timeout. Rule: Host = {0}, UrlPath = {1}, HttpMethod = {2}. Input: hostToMatch = {3}, urlPathToMatch = {4}, httpMethodToMatch = {5}.", Host, UrlPath, HttpMethod, hostToMatch, urlPathToMatch, httpMethodToMatch);
return false;
}
}
///
/// Generate a string out of this instance of the class
///
/// The string generated from current object
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "hostToMatch={0}, httpMethodToMatch={1}, urlPathToMatch={2}, fixedTarget={3}, rate={4}, description={5}", Host, HttpMethod, UrlPath, FixedTarget, Rate, Description);
}
}
}