Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/otel/common/workspaceOTelMetadata.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { URI } from '../../../util/vs/base/common/uri';
7
import { isEqualOrParent, relativePath } from '../../../util/vs/base/common/resources';
8
import { getOrderedRepoInfosFromContext, type IGitService, normalizeFetchUrl, type RepoContext } from '../../git/common/gitService';
9
import { CopilotChatAttr } from './genAiAttributes';
10
11
export interface WorkspaceOTelMetadata {
12
readonly headBranchName?: string;
13
readonly headCommitHash?: string;
14
readonly remoteUrl?: string;
15
readonly fileRelativePath?: string;
16
}
17
18
/**
19
* Synchronously resolve workspace metadata from the active repository.
20
* Uses `activeRepository.get()` which is non-blocking.
21
*/
22
export function resolveWorkspaceOTelMetadata(
23
gitService: IGitService,
24
fileUri?: URI,
25
): WorkspaceOTelMetadata {
26
const repoContext = gitService.activeRepository?.get();
27
if (!repoContext) {
28
return {};
29
}
30
return buildWorkspaceMetadata(repoContext, fileUri);
31
}
32
33
function buildWorkspaceMetadata(repoContext: RepoContext, fileUri?: URI): WorkspaceOTelMetadata {
34
let remoteUrl: string | undefined;
35
const repoInfo = Array.from(getOrderedRepoInfosFromContext(repoContext))[0];
36
if (repoInfo?.fetchUrl) {
37
remoteUrl = normalizeFetchUrl(repoInfo.fetchUrl);
38
}
39
40
let fileRelativePath: string | undefined;
41
if (fileUri && isEqualOrParent(fileUri, repoContext.rootUri)) {
42
fileRelativePath = relativePath(repoContext.rootUri, fileUri);
43
}
44
45
return {
46
headBranchName: repoContext.headBranchName,
47
headCommitHash: repoContext.headCommitHash,
48
remoteUrl,
49
fileRelativePath,
50
};
51
}
52
53
/**
54
* Convert workspace metadata to OTel attributes, omitting undefined values.
55
*/
56
export function workspaceMetadataToOTelAttributes(
57
metadata?: WorkspaceOTelMetadata,
58
): Record<string, string> {
59
if (!metadata) {
60
return {};
61
}
62
const attrs: Record<string, string> = {};
63
if (metadata.headBranchName) {
64
attrs[CopilotChatAttr.REPO_HEAD_BRANCH_NAME] = metadata.headBranchName;
65
}
66
if (metadata.headCommitHash) {
67
attrs[CopilotChatAttr.REPO_HEAD_COMMIT_HASH] = metadata.headCommitHash;
68
}
69
if (metadata.remoteUrl) {
70
attrs[CopilotChatAttr.REPO_REMOTE_URL] = metadata.remoteUrl;
71
}
72
if (metadata.fileRelativePath) {
73
attrs[CopilotChatAttr.FILE_RELATIVE_PATH] = metadata.fileRelativePath;
74
}
75
return attrs;
76
}
77
78