-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add LLM environment detection telemetry data point with rule-based detection system and multiple environment support #50663
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
09091ca
Initial plan
Copilot 57b7837
Add LLM environment detection telemetry with CLAUDECODE support
Copilot dc5494e
Change LLM telemetry to use simpler property name and descriptive values
Copilot e4c52b4
Simplify LLM telemetry tests to only test essential scenarios
Copilot c242c38
Merge branch 'main' into copilot/fix-50641
baronfel 92ec5d8
Implement rule-based environment detection system with nullable refer…
Copilot 7736af5
Update src/Cli/dotnet/Telemetry/LLMEnvironmentDetectorForTelemetry.cs
baronfel 984a512
Fix LLM detection syntax and add multiple environment support with te…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 33 additions & 56 deletions
89
src/Cli/dotnet/Telemetry/CIEnvironmentDetectorForTelemetry.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable disable | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Microsoft.DotNet.Cli.Telemetry; | ||
|
||
internal class CIEnvironmentDetectorForTelemetry : ICIEnvironmentDetector | ||
{ | ||
// Systems that provide boolean values only, so we can simply parse and check for true | ||
private static readonly string[] _booleanVariables = [ | ||
// Azure Pipelines - https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables#system-variables-devops-services | ||
"TF_BUILD", | ||
// GitHub Actions - https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables | ||
"GITHUB_ACTIONS", | ||
// AppVeyor - https://www.appveyor.com/docs/environment-variables/ | ||
"APPVEYOR", | ||
// A general-use flag - Many of the major players support this: AzDo, GitHub, GitLab, AppVeyor, Travis CI, CircleCI. | ||
// Given this, we could potentially remove all of these other options? | ||
"CI", | ||
// Travis CI - https://docs.travis-ci.com/user/environment-variables/#default-environment-variables | ||
"TRAVIS", | ||
// CircleCI - https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables | ||
"CIRCLECI", | ||
]; | ||
|
||
// Systems where every variable must be present and not-null before returning true | ||
private static readonly string[][] _allNotNullVariables = [ | ||
private static readonly EnvironmentDetectionRule[] _detectionRules = [ | ||
// Systems that provide boolean values only, so we can simply parse and check for true | ||
new BooleanEnvironmentRule( | ||
// Azure Pipelines - https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables#system-variables-devops-services | ||
"TF_BUILD", | ||
// GitHub Actions - https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables | ||
"GITHUB_ACTIONS", | ||
// AppVeyor - https://www.appveyor.com/docs/environment-variables/ | ||
"APPVEYOR", | ||
// A general-use flag - Many of the major players support this: AzDo, GitHub, GitLab, AppVeyor, Travis CI, CircleCI. | ||
// Given this, we could potentially remove all of these other options? | ||
"CI", | ||
// Travis CI - https://docs.travis-ci.com/user/environment-variables/#default-environment-variables | ||
"TRAVIS", | ||
// CircleCI - https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables | ||
"CIRCLECI" | ||
), | ||
|
||
// Systems where every variable must be present and not-null before returning true | ||
// AWS CodeBuild - https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html | ||
["CODEBUILD_BUILD_ID", "AWS_REGION"], | ||
new AllPresentEnvironmentRule("CODEBUILD_BUILD_ID", "AWS_REGION"), | ||
// Jenkins - https://github.com/jenkinsci/jenkins/blob/master/core/src/main/resources/jenkins/model/CoreEnvironmentContributor/buildEnv.groovy | ||
["BUILD_ID", "BUILD_URL"], | ||
new AllPresentEnvironmentRule("BUILD_ID", "BUILD_URL"), | ||
// Google Cloud Build - https://cloud.google.com/build/docs/configuring-builds/substitute-variable-values#using_default_substitutions | ||
["BUILD_ID", "PROJECT_ID"] | ||
]; | ||
|
||
// Systems where the variable must be present and not-null | ||
private static readonly string[] _ifNonNullVariables = [ | ||
// TeamCity - https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html#Predefined+Server+Build+Parameters | ||
"TEAMCITY_VERSION", | ||
// JetBrains Space - https://www.jetbrains.com/help/space/automation-environment-variables.html#general | ||
"JB_SPACE_API_URL" | ||
new AllPresentEnvironmentRule("BUILD_ID", "PROJECT_ID"), | ||
|
||
// Systems where the variable must be present and not-null | ||
new AnyPresentEnvironmentRule( | ||
// TeamCity - https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html#Predefined+Server+Build+Parameters | ||
"TEAMCITY_VERSION", | ||
// JetBrains Space - https://www.jetbrains.com/help/space/automation-environment-variables.html#general | ||
"JB_SPACE_API_URL" | ||
) | ||
]; | ||
|
||
public bool IsCIEnvironment() | ||
{ | ||
foreach (var booleanVariable in _booleanVariables) | ||
{ | ||
if (bool.TryParse(Environment.GetEnvironmentVariable(booleanVariable), out bool envVar) && envVar) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
foreach (var variables in _allNotNullVariables) | ||
{ | ||
if (variables.All((variable) => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(variable)))) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
foreach (var variable in _ifNonNullVariables) | ||
{ | ||
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(variable))) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
return _detectionRules.Any(rule => rule.IsMatch()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.DotNet.Cli.Telemetry; | ||
|
||
/// <summary> | ||
/// Base class for environment detection rules that can be evaluated against environment variables. | ||
/// </summary> | ||
internal abstract class EnvironmentDetectionRule | ||
{ | ||
/// <summary> | ||
/// Evaluates the rule against the current environment. | ||
/// </summary> | ||
/// <returns>True if the rule matches the current environment; otherwise, false.</returns> | ||
public abstract bool IsMatch(); | ||
} | ||
|
||
/// <summary> | ||
/// Rule that matches when any of the specified environment variables is set to "true". | ||
/// </summary> | ||
internal class BooleanEnvironmentRule : EnvironmentDetectionRule | ||
{ | ||
private readonly string[] _variables; | ||
|
||
public BooleanEnvironmentRule(params string[] variables) | ||
{ | ||
_variables = variables ?? throw new ArgumentNullException(nameof(variables)); | ||
} | ||
|
||
public override bool IsMatch() | ||
{ | ||
return _variables.Any(variable => | ||
bool.TryParse(Environment.GetEnvironmentVariable(variable), out bool value) && value); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Rule that matches when all specified environment variables are present and not null/empty. | ||
/// </summary> | ||
internal class AllPresentEnvironmentRule : EnvironmentDetectionRule | ||
{ | ||
private readonly string[] _variables; | ||
|
||
public AllPresentEnvironmentRule(params string[] variables) | ||
{ | ||
_variables = variables ?? throw new ArgumentNullException(nameof(variables)); | ||
} | ||
|
||
public override bool IsMatch() | ||
{ | ||
return _variables.All(variable => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(variable))); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Rule that matches when any of the specified environment variables is present and not null/empty. | ||
/// </summary> | ||
internal class AnyPresentEnvironmentRule : EnvironmentDetectionRule | ||
{ | ||
private readonly string[] _variables; | ||
|
||
public AnyPresentEnvironmentRule(params string[] variables) | ||
{ | ||
_variables = variables ?? throw new ArgumentNullException(nameof(variables)); | ||
} | ||
|
||
public override bool IsMatch() | ||
{ | ||
return _variables.Any(variable => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(variable))); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Rule that matches when any of the specified environment variables is present and not null/empty, | ||
/// and returns the associated result value. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the result value.</typeparam> | ||
internal class EnvironmentDetectionRuleWithResult<T> where T : class | ||
baronfel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private readonly string[] _variables; | ||
private readonly T _result; | ||
|
||
public EnvironmentDetectionRuleWithResult(T result, params string[] variables) | ||
{ | ||
_variables = variables ?? throw new ArgumentNullException(nameof(variables)); | ||
_result = result ?? throw new ArgumentNullException(nameof(result)); | ||
} | ||
|
||
/// <summary> | ||
/// Evaluates the rule and returns the result if matched. | ||
/// </summary> | ||
/// <returns>The result value if the rule matches; otherwise, null.</returns> | ||
public T? GetResult() | ||
{ | ||
return _variables.Any(variable => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(variable))) | ||
? _result | ||
: null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.DotNet.Cli.Telemetry; | ||
|
||
internal interface ILLMEnvironmentDetector | ||
{ | ||
string? GetLLMEnvironment(); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Cli/dotnet/Telemetry/LLMEnvironmentDetectorForTelemetry.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Linq; | ||
|
||
namespace Microsoft.DotNet.Cli.Telemetry; | ||
|
||
internal class LLMEnvironmentDetectorForTelemetry : ILLMEnvironmentDetector | ||
{ | ||
private static readonly EnvironmentDetectionRuleWithResult<string>[] _detectionRules = [ | ||
// Claude Code | ||
new EnvironmentDetectionRuleWithResult<string>("claude", "CLAUDECODE"), | ||
// Cursor AI | ||
new EnvironmentDetectionRuleWithResult<string>("cursor", "CURSOR_EDITOR") | ||
]; | ||
|
||
public string? GetLLMEnvironment() | ||
{ | ||
var results = _detectionRules.Select(r => r.GetResult()).Where(r => r != null).ToArray(); | ||
return results.Length > 0 ? string.Join(", ", results) : null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.