Skip to content

Commit bdeb7de

Browse files
pelikhanCopilotgithub-actions[bot]Copilotgh-aw-bot
authored
Add hostname-only BYOK endpoint attribution to OTel spans (#80516)
* Initial plan * Record hostname-only Copilot BYOK endpoint attribution in run telemetry Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> * Use checked YAML writer for cleanup telemetry metadata Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> * docs: document BYOK endpoint attribution Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Preserve BYOK endpoint metadata in workflow locks Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> * Fix BYOK endpoint attribution review feedback Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> * Keep checked YAML formatting for telemetry env Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --------- Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>
1 parent 46e5169 commit bdeb7de

15 files changed

Lines changed: 254 additions & 5 deletions

‎.github/workflows/daily-byok-ollama-test.lock.yml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.github/workflows/smoke-copilot-aoai-apikey.lock.yml‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.github/workflows/smoke-copilot-aoai-entra.lock.yml‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// @ts-check
2+
3+
const { isIP } = require("net");
4+
5+
/**
6+
* Retain only an inference endpoint's hostname for telemetry. Never publish
7+
* URL credentials, paths, queries, or fragments, even when parsing fails.
8+
* @param {unknown} endpoint
9+
* @returns {string}
10+
*/
11+
function genAiServerAddress(endpoint) {
12+
if (typeof endpoint !== "string" || !endpoint.trim() || endpoint.includes("${{")) return "";
13+
try {
14+
const value = endpoint.trim();
15+
const normalizeIP = host => {
16+
const unbracketed = host.replace(/^\[|\]$/g, "");
17+
if (isIP(unbracketed) === 4) return unbracketed;
18+
const address = unbracketed.replace(/%(?:25)?[a-z0-9_.~-]+$/i, "");
19+
return isIP(address) === 6 ? address : "";
20+
};
21+
const literalIP = normalizeIP(value);
22+
if (literalIP) return literalIP;
23+
24+
const hasScheme = value.includes("://");
25+
if (!hasScheme && /[/?#@\\\s]/.test(value)) return "";
26+
if (hasScheme && !/^[a-z][a-z0-9+.-]*:\/\/[^/?#\\]+/i.test(value)) return "";
27+
const url = new URL(hasScheme ? value : `https://${value}`);
28+
if (url.protocol !== "https:" && url.protocol !== "http:") return "";
29+
const hostname = normalizeIP(url.hostname);
30+
if (hostname) return hostname;
31+
if (!hasScheme) {
32+
const host = url.hostname.toLowerCase();
33+
const hasPort = /:\d+$/.test(value) || /\]:\d+$/.test(value);
34+
if (["http", "https", "file", "ftp", "ftps", "ws", "wss", "data", "javascript", "mailto"].includes(host)) return "";
35+
if (host !== "localhost" && !host.includes(".") && !hasPort) return "";
36+
}
37+
return url.hostname;
38+
} catch {
39+
return "";
40+
}
41+
}
42+
43+
module.exports = { genAiServerAddress };
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
const { genAiServerAddress } = require("./genai_endpoint.cjs");
4+
5+
describe("genAiServerAddress", () => {
6+
it.each([
7+
["https://example.ai.gateway.azure.com/default/models/openai/v1", "example.ai.gateway.azure.com"],
8+
["https://gw.azure-api.net:8443/v1?key=private#fragment", "gw.azure-api.net"],
9+
["http://localhost:11434/v1", "localhost"],
10+
["api.openai.com:8443", "api.openai.com"],
11+
["https://[2001:db8::1]:8443/v1", "2001:db8::1"],
12+
["2001:db8::1", "2001:db8::1"],
13+
["fe80::1%eth0", "fe80::1"],
14+
["${{ secrets.ENDPOINT }}", ""],
15+
["/private/endpoint", ""],
16+
["file:/private/endpoint", ""],
17+
["file:///private/endpoint", ""],
18+
["https:/gw.example.com/v1", ""],
19+
["mytoken/v1", ""],
20+
["https:///sk-SECRET/v1", ""],
21+
["https:/sk-SECRET/v1", ""],
22+
["https:443", ""],
23+
["ftp://gw.example.com/v1", ""],
24+
["https://", ""],
25+
["not a url", ""],
26+
["", ""],
27+
[undefined, ""],
28+
[{ hostname: "example.com" }, ""],
29+
])("extracts a safe hostname from %s", (endpoint, expected) => {
30+
expect(genAiServerAddress(endpoint)).toBe(expected);
31+
});
32+
33+
it("strips URL user information", () => {
34+
const endpoint = new URL("https://gw.azure-api.net/v1");
35+
endpoint.username = "user";
36+
endpoint.password = "placeholder";
37+
expect(genAiServerAddress(endpoint.href)).toBe("gw.azure-api.net");
38+
});
39+
});

‎actions/setup/js/generate_aw_info.cjs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { validateContextVariables } = require("./validate_context_variables.cjs")
99
const validateLockdownRequirements = require("./validate_lockdown_requirements.cjs");
1010
const { writeMergedModelsJSON } = require("./merge_frontmatter_models.cjs");
1111
const { getErrorMessage } = require("./error_helpers.cjs");
12+
const { genAiServerAddress } = require("./genai_endpoint.cjs");
1213
const { ERR_CONFIG, ERR_SYSTEM } = require("./error_codes.cjs");
1314
const { buildWorkflowCallId, normalizeLegacyWorkQueueContext, normalizeWorkQueueContext, resolveWorkQueueRuntime, readInboundAwContext } = require("./aw_context.cjs");
1415
const { normalizeWebhookContext } = require("./webhook_context.cjs");
@@ -159,6 +160,8 @@ async function main(core, ctx, githubClient) {
159160
const genAiProvider = (process.env.GH_AW_INFO_GENAI_PROVIDER || "").trim();
160161
if (genAiProvider) {
161162
awInfo.genai_provider = genAiProvider;
163+
const serverAddress = genAiServerAddress(process.env.GH_AW_INFO_GENAI_ENDPOINT);
164+
if (serverAddress) awInfo.genai_server_address = serverAddress;
162165
}
163166

164167
const frontmatterSource = process.env.GH_AW_INFO_FRONTMATTER_SOURCE || "";

‎actions/setup/js/generate_aw_info.test.cjs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,21 @@ describe("generate_aw_info.cjs", () => {
161161
});
162162

163163
it("records the BYOK GenAI provider only when configured", async () => {
164+
const endpoint = new URL("https://gw.azure-api.net:8443/v1?key=private#fragment");
165+
endpoint.username = "user";
166+
endpoint.password = "placeholder";
167+
process.env.GH_AW_INFO_GENAI_ENDPOINT = endpoint.href;
164168
await main(mockCore, mockContext);
165169
expect(JSON.parse(fs.readFileSync(awInfoPath, "utf8"))).not.toHaveProperty("genai_provider");
170+
expect(JSON.parse(fs.readFileSync(awInfoPath, "utf8"))).not.toHaveProperty("genai_server_address");
166171

167172
process.env.GH_AW_INFO_GENAI_PROVIDER = "azure.ai.openai";
168173
await main(mockCore, mockContext);
169-
expect(JSON.parse(fs.readFileSync(awInfoPath, "utf8")).genai_provider).toBe("azure.ai.openai");
174+
const contents = fs.readFileSync(awInfoPath, "utf8");
175+
expect(JSON.parse(contents).genai_provider).toBe("azure.ai.openai");
176+
expect(JSON.parse(contents).genai_server_address).toBe("gw.azure-api.net");
177+
expect(contents).not.toContain("placeholder");
178+
expect(contents).not.toContain("key=private");
170179
});
171180

172181
it("should write aw_info.json with values from env vars and context", async () => {

‎actions/setup/js/send_otlp_span.cjs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const { isDurableSessionRecord } = require("./durable_session_record.cjs");
2121
const { readEvalsNotEvaluatedStatus } = require("./evals_status.cjs");
2222
const { MAX_OTLP_RESPONSE_BYTES, readOTLPResponse, inspectOTLPResponse, unavailableOTLPResponse } = require("./otlp_response.cjs");
2323
const { buildTraceparent, buildServiceRequestHeaders, isValidTraceId, isValidSpanId } = require("./service_correlation.cjs");
24+
const { genAiServerAddress } = require("./genai_endpoint.cjs");
2425
const {
2526
OTEL_ATTR_CICD_PIPELINE_NAME,
2627
OTEL_ATTR_CICD_PIPELINE_RUN_ID,
@@ -118,6 +119,13 @@ function resolveRunGenAiProvider(awInfo, engineId) {
118119
return { provider: resolveGenAiProvider(engineId), system: ENGINE_TO_SYSTEM_MAP[engineId] || engineId };
119120
}
120121

122+
/** @param {any} awInfo */
123+
function resolveRunGenAiServerAddress(awInfo) {
124+
const byokProvider = readContextString(awInfo?.genai_provider) || (process.env.GH_AW_INFO_GENAI_PROVIDER || "").trim();
125+
if (!byokProvider) return "";
126+
return genAiServerAddress(awInfo?.genai_server_address) || genAiServerAddress(process.env.GH_AW_INFO_GENAI_ENDPOINT);
127+
}
128+
121129
/** True when OTEL_SEMCONV_STABILITY_OPT_IN contains gen_ai_v1 (spec §6.7). */
122130
function isGenAiV1() {
123131
return (process.env.OTEL_SEMCONV_STABILITY_OPT_IN || "")
@@ -1665,6 +1673,8 @@ async function sendJobSetupSpan(options = {}) {
16651673
attributes.push(buildAttr("gen_ai.system", genAi.system));
16661674
}
16671675
attributes.push(buildAttr(OTEL_ATTR_GEN_AI_PROVIDER_NAME, genAi.provider));
1676+
const serverAddress = resolveRunGenAiServerAddress(awInfo);
1677+
if (serverAddress) attributes.push(buildAttr("server.address", serverAddress));
16681678
attributes.push(buildAttr(OTEL_ATTR_ENGINE_ID, engineId));
16691679
}
16701680
if (eventName) {
@@ -2583,6 +2593,8 @@ async function sendJobConclusionSpan(spanName, options = {}) {
25832593
const genAi = resolveRunGenAiProvider(awInfo, engineId);
25842594
if (!optIn) attributes.push(buildAttr("gen_ai.system", genAi.system));
25852595
attributes.push(buildAttr(OTEL_ATTR_GEN_AI_PROVIDER_NAME, genAi.provider));
2596+
const serverAddress = resolveRunGenAiServerAddress(awInfo);
2597+
if (serverAddress) attributes.push(buildAttr("server.address", serverAddress));
25862598
attributes.push(buildAttr(OTEL_ATTR_ENGINE_ID, engineId));
25872599
}
25882600
if (model) attributes.push(buildAttr("gen_ai.request.model", model));

0 commit comments

Comments
 (0)