/// <summary> Set the name of the current transaction. Supports web applications only. </summary> /// /// <exception cref="ArgumentNullException"> Thrown when <paramref name="key"/> is null. </exception> /// /// <param name="category"> The category of this transaction, used to distinguish different types /// of transactions. Only the first 1000 characters are retained. If null is passed, the /// category defaults to "Custom". </param> /// <param name="name"> The name of the transaction starting with a forward slash. example: /// /store/order Only the first 1000 characters are retained. </param> public void SetTransactionName(string category, string name) { name = name ?? throw new ArgumentNullException(nameof(name)); using (new IgnoreWork()) { // Default to "Custom" category if none provided if (string.IsNullOrEmpty(category?.Trim())) { category = MetricNames.Custom; } // Get rid of any slashes category = category.Trim(TrimPathChar); name = name.Trim(TrimPathChar); // Clamp the category and name to a predetermined length category = Clamper.ClampLength(category); name = Clamper.ClampLength(name); var transaction = GetCurrentInternalTransaction(); var currentTransactionName = transaction.CandidateTransactionName.CurrentTransactionName; var newTransactionName = currentTransactionName.IsWeb ? TransactionName.ForWebTransaction(category, name) : TransactionName.ForOtherTransaction(category, name); transaction.CandidateTransactionName.TrySet(newTransactionName, TransactionNamePriority.UserTransactionName); } }
public static TransactionName ForCustomTransaction(bool isWeb, string name, int maxLength) { // Note: In our public docs to tells users that they must prefix their metric names with "Custom/", but there's no mechanism that actually enforces this restriction, so there's no way to know whether it'll be there or not. For consistency, we'll just strip off "Custom/" if there's at all and then we know it's consistently not there. if (name.StartsWith("Custom/")) { name = name.Substring(7); } name = Clamper.ClampLength(name.Trim(), maxLength); if (name.Length <= 0) { throw new ArgumentException("A segment name cannot be an empty string."); } return(new TransactionName(isWeb, MetricNames.Custom, name)); }
/// <summary> Gets custom metric suffix. </summary> /// <exception cref="ArgumentException"> Thrown if <paramref name="name"/> is null or empty. </exception> /// <param name="name"> The name to process. </param> /// <returns> The custom metric suffix. </returns> private static string GetCustomMetricSuffix(string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("The name parameter must have a value that is not null or empty."); } name = Clamper.ClampLength(name); // If the name provided already contains the "Custom/" prefix, remove it and use the remaining segment as the "name" if (name.StartsWith(CustomMetricNamePrefixAndSeparator, StringComparison.InvariantCultureIgnoreCase)) { name = name.Substring(CustomMetricNamePrefixAndSeparator.Length); } return(name); }
private string NormalizeString(string data) { return(Clamper.ClampLength(data.Trim(), 255)); }