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

[tracing] Tracing via custom pipeline policy #31157

Closed
wants to merge 7 commits into from

Conversation

maorleger
Copy link
Member

@maorleger maorleger commented Sep 18, 2024

very rough around the edges, but this is a proof of concept that can hopefully help you get started. I will add comments explaining what I plan to add in a separate PR but hopefully you can use the logic in ai-inference to get going.

FWIW I left as much of your original code unchanged so it is easier for you to understand - feel free to ask me if you'd like me to give it a thorough review

Based off of #30800

@github-actions github-actions bot added Azure.Core OpenTelemetryInstrumentation test-utils Label for the issues related to the @azure/test-utils package labels Sep 18, 2024
@azure-sdk
Copy link
Collaborator

azure-sdk commented Sep 18, 2024

API change check

API changes are not detected in this pull request.

Copy link
Member Author

@maorleger maorleger left a comment

Choose a reason for hiding this comment

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

Left some comments for the reader - feel free to ask if you have questions

},
"devDependencies": {
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
Copy link
Member Author

Choose a reason for hiding this comment

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

enable prettier support in VSCode

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export const SDK_VERSION: string = "1.0.0-beta.2";
Copy link
Member Author

Choose a reason for hiding this comment

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

You'll need to add this to the constantPaths entry in package.json to allow the azure sdk bot to automatically bump the version here

/**
* Copied over verbatim from core-tracing
*/
function traceAsync<
Copy link
Member Author

Choose a reason for hiding this comment

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

Probably not needed unless you plan to use it

namespace: "Microsoft.CognitiveServices",
packageName: "ai-inference-rest",
}),
traceAsync,
Copy link
Member Author

Choose a reason for hiding this comment

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

Augmenting the core-level client. If you remove the traceAsync option you'll be able to just say const traceClient = createTracingClient({ .... }) etc

});
}

export const tracingPolicy: PipelinePolicy = {
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's your custom policy - you now have access to:

  1. The pipeline request
  2. The pipeline response (if you await next() )
  3. The tracing client
  4. Any package specific details

Change it however you'd like. The downside is that you're now working at a lower level so you only have the raw request body / headers / etc. You'll need to do some work to get it to work for your scenario but everything should be available

// }
const operationName = getOperationName(url.pathname);
const name = `${operationName}`.trim();
const { span, updatedOptions } = traceClient.startSpan(name, {
Copy link
Member Author

Choose a reason for hiding this comment

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

Consider using traceClient.withSpan if you can avoid the onStartTracing and onEndTracing calls or call them at the right time. It does a lot of this for you.

tracingOptions: pipelineRequest.tracingOptions,
});
pipelineRequest.tracingOptions = updatedOptions.tracingOptions;
onStartTracing?.(span, [{ ...pipelineRequest, headers: {} }, name, pipelineRequest.url]);
Copy link
Member Author

Choose a reason for hiding this comment

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

headers need to be updated here since the core-rest-pipeline headers are incompatible with the core-client-rest headers. You'll just need to update some of the types (here and everywhere else I am ignoring headers for the proof-of-concept)

onStartTracing?.(span, [{ ...pipelineRequest, headers: {} }, name, pipelineRequest.url]);

return traceClient
.withContext(updatedOptions.tracingOptions.tracingContext, () => next(pipelineRequest))
Copy link
Member Author

@maorleger maorleger Sep 18, 2024

Choose a reason for hiding this comment

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

strongly recommend using async/await here instead of chaining - I left it as close as possible to the original code to avoid confusion.

assert.isNotNull(client.pipeline);
});

it.only("tracing should work", async function () {
Copy link
Member Author

Choose a reason for hiding this comment

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

Only kept the basic test here, once you "productionalize" this code you can fix the tests to your liking

@@ -90,6 +90,7 @@ export interface TracingContext {

// @public
export interface TracingSpan {
addEvent(name: string, attributes?: Record<string, unknown>, startTime?: Date): void;
Copy link
Member Author

Choose a reason for hiding this comment

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

I will add this and other core-tracing changes separately - but I may need some time to work out the right API shape

@maorleger
Copy link
Member Author

Closing in favor of #31160

@maorleger maorleger closed this Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Core OpenTelemetryInstrumentation test-utils Label for the issues related to the @azure/test-utils package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants