Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit calculated agent service name to 80 characters #2266

Merged
merged 1 commit into from May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Agent.Listener/Configuration/ServiceControlManager.cs
Expand Up @@ -53,6 +53,18 @@ public void CalculateServiceName(AgentSettings settings, string serviceNamePatte
}

serviceName = StringUtil.Format(serviceNamePattern, accountName, settings.PoolName, settings.AgentName);

if (serviceName.Length > 80)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i assume 80 won't break, only 81+ will break.

Copy link
Contributor Author

@juliobbv juliobbv May 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. I tried out with a calculated name with exactly 80 characters, and it works.

{
Trace.Verbose($"Calculated service name is too long (> 80 chars). Trying again by calculating a shorter name.");

string accountNameSubstring = StringUtil.SubstringPrefix(accountName, 30);
string poolNameSubstring = StringUtil.SubstringPrefix(settings.PoolName, 30);
string agentNameSubstring = StringUtil.SubstringPrefix(settings.AgentName, 20);

serviceName = StringUtil.Format(serviceNamePattern, accountNameSubstring, poolNameSubstring, agentNameSubstring);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the serviceNamePattern contains any chars that will contribute to the 80 chars?

}

serviceDisplayName = StringUtil.Format(serviceDisplayNamePattern, accountName, settings.PoolName, settings.AgentName);

Trace.Info($"Service name '{serviceName}' display name '{serviceDisplayName}' will be used for service configuration.");
Expand Down
5 changes: 5 additions & 0 deletions src/Agent.Sdk/Util/StringUtil.cs
Expand Up @@ -31,6 +31,11 @@ static StringUtil()
#endif
}

public static string SubstringPrefix(string value, int count)
{
return value?.Substring(0, Math.Min(value.Length, count));
}

public static T ConvertFromJson<T>(string value)
{
return JsonConvert.DeserializeObject<T>(value, s_serializerSettings.Value);
Expand Down