From 1cfb58193703c5dd5192935e9dd0d638199430f3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 4 Mar 2026 18:31:32 +0000 Subject: [PATCH 1/6] Initial plan From cac1bc0c88890aef6f8a275b77df0bb143746bf6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 4 Mar 2026 19:23:58 +0000 Subject: [PATCH 2/6] feat: add generate-method-instrumentation flag for ActivitySource-based distributed tracing in client methods Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> --- .../http-client-csharp/emitter/src/options.ts | 8 +++++++ .../src/Providers/ClientProvider.cs | 24 +++++++++++++++++++ .../Providers/ScmMethodProviderCollection.cs | 14 +++++++++++ .../test/TestHelpers/Configuration.json | 3 ++- .../test/TestHelpers/MockHelpers.cs | 2 +- .../src/Configuration.cs | 15 ++++++++++-- 6 files changed, 62 insertions(+), 4 deletions(-) diff --git a/packages/http-client-csharp/emitter/src/options.ts b/packages/http-client-csharp/emitter/src/options.ts index 42196440cac..8296b796755 100644 --- a/packages/http-client-csharp/emitter/src/options.ts +++ b/packages/http-client-csharp/emitter/src/options.ts @@ -24,6 +24,7 @@ export interface CSharpEmitterOptions { "sdk-context-options"?: CreateSdkContextOptions; "generate-protocol-methods"?: boolean; "generate-convenience-methods"?: boolean; + "generate-method-instrumentation"?: boolean; "package-name"?: string; license?: { name: string; @@ -61,6 +62,12 @@ export const CSharpEmitterOptionsSchema: JSONSchemaType = description: "Set to `false` to skip generation of convenience methods. The default value is `true`.", }, + "generate-method-instrumentation": { + type: "boolean", + nullable: true, + description: + "Set to `false` to disable generation of ActivitySource-based distributed tracing instrumentation in client methods. The default value is `true`.", + }, "unreferenced-types-handling": { type: "string", enum: ["removeOrInternalize", "internalize", "keepAll"], @@ -156,6 +163,7 @@ export const defaultOptions = { "save-inputs": false, "generate-protocol-methods": true, "generate-convenience-methods": true, + "generate-method-instrumentation": true, "package-name": undefined, debug: undefined, logLevel: LoggerLevel.INFO, diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientProvider.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientProvider.cs index 606a888af4c..d9e15213a1e 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientProvider.cs @@ -4,6 +4,7 @@ using System; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Threading; @@ -59,6 +60,7 @@ private record ApiVersionFields(FieldProvider Field, PropertyProvider? Correspon private Dictionary? _methodCache; private Dictionary MethodCache => _methodCache ??= []; private TypeProvider? _backCompatProvider; + private FieldProvider? _activitySourceField; /// /// Gets the effective type provider to use for backward compatibility checks. @@ -366,6 +368,22 @@ private IReadOnlyList GetClientParameters() public PropertyProvider PipelineProperty { get; } public FieldProvider EndpointField { get; } + /// + /// Gets the ActivitySource field used for distributed tracing instrumentation. + /// Returns null if method instrumentation is disabled or if this is a sub-client. + /// + internal FieldProvider? ActivitySourceField => ScmCodeModelGenerator.Instance.Configuration.GenerateMethodInstrumentation && ClientOptions != null + ? _activitySourceField ??= BuildActivitySourceField() + : null; + + private FieldProvider BuildActivitySourceField() + => new FieldProvider( + FieldModifiers.Private | FieldModifiers.Static | FieldModifiers.ReadOnly, + typeof(ActivitySource), + "_activitySource", + this, + initializationValue: New.Instance(typeof(ActivitySource), Literal(ScmCodeModelGenerator.Instance.Configuration.PackageName))); + public IReadOnlyList SubClients => _subClients.Value; protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", $"{Name}.cs"); @@ -376,6 +394,12 @@ protected override FieldProvider[] BuildFields() { List fields = [EndpointField]; + // Add ActivitySource field for distributed tracing instrumentation (only for root clients) + if (ActivitySourceField != null) + { + fields.Add(ActivitySourceField); + } + if (_apiKeyAuthFields != null) { fields.Add(_apiKeyAuthFields.AuthField); diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs index 6879f6c7b7f..c70c11e4880 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs @@ -5,6 +5,7 @@ using System.ClientModel.Primitives; using System.Collections; using System.Collections.Generic; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; @@ -165,6 +166,19 @@ .. GetStackVariablesForReturnValueConversion(result, responseBodyType, isAsync, ]; } + // Prepend activity instrumentation statement if enabled + if (Client.ActivitySourceField != null) + { + var activityStatement = UsingDeclare( + "activity", + new CSharpType(typeof(Activity), isNullable: true), + Client.ActivitySourceField.Invoke( + nameof(ActivitySource.StartActivity), + [Literal($"{Client.Name}.{ServiceMethod.Name}"), FrameworkEnumValue(ActivityKind.Client)]), + out _); + methodBody = [activityStatement, .. methodBody]; + } + var convenienceMethod = new ScmMethodProvider(methodSignature, methodBody, EnclosingType, ScmMethodKind.Convenience, collectionDefinition: collection, serviceMethod: ServiceMethod); if (convenienceMethod.XmlDocs != null) diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/Configuration.json b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/Configuration.json index a0867eb37e3..d47eaf9a7c4 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/Configuration.json +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/Configuration.json @@ -5,5 +5,6 @@ "unknown-bool-property": false, "package-name": "SampleLibrary", "unknown-string-property": "unknownPropertyValue", - "disable-xml-docs": true + "disable-xml-docs": true, + "generate-method-instrumentation": false } diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/MockHelpers.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/MockHelpers.cs index 9148f659e43..08dc4e30676 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/MockHelpers.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/MockHelpers.cs @@ -148,7 +148,7 @@ public static Mock LoadMockGenerator( var loadMethod = typeof(Configuration).GetMethod("Load", BindingFlags.Static | BindingFlags.NonPublic); if (includeXmlDocs) { - configuration = "{\"disable-xml-docs\": false, \"package-name\": \"Sample.Namespace\"}"; + configuration = "{\"disable-xml-docs\": false, \"package-name\": \"Sample.Namespace\", \"generate-method-instrumentation\": false}"; } object?[] parameters = [_configFilePath, configuration]; var config = loadMethod?.Invoke(null, parameters); diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Configuration.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Configuration.cs index 98169440b6f..426b0229db2 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Configuration.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Configuration.cs @@ -35,7 +35,8 @@ public Configuration( string packageName, bool disableXmlDocs, UnreferencedTypesHandlingOption unreferencedTypesHandling, - LicenseInfo? licenseInfo) + LicenseInfo? licenseInfo, + bool generateMethodInstrumentation = true) { OutputDirectory = outputPath; AdditionalConfigurationOptions = additionalConfigurationOptions; @@ -43,6 +44,7 @@ public Configuration( DisableXmlDocs = disableXmlDocs; UnreferencedTypesHandling = unreferencedTypesHandling; LicenseInfo = licenseInfo; + GenerateMethodInstrumentation = generateMethodInstrumentation; } /// @@ -53,6 +55,7 @@ private static class Options public const string PackageName = "package-name"; public const string DisableXmlDocs = "disable-xml-docs"; public const string UnreferencedTypesHandling = "unreferenced-types-handling"; + public const string GenerateMethodInstrumentation = "generate-method-instrumentation"; } /// @@ -60,6 +63,11 @@ private static class Options /// public bool DisableXmlDocs { get; } + /// + /// Gets whether ActivitySource-based distributed tracing instrumentation should be generated in client methods. + /// + public bool GenerateMethodInstrumentation { get; } + /// /// Gets the root output directory for the generated library. /// @@ -123,7 +131,8 @@ internal static Configuration Load(string outputPath, string? json = null) ReadRequiredStringOption(root, Options.PackageName), ReadOption(root, Options.DisableXmlDocs), ReadEnumOption(root, Options.UnreferencedTypesHandling), - ReadLicenseInfo(root)); + ReadLicenseInfo(root), + ReadOption(root, Options.GenerateMethodInstrumentation)); } private static LicenseInfo? ReadLicenseInfo(JsonElement root) @@ -154,6 +163,7 @@ internal static Configuration Load(string outputPath, string? json = null) private static readonly Dictionary _defaultBoolOptionValues = new() { { Options.DisableXmlDocs, false }, + { Options.GenerateMethodInstrumentation, true }, }; /// @@ -164,6 +174,7 @@ internal static Configuration Load(string outputPath, string? json = null) Options.PackageName, Options.DisableXmlDocs, Options.UnreferencedTypesHandling, + Options.GenerateMethodInstrumentation, }; private static bool ReadOption(JsonElement root, string option) From 7b53cd062eedc4660d184e4c32eb4447ae8d4604 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 4 Mar 2026 20:07:05 +0000 Subject: [PATCH 3/6] regen: run Generate.ps1 to update generated test projects with method instrumentation Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> --- .../Local/Sample-TypeSpec/Configuration.json | 1 + .../src/Generated/SampleTypeSpecClient.cs | 54 +++++++++++++++++++ .../authentication/api-key/Configuration.json | 1 + .../http/custom/Configuration.json | 1 + .../authentication/oauth2/Configuration.json | 1 + .../authentication/union/Configuration.json | 1 + .../client-operation-group/Configuration.json | 1 + .../structure/default/Configuration.json | 1 + .../structure/multi-client/Configuration.json | 1 + .../renamed-operation/Configuration.json | 1 + .../two-operation-group/Configuration.json | 1 + .../http/documentation/Configuration.json | 1 + .../http/encode/array/Configuration.json | 1 + .../http/encode/bytes/Configuration.json | 1 + .../http/encode/datetime/Configuration.json | 1 + .../http/encode/duration/Configuration.json | 1 + .../http/encode/numeric/Configuration.json | 1 + .../http/parameters/basic/Configuration.json | 1 + .../body-optionality/Configuration.json | 1 + .../collection-format/Configuration.json | 1 + .../http/parameters/path/Configuration.json | 1 + .../http/parameters/query/Configuration.json | 1 + .../http/parameters/spread/Configuration.json | 1 + .../content-negotiation/Configuration.json | 1 + .../json-merge-patch/Configuration.json | 1 + .../payload/media-type/Configuration.json | 1 + .../http/payload/multipart/Configuration.json | 1 + .../http/payload/pageable/Configuration.json | 1 + .../http/payload/xml/Configuration.json | 1 + .../srv-driven/v1/Configuration.json | 1 + .../srv-driven/v2/Configuration.json | 1 + .../status-code-range/Configuration.json | 1 + .../Spector/http/routes/Configuration.json | 1 + .../encoded-name/json/Configuration.json | 1 + .../endpoint/not-defined/Configuration.json | 1 + .../server/path/multiple/Configuration.json | 1 + .../server/path/single/Configuration.json | 1 + .../versions/not-versioned/Configuration.json | 1 + .../versions/versioned/Configuration.json | 1 + .../conditional-request/Configuration.json | 1 + .../repeatability/Configuration.json | 1 + .../http/special-words/Configuration.json | 1 + .../http/type/array/Configuration.json | 1 + .../http/type/dictionary/Configuration.json | 1 + .../type/enum/extensible/Configuration.json | 1 + .../http/type/enum/fixed/Configuration.json | 1 + .../http/type/model/empty/Configuration.json | 1 + .../enum-discriminator/Configuration.json | 1 + .../nested-discriminator/Configuration.json | 1 + .../not-discriminated/Configuration.json | 1 + .../inheritance/recursive/Configuration.json | 1 + .../single-discriminator/Configuration.json | 1 + .../http/type/model/usage/Configuration.json | 1 + .../type/model/visibility/Configuration.json | 1 + .../additional-properties/Configuration.json | 1 + .../type/property/nullable/Configuration.json | 1 + .../property/optionality/Configuration.json | 1 + .../property/value-types/Configuration.json | 1 + .../http/type/scalar/Configuration.json | 1 + .../http/type/union/Configuration.json | 1 + .../versioning/added/v1/Configuration.json | 1 + .../versioning/added/v2/Configuration.json | 1 + .../madeOptional/v1/Configuration.json | 1 + .../madeOptional/v2/Configuration.json | 1 + .../versioning/removed/v1/Configuration.json | 1 + .../versioning/removed/v2/Configuration.json | 1 + .../removed/v2Preview/Configuration.json | 1 + .../renamedFrom/v1/Configuration.json | 1 + .../renamedFrom/v2/Configuration.json | 1 + .../v1/Configuration.json | 1 + .../v2/Configuration.json | 1 + .../typeChangedFrom/v1/Configuration.json | 1 + .../typeChangedFrom/v2/Configuration.json | 1 + 73 files changed, 126 insertions(+) diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/Configuration.json index 5444bcf8177..c8151fbc4bb 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/Configuration.json @@ -1,4 +1,5 @@ { + "generate-method-instrumentation": true, "package-name": "SampleTypeSpec", "license": { "name": "MIT License", diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs index f2e56c36cb0..1a4d8093b78 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs @@ -9,6 +9,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -20,6 +21,7 @@ namespace SampleTypeSpec public partial class SampleTypeSpecClient { private readonly Uri _endpoint; + private static readonly ActivitySource _activitySource = new ActivitySource("SampleTypeSpec"); /// A credential used to authenticate to the service. private readonly ApiKeyCredential _keyCredential; private const string AuthorizationHeader = "my-api-key"; @@ -157,6 +159,7 @@ public virtual ClientResult SayHi(string headParameter, string queryParam Argument.AssertNotNullOrEmpty(headParameter, nameof(headParameter)); Argument.AssertNotNullOrEmpty(queryParameter, nameof(queryParameter)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.SayHi", ActivityKind.Client); ClientResult result = SayHi(headParameter, queryParameter, optionalQuery, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -174,6 +177,7 @@ public virtual async Task> SayHiAsync(string headParameter, Argument.AssertNotNullOrEmpty(headParameter, nameof(headParameter)); Argument.AssertNotNullOrEmpty(queryParameter, nameof(queryParameter)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.SayHi", ActivityKind.Client); ClientResult result = await SayHiAsync(headParameter, queryParameter, optionalQuery, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -244,6 +248,7 @@ public virtual ClientResult HelloAgain(string p2, string p1, Rou Argument.AssertNotNullOrEmpty(p1, nameof(p1)); Argument.AssertNotNull(action, nameof(action)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloAgain", ActivityKind.Client); ClientResult result = HelloAgain(p2, p1, action, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); } @@ -262,6 +267,7 @@ public virtual async Task> HelloAgainAsync(string p Argument.AssertNotNullOrEmpty(p1, nameof(p1)); Argument.AssertNotNull(action, nameof(action)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloAgain", ActivityKind.Client); ClientResult result = await HelloAgainAsync(p2, p1, action, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); } @@ -327,6 +333,7 @@ public virtual ClientResult NoContentType(Wrapper info, Cancella { Argument.AssertNotNull(info, nameof(info)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.NoContentType", ActivityKind.Client); ClientResult result = NoContentType(info.P2, info.P1, info.Action, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); } @@ -340,6 +347,7 @@ public virtual async Task> NoContentTypeAsync(Wrapp { Argument.AssertNotNull(info, nameof(info)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.NoContentType", ActivityKind.Client); ClientResult result = await NoContentTypeAsync(info.P2, info.P1, info.Action, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); } @@ -383,6 +391,7 @@ public virtual async Task HelloDemo2Async(RequestOptions options) /// Service returned a non-success status code. public virtual ClientResult HelloDemo2(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloDemo2", ActivityKind.Client); ClientResult result = HelloDemo2(cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -392,6 +401,7 @@ public virtual ClientResult HelloDemo2(CancellationToken cancellationToke /// Service returned a non-success status code. public virtual async Task> HelloDemo2Async(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloDemo2", ActivityKind.Client); ClientResult result = await HelloDemo2Async(cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -447,6 +457,7 @@ public virtual ClientResult CreateLiteral(Thing body, CancellationToken c { Argument.AssertNotNull(body, nameof(body)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.CreateLiteral", ActivityKind.Client); ClientResult result = CreateLiteral(body, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -460,6 +471,7 @@ public virtual async Task> CreateLiteralAsync(Thing body, Ca { Argument.AssertNotNull(body, nameof(body)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.CreateLiteral", ActivityKind.Client); ClientResult result = await CreateLiteralAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -503,6 +515,7 @@ public virtual async Task HelloLiteralAsync(RequestOptions options /// Service returned a non-success status code. public virtual ClientResult HelloLiteral(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloLiteral", ActivityKind.Client); ClientResult result = HelloLiteral(cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -512,6 +525,7 @@ public virtual ClientResult HelloLiteral(CancellationToken cancellationTo /// Service returned a non-success status code. public virtual async Task> HelloLiteralAsync(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloLiteral", ActivityKind.Client); ClientResult result = await HelloLiteralAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -558,6 +572,7 @@ public virtual async Task TopActionAsync(DateTimeOffset action, Re /// Service returned a non-success status code. public virtual ClientResult TopAction(DateTimeOffset action, CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.TopAction", ActivityKind.Client); ClientResult result = TopAction(action, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -568,6 +583,7 @@ public virtual ClientResult TopAction(DateTimeOffset action, Cancellation /// Service returned a non-success status code. public virtual async Task> TopActionAsync(DateTimeOffset action, CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.TopAction", ActivityKind.Client); ClientResult result = await TopActionAsync(action, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -718,6 +734,7 @@ public virtual ClientResult AnonymousBody(string name, BinaryData require Argument.AssertNotNullOrEmpty(requiredBadDescription, nameof(requiredBadDescription)); Argument.AssertNotNullOrEmpty(propertyWithSpecialDocs, nameof(propertyWithSpecialDocs)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.AnonymousBody", ActivityKind.Client); Thing spreadModel = new Thing( default, requiredUnion, @@ -769,6 +786,7 @@ public virtual async Task> AnonymousBodyAsync(string name, B Argument.AssertNotNullOrEmpty(requiredBadDescription, nameof(requiredBadDescription)); Argument.AssertNotNullOrEmpty(propertyWithSpecialDocs, nameof(propertyWithSpecialDocs)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.AnonymousBody", ActivityKind.Client); Thing spreadModel = new Thing( default, requiredUnion, @@ -844,6 +862,7 @@ public virtual ClientResult FriendlyModel(string name, CancellationToken { Argument.AssertNotNullOrEmpty(name, nameof(name)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.FriendlyModel", ActivityKind.Client); Friend spreadModel = new Friend(name, default); ClientResult result = FriendlyModel(spreadModel, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Friend)result, result.GetRawResponse()); @@ -859,6 +878,7 @@ public virtual async Task> FriendlyModelAsync(string name, { Argument.AssertNotNullOrEmpty(name, nameof(name)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.FriendlyModel", ActivityKind.Client); Friend spreadModel = new Friend(name, default); ClientResult result = await FriendlyModelAsync(spreadModel, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Friend)result, result.GetRawResponse()); @@ -903,6 +923,7 @@ public virtual async Task AddTimeHeaderAsync(RequestOptions option /// Service returned a non-success status code. public virtual ClientResult AddTimeHeader(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.AddTimeHeader", ActivityKind.Client); return AddTimeHeader(cancellationToken.ToRequestOptions()); } @@ -911,6 +932,7 @@ public virtual ClientResult AddTimeHeader(CancellationToken cancellationToken = /// Service returned a non-success status code. public virtual async Task AddTimeHeaderAsync(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.AddTimeHeader", ActivityKind.Client); return await AddTimeHeaderAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); } @@ -966,6 +988,7 @@ public virtual ClientResult ProjectedNameModel(string otherN { Argument.AssertNotNullOrEmpty(otherName, nameof(otherName)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.ProjectedNameModel", ActivityKind.Client); RenamedModelCustom spreadModel = new RenamedModelCustom(default, otherName); ClientResult result = ProjectedNameModel(spreadModel, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((RenamedModelCustom)result, result.GetRawResponse()); @@ -981,6 +1004,7 @@ public virtual async Task> ProjectedNameModelAs { Argument.AssertNotNullOrEmpty(otherName, nameof(otherName)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.ProjectedNameModel", ActivityKind.Client); RenamedModelCustom spreadModel = new RenamedModelCustom(default, otherName); ClientResult result = await ProjectedNameModelAsync(spreadModel, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((RenamedModelCustom)result, result.GetRawResponse()); @@ -1025,6 +1049,7 @@ public virtual async Task ReturnsAnonymousModelAsync(RequestOption /// Service returned a non-success status code. public virtual ClientResult ReturnsAnonymousModel(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.ReturnsAnonymousModel", ActivityKind.Client); ClientResult result = ReturnsAnonymousModel(cancellationToken.ToRequestOptions()); return ClientResult.FromValue((ReturnsAnonymousModelResponse)result, result.GetRawResponse()); } @@ -1034,6 +1059,7 @@ public virtual ClientResult ReturnsAnonymousModel /// Service returned a non-success status code. public virtual async Task> ReturnsAnonymousModelAsync(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.ReturnsAnonymousModel", ActivityKind.Client); ClientResult result = await ReturnsAnonymousModelAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((ReturnsAnonymousModelResponse)result, result.GetRawResponse()); } @@ -1092,6 +1118,7 @@ public virtual ClientResult GetUnknownValue(string accept, CancellationT { Argument.AssertNotNullOrEmpty(accept, nameof(accept)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetUnknownValue", ActivityKind.Client); ClientResult result = GetUnknownValue(accept, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(result.GetRawResponse().Content.ToString(), result.GetRawResponse()); } @@ -1106,6 +1133,7 @@ public virtual async Task> GetUnknownValueAsync(string acce { Argument.AssertNotNullOrEmpty(accept, nameof(accept)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetUnknownValue", ActivityKind.Client); ClientResult result = await GetUnknownValueAsync(accept, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(result.GetRawResponse().Content.ToString(), result.GetRawResponse()); } @@ -1161,6 +1189,7 @@ public virtual ClientResult InternalProtocol(Thing body, CancellationToke { Argument.AssertNotNull(body, nameof(body)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.InternalProtocol", ActivityKind.Client); ClientResult result = InternalProtocol(body, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -1174,6 +1203,7 @@ public virtual async Task> InternalProtocolAsync(Thing body, { Argument.AssertNotNull(body, nameof(body)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.InternalProtocol", ActivityKind.Client); ClientResult result = await InternalProtocolAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Thing)result, result.GetRawResponse()); } @@ -1217,6 +1247,7 @@ public virtual async Task StillConvenientAsync(RequestOptions opti /// Service returned a non-success status code. public virtual ClientResult StillConvenient(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.StillConvenient", ActivityKind.Client); return StillConvenient(cancellationToken.ToRequestOptions()); } @@ -1225,6 +1256,7 @@ public virtual ClientResult StillConvenient(CancellationToken cancellationToken /// Service returned a non-success status code. public virtual async Task StillConvenientAsync(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.StillConvenient", ActivityKind.Client); return await StillConvenientAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); } @@ -1282,6 +1314,7 @@ public virtual ClientResult HeadAsBoolean(string id, CancellationToken cancellat { Argument.AssertNotNullOrEmpty(id, nameof(id)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HeadAsBoolean", ActivityKind.Client); return HeadAsBoolean(id, cancellationToken.ToRequestOptions()); } @@ -1295,6 +1328,7 @@ public virtual async Task HeadAsBooleanAsync(string id, Cancellati { Argument.AssertNotNullOrEmpty(id, nameof(id)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HeadAsBoolean", ActivityKind.Client); return await HeadAsBooleanAsync(id, cancellationToken.ToRequestOptions()).ConfigureAwait(false); } @@ -1352,6 +1386,7 @@ public virtual ClientResult WithApiVersion(string p1, CancellationToken cancella { Argument.AssertNotNullOrEmpty(p1, nameof(p1)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.WithApiVersion", ActivityKind.Client); return WithApiVersion(p1, cancellationToken.ToRequestOptions()); } @@ -1365,6 +1400,7 @@ public virtual async Task WithApiVersionAsync(string p1, Cancellat { Argument.AssertNotNullOrEmpty(p1, nameof(p1)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.WithApiVersion", ActivityKind.Client); return await WithApiVersionAsync(p1, cancellationToken.ToRequestOptions()).ConfigureAwait(false); } @@ -1405,6 +1441,7 @@ public virtual AsyncCollectionResult GetWithNextLinkAsync(RequestOptions options /// Service returned a non-success status code. public virtual CollectionResult GetWithNextLink(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); return new SampleTypeSpecClientGetWithNextLinkCollectionResultOfT(this, cancellationToken.ToRequestOptions()); } @@ -1413,6 +1450,7 @@ public virtual CollectionResult GetWithNextLink(CancellationToken cancell /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithNextLinkAsync(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); return new SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions()); } @@ -1453,6 +1491,7 @@ public virtual AsyncCollectionResult GetWithStringNextLinkAsync(RequestOptions o /// Service returned a non-success status code. public virtual CollectionResult GetWithStringNextLink(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); return new SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT(this, cancellationToken.ToRequestOptions()); } @@ -1461,6 +1500,7 @@ public virtual CollectionResult GetWithStringNextLink(CancellationToken c /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithStringNextLinkAsync(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); return new SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions()); } @@ -1504,6 +1544,7 @@ public virtual AsyncCollectionResult GetWithContinuationTokenAsync(string token, /// Service returned a non-success status code. public virtual CollectionResult GetWithContinuationToken(string token = default, CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); return new SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT(this, token, cancellationToken.ToRequestOptions()); } @@ -1513,6 +1554,7 @@ public virtual CollectionResult GetWithContinuationToken(string token = d /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithContinuationTokenAsync(string token = default, CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); return new SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT(this, token, cancellationToken.ToRequestOptions()); } @@ -1556,6 +1598,7 @@ public virtual AsyncCollectionResult GetWithContinuationTokenHeaderResponseAsync /// Service returned a non-success status code. public virtual CollectionResult GetWithContinuationTokenHeaderResponse(string token = default, CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); return new SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT(this, token, cancellationToken.ToRequestOptions()); } @@ -1565,6 +1608,7 @@ public virtual CollectionResult GetWithContinuationTokenHeaderResponse(st /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithContinuationTokenHeaderResponseAsync(string token = default, CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); return new SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT(this, token, cancellationToken.ToRequestOptions()); } @@ -1605,6 +1649,7 @@ public virtual AsyncCollectionResult GetWithPagingAsync(RequestOptions options) /// Service returned a non-success status code. public virtual CollectionResult GetWithPaging(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); return new SampleTypeSpecClientGetWithPagingCollectionResultOfT(this, cancellationToken.ToRequestOptions()); } @@ -1613,6 +1658,7 @@ public virtual CollectionResult GetWithPaging(CancellationToken cancellat /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithPagingAsync(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); return new SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions()); } @@ -1681,6 +1727,7 @@ public virtual ClientResult EmbeddedParameters(ModelWithEmbeddedNonBodyParameter { Argument.AssertNotNull(body, nameof(body)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.EmbeddedParameters", ActivityKind.Client); return EmbeddedParameters(body.RequiredHeader, body.RequiredQuery, body, body.OptionalHeader, body.OptionalQuery, cancellationToken.ToRequestOptions()); } @@ -1693,6 +1740,7 @@ public virtual async Task EmbeddedParametersAsync(ModelWithEmbedde { Argument.AssertNotNull(body, nameof(body)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.EmbeddedParameters", ActivityKind.Client); return await EmbeddedParametersAsync(body.RequiredHeader, body.RequiredQuery, body, body.OptionalHeader, body.OptionalQuery, cancellationToken.ToRequestOptions()).ConfigureAwait(false); } @@ -1747,6 +1795,7 @@ public virtual ClientResult DynamicModelOperation(DynamicModel body, Cancellatio { Argument.AssertNotNull(body, nameof(body)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.DynamicModelOperation", ActivityKind.Client); return DynamicModelOperation(body, cancellationToken.ToRequestOptions()); } @@ -1759,6 +1808,7 @@ public virtual async Task DynamicModelOperationAsync(DynamicModel { Argument.AssertNotNull(body, nameof(body)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.DynamicModelOperation", ActivityKind.Client); return await DynamicModelOperationAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); } @@ -1801,6 +1851,7 @@ public virtual async Task GetXmlAdvancedModelAsync(RequestOptions /// Service returned a non-success status code. public virtual ClientResult GetXmlAdvancedModel(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetXmlAdvancedModel", ActivityKind.Client); ClientResult result = GetXmlAdvancedModel(cancellationToken.ToRequestOptions()); return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); } @@ -1810,6 +1861,7 @@ public virtual ClientResult GetXmlAdvancedModel(CancellationTo /// Service returned a non-success status code. public virtual async Task> GetXmlAdvancedModelAsync(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetXmlAdvancedModel", ActivityKind.Client); ClientResult result = await GetXmlAdvancedModelAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); } @@ -1865,6 +1917,7 @@ public virtual ClientResult UpdateXmlAdvancedModel(XmlAdvanced { Argument.AssertNotNull(body, nameof(body)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.UpdateXmlAdvancedModel", ActivityKind.Client); ClientResult result = UpdateXmlAdvancedModel(body, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); } @@ -1878,6 +1931,7 @@ public virtual async Task> UpdateXmlAdvancedModel { Argument.AssertNotNull(body, nameof(body)); + using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.UpdateXmlAdvancedModel", ActivityKind.Client); ClientResult result = await UpdateXmlAdvancedModelAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/api-key/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/api-key/Configuration.json index a44e714e395..035f2081f98 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/api-key/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/api-key/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Authentication.ApiKey" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/http/custom/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/http/custom/Configuration.json index 26b8aa910d3..cc063137721 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/http/custom/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/http/custom/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Authentication.Http.Custom" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/oauth2/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/oauth2/Configuration.json index b7899aea872..7777f2aab51 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/oauth2/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/oauth2/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Authentication.OAuth2" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/union/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/union/Configuration.json index 5e333f3ee24..c4c745fe2ff 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/union/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/authentication/union/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Authentication.Union" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/client-operation-group/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/client-operation-group/Configuration.json index 553878dcc41..fb5bcd90bd8 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/client-operation-group/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/client-operation-group/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Client.Structure.Service" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/default/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/default/Configuration.json index e75adbce7d4..e60df20b84c 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/default/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/default/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Client.Structure.Service.Default" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/multi-client/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/multi-client/Configuration.json index cd7567f13fd..1edcec08c28 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/multi-client/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/multi-client/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Client.Structure.Service.Multi.Client" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/renamed-operation/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/renamed-operation/Configuration.json index 2c423376a5d..fe5abb223d1 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/renamed-operation/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/renamed-operation/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Client.Structure.Service.Renamed.Operation" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/two-operation-group/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/two-operation-group/Configuration.json index 58a22dafa6e..da51b8cf774 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/two-operation-group/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/client/structure/two-operation-group/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Client.Structure.Service.TwoOperationGroup" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/documentation/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/documentation/Configuration.json index 3335831568c..5464bb22227 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/documentation/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/documentation/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Documentation" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/array/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/array/Configuration.json index 8bd0bb4f945..c03fc48bf93 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/array/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/array/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Encode.Array" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/bytes/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/bytes/Configuration.json index 48efce938b5..030f9410a0f 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/bytes/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/bytes/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Encode.Bytes" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/datetime/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/datetime/Configuration.json index 0a0a84d86e4..d14072a85f8 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/datetime/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/datetime/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Encode.Datetime" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/duration/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/duration/Configuration.json index e90188553c2..7b3d3e35934 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/duration/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/duration/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Encode.Duration" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/numeric/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/numeric/Configuration.json index e4685321069..f0e51ed86e3 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/numeric/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/encode/numeric/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Encode.Numeric" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/basic/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/basic/Configuration.json index 481cf25b9ee..04cdf49e82b 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/basic/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/basic/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Parameters.Basic" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/body-optionality/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/body-optionality/Configuration.json index 87417f1028e..5a36f3d4c5e 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/body-optionality/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/body-optionality/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Parameters.BodyOptionality" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/collection-format/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/collection-format/Configuration.json index 96a68122676..6467873cfb9 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/collection-format/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/collection-format/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Parameters.CollectionFormat" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/path/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/path/Configuration.json index 1c48fac1025..4d9295fbdb1 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/path/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/path/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Parameters.Path" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/query/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/query/Configuration.json index 18b95047fda..c05e4703f1e 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/query/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/query/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Parameters.Query" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/spread/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/spread/Configuration.json index 18cbb063e8d..5903e41fad9 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/spread/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/parameters/spread/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Parameters.Spread" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/content-negotiation/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/content-negotiation/Configuration.json index 804b2d9bd5f..66596f12ec6 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/content-negotiation/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/content-negotiation/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Payload.ContentNegotiation" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/json-merge-patch/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/json-merge-patch/Configuration.json index 4b0db821922..ee92289e3c3 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/json-merge-patch/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/json-merge-patch/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Payload.JsonMergePatch" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/media-type/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/media-type/Configuration.json index e5d0e20486a..2c5f6a2e7fc 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/media-type/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/media-type/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Payload.MediaType" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/multipart/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/multipart/Configuration.json index a4e60904404..ac9ded5e277 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/multipart/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/multipart/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Payload.MultiPart" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/pageable/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/pageable/Configuration.json index a8b33a617ab..84ed0d3246d 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/pageable/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/pageable/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Payload.Pageable" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/xml/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/xml/Configuration.json index a7354dd1925..060aab31f11 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/xml/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/payload/xml/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Payload.Xml" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/resiliency/srv-driven/v1/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/resiliency/srv-driven/v1/Configuration.json index e1c0feb0334..1e0f5c6091d 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/resiliency/srv-driven/v1/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/resiliency/srv-driven/v1/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Resiliency.SrvDriven.V1" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/resiliency/srv-driven/v2/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/resiliency/srv-driven/v2/Configuration.json index cda8e93495c..d7d10ec4356 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/resiliency/srv-driven/v2/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/resiliency/srv-driven/v2/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Resiliency.SrvDriven.V2" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/response/status-code-range/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/response/status-code-range/Configuration.json index 20947b8a356..5adc80dbebc 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/response/status-code-range/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/response/status-code-range/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Response.StatusCodeRange" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/routes/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/routes/Configuration.json index b2443847ee1..e84bc9b2548 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/routes/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/routes/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Routes" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/serialization/encoded-name/json/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/serialization/encoded-name/json/Configuration.json index 67ae85dad77..61a8da35054 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/serialization/encoded-name/json/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/serialization/encoded-name/json/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Serialization.EncodedName.Json" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/server/endpoint/not-defined/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/server/endpoint/not-defined/Configuration.json index ffea67465e2..b11b4a61bc1 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/server/endpoint/not-defined/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/server/endpoint/not-defined/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Server.Endpoint.NotDefined" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/server/path/multiple/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/server/path/multiple/Configuration.json index d4233efb232..d19ef11cc78 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/server/path/multiple/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/server/path/multiple/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Server.Path.Multiple" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/server/path/single/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/server/path/single/Configuration.json index ee6ea4b1942..5b0f161badd 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/server/path/single/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/server/path/single/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Server.Path.Single" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/server/versions/not-versioned/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/server/versions/not-versioned/Configuration.json index 85d581eec3e..2086b5a4fdc 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/server/versions/not-versioned/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/server/versions/not-versioned/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Server.Versions.NotVersioned" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/server/versions/versioned/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/server/versions/versioned/Configuration.json index 4e9a6472ce2..f651a51f2b8 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/server/versions/versioned/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/server/versions/versioned/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Server.Versions.Versioned" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/special-headers/conditional-request/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/special-headers/conditional-request/Configuration.json index 6c924699d48..540062f0ea4 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/special-headers/conditional-request/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/special-headers/conditional-request/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "SpecialHeaders.ConditionalRequest" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/special-headers/repeatability/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/special-headers/repeatability/Configuration.json index 04f07c86704..4b3effe7978 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/special-headers/repeatability/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/special-headers/repeatability/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "SpecialHeaders.Repeatability" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/special-words/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/special-words/Configuration.json index 86cff884aea..a797f380597 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/special-words/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/special-words/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "SpecialWords" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/array/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/array/Configuration.json index fd8654bd80d..d3622607708 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/array/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/array/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Array" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/dictionary/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/dictionary/Configuration.json index b86c2d49328..3258334efb0 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/dictionary/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/dictionary/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Dictionary" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/enum/extensible/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/enum/extensible/Configuration.json index 3f63e3ba1da..fa3bee416cf 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/enum/extensible/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/enum/extensible/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Enum.Extensible" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/enum/fixed/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/enum/fixed/Configuration.json index 6e0a90c0ca3..dade51b58a2 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/enum/fixed/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/enum/fixed/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Enum.Fixed" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/empty/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/empty/Configuration.json index 2869a238afc..b6e8a9de3ee 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/empty/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/empty/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Model.Empty" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/enum-discriminator/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/enum-discriminator/Configuration.json index 7d4e0762f65..1945dd8eeb8 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/enum-discriminator/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/enum-discriminator/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Model.Inheritance.EnumDiscriminator" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/nested-discriminator/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/nested-discriminator/Configuration.json index 1d4ccc8b5fe..23a818ba5f6 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/nested-discriminator/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/nested-discriminator/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Model.Inheritance.NestedDiscriminator" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/not-discriminated/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/not-discriminated/Configuration.json index 307a5dce4f5..c2c403b032b 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/not-discriminated/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/not-discriminated/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Model.Inheritance.NotDiscriminated" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/recursive/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/recursive/Configuration.json index dd38b6f0328..c06197b0740 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/recursive/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/recursive/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Model.Inheritance.Recursive" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/single-discriminator/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/single-discriminator/Configuration.json index 63b51a5d6fc..8301560150e 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/single-discriminator/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/inheritance/single-discriminator/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Model.Inheritance.SingleDiscriminator" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/usage/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/usage/Configuration.json index dcf477b29ba..48bb442cb24 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/usage/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/usage/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Model.Usage" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/visibility/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/visibility/Configuration.json index 098d67ccb5f..edae200108a 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/visibility/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/model/visibility/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Model.Visibility" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/additional-properties/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/additional-properties/Configuration.json index 3a64aca40cf..0afa0e9b064 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/additional-properties/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/additional-properties/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Property.AdditionalProperties" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/nullable/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/nullable/Configuration.json index 8843693bc40..67ee217b895 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/nullable/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/nullable/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Property.Nullable" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/optionality/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/optionality/Configuration.json index 4d9a2e48d18..5439be53b71 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/optionality/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/optionality/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Property.Optional" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/value-types/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/value-types/Configuration.json index d7ad85315d2..0d1aefa10ea 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/value-types/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/property/value-types/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Property.ValueTypes" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/scalar/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/scalar/Configuration.json index 14a986ecea4..b63810cc117 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/scalar/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/scalar/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Scalar" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/union/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/union/Configuration.json index a03ddf04c20..aafbee1fcee 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/type/union/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/type/union/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Type.Union" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/added/v1/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/added/v1/Configuration.json index fced31fd667..c51477a7f97 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/added/v1/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/added/v1/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.Added.V1" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/added/v2/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/added/v2/Configuration.json index 4ec3e56aec9..4c6b6f45523 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/added/v2/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/added/v2/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.Added.V2" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/madeOptional/v1/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/madeOptional/v1/Configuration.json index 1b156b851a2..2d4f72de89e 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/madeOptional/v1/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/madeOptional/v1/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.MadeOptional.V1" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/madeOptional/v2/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/madeOptional/v2/Configuration.json index 09874324060..18f0aa608f3 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/madeOptional/v2/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/madeOptional/v2/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.MadeOptional.V2" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v1/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v1/Configuration.json index bb3e37ca9a2..1a856443fd5 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v1/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v1/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.Removed.V1" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v2/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v2/Configuration.json index 936dc888824..1b803a6a366 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v2/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v2/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.Removed.V2" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v2Preview/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v2Preview/Configuration.json index b8a4b1ee2ec..747bbb4098a 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v2Preview/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/removed/v2Preview/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.Removed.V2Preview" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/renamedFrom/v1/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/renamedFrom/v1/Configuration.json index 01c11ac4101..b9452fcea46 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/renamedFrom/v1/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/renamedFrom/v1/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.RenamedFrom.V1" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/renamedFrom/v2/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/renamedFrom/v2/Configuration.json index fb28ade7416..a73919050e2 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/renamedFrom/v2/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/renamedFrom/v2/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.RenamedFrom.V2" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/returnTypeChangedFrom/v1/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/returnTypeChangedFrom/v1/Configuration.json index 40b00692dfc..428d3846bfa 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/returnTypeChangedFrom/v1/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/returnTypeChangedFrom/v1/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.ReturnTypeChangedFrom.V1" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/returnTypeChangedFrom/v2/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/returnTypeChangedFrom/v2/Configuration.json index f6801f05d79..d14631361c6 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/returnTypeChangedFrom/v2/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/returnTypeChangedFrom/v2/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.ReturnTypeChangedFrom.V2" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/typeChangedFrom/v1/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/typeChangedFrom/v1/Configuration.json index 3e3258c3e7e..17217ee2396 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/typeChangedFrom/v1/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/typeChangedFrom/v1/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.TypeChangedFrom.V1" } diff --git a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/typeChangedFrom/v2/Configuration.json b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/typeChangedFrom/v2/Configuration.json index 270c0344667..8629a8b0ee8 100644 --- a/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/typeChangedFrom/v2/Configuration.json +++ b/packages/http-client-csharp/generator/TestProjects/Spector/http/versioning/typeChangedFrom/v2/Configuration.json @@ -1,3 +1,4 @@ { + "generate-method-instrumentation": true, "package-name": "Versioning.TypeChangedFrom.V2" } From 1853adb2f14afd9bb113ea140bcd1a040df24bff Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 4 Mar 2026 20:39:53 +0000 Subject: [PATCH 4/6] Add tracing tests, fix sub-client ActivitySource, and improve paging tracing - Fix ActivitySourceField for sub-clients by removing ClientOptions != null check - Update CollectionResultDefinition to accept and dispose Activity in GetRawPages via try-finally - Update paging convenience methods to pass activity to CollectionResult (instead of using) - Add enableMethodInstrumentation param to MockHelpers.LoadMockGenerator - Add 5 tests: TracingIsAddedToConvenienceMethod, TracingIsNotAddedToConvenienceMethodWhenDisabled, TracingIsAddedToSubClientConvenienceMethod, TracingIsAddedToPagingConvenienceMethod, TracingInPagingCollectionDisposesActivityViaFinally - Regenerate Local Sample-TypeSpec test project with new tracing behavior Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> --- .../src/Providers/ClientProvider.cs | 4 +- .../Providers/CollectionResultDefinition.cs | 56 +++++- .../Providers/ScmMethodProviderCollection.cs | 42 +++-- .../ScmMethodProviderCollectionTests.cs | 161 ++++++++++++++++++ .../test/TestHelpers/MockHelpers.cs | 7 +- .../src/Generated/AnimalOperations.cs | 6 + ...hContinuationTokenAsyncCollectionResult.cs | 33 ++-- ...ntinuationTokenAsyncCollectionResultOfT.cs | 33 ++-- ...etWithContinuationTokenCollectionResult.cs | 33 ++-- ...ithContinuationTokenCollectionResultOfT.cs | 33 ++-- ...okenHeaderResponseAsyncCollectionResult.cs | 39 +++-- ...nHeaderResponseAsyncCollectionResultOfT.cs | 39 +++-- ...tionTokenHeaderResponseCollectionResult.cs | 39 +++-- ...nTokenHeaderResponseCollectionResultOfT.cs | 39 +++-- ...entGetWithNextLinkAsyncCollectionResult.cs | 33 ++-- ...GetWithNextLinkAsyncCollectionResultOfT.cs | 33 ++-- ...ecClientGetWithNextLinkCollectionResult.cs | 33 ++-- ...lientGetWithNextLinkCollectionResultOfT.cs | 33 ++-- ...lientGetWithPagingAsyncCollectionResult.cs | 17 +- ...ntGetWithPagingAsyncCollectionResultOfT.cs | 17 +- ...SpecClientGetWithPagingCollectionResult.cs | 17 +- ...cClientGetWithPagingCollectionResultOfT.cs | 17 +- ...WithStringNextLinkAsyncCollectionResult.cs | 35 ++-- ...hStringNextLinkAsyncCollectionResultOfT.cs | 35 ++-- ...ntGetWithStringNextLinkCollectionResult.cs | 35 ++-- ...etWithStringNextLinkCollectionResultOfT.cs | 35 ++-- .../src/Generated/DogOperations.cs | 4 + .../Sample-TypeSpec/src/Generated/Metrics.cs | 4 + .../src/Generated/PetOperations.cs | 6 + .../src/Generated/PlantOperations.cs | 10 ++ .../src/Generated/SampleTypeSpecClient.cs | 40 ++--- 31 files changed, 720 insertions(+), 248 deletions(-) diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientProvider.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientProvider.cs index d9e15213a1e..9c0e5cdb15e 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientProvider.cs @@ -370,9 +370,9 @@ private IReadOnlyList GetClientParameters() /// /// Gets the ActivitySource field used for distributed tracing instrumentation. - /// Returns null if method instrumentation is disabled or if this is a sub-client. + /// Returns null if method instrumentation is disabled. /// - internal FieldProvider? ActivitySourceField => ScmCodeModelGenerator.Instance.Configuration.GenerateMethodInstrumentation && ClientOptions != null + internal FieldProvider? ActivitySourceField => ScmCodeModelGenerator.Instance.Configuration.GenerateMethodInstrumentation ? _activitySourceField ??= BuildActivitySourceField() : null; diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs index d704c3eec74..fd3eeb2b703 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs @@ -5,6 +5,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -27,6 +28,7 @@ public class CollectionResultDefinition : TypeProvider protected bool IsAsync { get; } protected ClientProvider Client { get; } protected FieldProvider ClientField { get; } + protected FieldProvider? ActivityField { get; } protected InputOperation Operation { get; } protected InputPagingServiceMetadata Paging { get; } @@ -73,6 +75,16 @@ public CollectionResultDefinition(ClientProvider client, InputPagingServiceMetho Client.Type, "_client", this); + + if (Client.ActivitySourceField != null) + { + ActivityField = new FieldProvider( + FieldModifiers.Private | FieldModifiers.ReadOnly, + new CSharpType(typeof(Activity), isNullable: true), + "_activity", + this); + } + Operation = serviceMethod.Operation; Paging = serviceMethod.PagingMetadata; IsAsync = isAsync; @@ -187,7 +199,10 @@ protected override string BuildName() protected override TypeSignatureModifiers BuildDeclarationModifiers() => TypeSignatureModifiers.Internal | TypeSignatureModifiers.Partial | TypeSignatureModifiers.Class; - protected override FieldProvider[] BuildFields() => [ClientField, .. RequestFields]; + protected override FieldProvider[] BuildFields() + => ActivityField != null + ? [ClientField, ActivityField, .. RequestFields] + : [ClientField, .. RequestFields]; protected override CSharpType[] BuildImplements() => (_modelType: ItemModelType, IsAsync) switch @@ -204,6 +219,18 @@ protected override ConstructorProvider[] BuildConstructors() "client", $"The {Client.Type.Name} client used to send requests.", Client.Type); + var parameters = new List { clientParameter }; + parameters.AddRange(CreateRequestParameters); + ParameterProvider? activityParameter = null; + if (ActivityField != null) + { + activityParameter = new ParameterProvider( + "activity", + $"The activity for distributed tracing.", + new CSharpType(typeof(Activity), isNullable: true), + defaultValue: Null); + parameters.Add(activityParameter); + } return [ new ConstructorProvider( @@ -211,11 +238,8 @@ protected override ConstructorProvider[] BuildConstructors() Type, $"Initializes a new instance of {Name}, which is used to iterate over the pages of a collection.", MethodSignatureModifiers.Public, - [ - clientParameter, - .. CreateRequestParameters - ]), - BuildConstructorBody(clientParameter), + parameters), + BuildConstructorBody(clientParameter, activityParameter), this) ]; } @@ -230,7 +254,7 @@ protected ValueExpression BuildGetPropertyExpression(IReadOnlyList segme return ResponseModel.GetPropertyExpression(responseModel, segments); } - private MethodBodyStatement[] BuildConstructorBody(ParameterProvider clientParameter) + private MethodBodyStatement[] BuildConstructorBody(ParameterProvider clientParameter, ParameterProvider? activityParameter) { var statements = new List(CreateRequestParameters.Count + 1); @@ -242,6 +266,12 @@ private MethodBodyStatement[] BuildConstructorBody(ParameterProvider clientParam var field = RequestFields[parameterNumber]; statements.Add(field.Assign(parameter).Terminate()); } + + if (ActivityField != null && activityParameter != null) + { + statements.Add(ActivityField.Assign(activityParameter).Terminate()); + } + return statements.ToArray(); } @@ -254,6 +284,18 @@ protected override MethodProvider[] BuildMethods() (_, not null) => BuildGetRawPagesForContinuationToken() }; + // Wrap with try-finally to dispose the activity when enumeration completes + if (ActivityField != null) + { + getRawPagesMethodBody = + [ + new TryCatchFinallyStatement( + new TryExpression(getRawPagesMethodBody), + [], + new FinallyExpression(ActivityField.AsValueExpression.NullConditional().Invoke(nameof(IDisposable.Dispose), []).Terminate())) + ]; + } + var methods = new List { new MethodProvider( diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs index c70c11e4880..b3fe25dbc5b 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs @@ -129,7 +129,21 @@ private ScmMethodProvider BuildConvenienceMethod(MethodProvider protocolMethod, if (_pagingServiceMethod != null) { collection = ScmCodeModelGenerator.Instance.TypeFactory.ClientResponseApi.CreateClientCollectionResultDefinition(Client, _pagingServiceMethod, responseBodyType, isAsync); - methodBody = [.. GetPagingMethodBody(collection, ConvenienceMethodParameters, true)]; + // For paging, declare activity WITHOUT `using` so it's passed to the collection and disposed there via try-finally + VariableExpression? activityVar = null; + MethodBodyStatement[] pagingPrefix = []; + if (Client.ActivitySourceField != null) + { + var activityDecl = Declare( + "activity", + new CSharpType(typeof(Activity), isNullable: true), + Client.ActivitySourceField.Invoke( + nameof(ActivitySource.StartActivity), + [Literal($"{Client.Name}.{ServiceMethod.Name}"), FrameworkEnumValue(ActivityKind.Client)]), + out activityVar); + pagingPrefix = [activityDecl]; + } + methodBody = [.. pagingPrefix, .. GetPagingMethodBody(collection, ConvenienceMethodParameters, true, activityVar)]; } else if (responseBodyType is null) { @@ -166,8 +180,9 @@ .. GetStackVariablesForReturnValueConversion(result, responseBodyType, isAsync, ]; } - // Prepend activity instrumentation statement if enabled - if (Client.ActivitySourceField != null) + // Prepend activity instrumentation statement if enabled (non-paging methods only; + // paging methods handle the activity in the collection via try-finally) + if (Client.ActivitySourceField != null && _pagingServiceMethod == null) { var activityStatement = UsingDeclare( "activity", @@ -1013,20 +1028,19 @@ private ParameterProvider ProcessOptionalParameters( private IEnumerable GetPagingMethodBody( TypeProvider collection, IReadOnlyList parameters, - bool isConvenience) + bool isConvenience, + ValueExpression? activityVar = null) { if (isConvenience) { - return - [ - .. GetStackVariablesForProtocolParamConversion(ConvenienceMethodParameters, out var declarations), - Return(New.Instance( - collection.Type, - [ - This, - .. GetProtocolMethodArguments(declarations) - ])) - ]; + var stackStatements = GetStackVariablesForProtocolParamConversion(ConvenienceMethodParameters, out var declarations).ToArray(); + var constructorArgs = new List { This }; + constructorArgs.AddRange(GetProtocolMethodArguments(declarations)); + if (activityVar != null) + { + constructorArgs.Add(activityVar); + } + return [.. stackStatements, Return(New.Instance(collection.Type, [.. constructorArgs]))]; } return Return(New.Instance( diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs index b54ece36bd0..f1d9d74d964 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs @@ -1996,5 +1996,166 @@ public void ConvenienceMethod_JsonListBody_DoesNotUseXmlFromEnumerable() Assert.IsFalse(methodBody.Contains("rootNameHint")); Assert.IsFalse(methodBody.Contains("childNameHint")); } + + [Test] + public void TracingIsAddedToConvenienceMethod() + { + var inputOperation = InputFactory.Operation("TestOperation"); + var inputServiceMethod = InputFactory.BasicServiceMethod("TestOperation", inputOperation); + var inputClient = InputFactory.Client("TestClient", methods: [inputServiceMethod]); + + MockHelpers.LoadMockGenerator(clients: () => [inputClient], enableMethodInstrumentation: true); + + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + Assert.IsNotNull(client); + + // Root client has ActivitySourceField + Assert.IsNotNull(client!.ActivitySourceField); + + var methodCollection = new ScmMethodProviderCollection(inputServiceMethod, client); + var convenienceMethod = methodCollection.FirstOrDefault( + m => !m.Signature.Parameters.Any(p => p.Name == "options") && m.Signature.Name == "TestOperation"); + Assert.IsNotNull(convenienceMethod); + + var methodBody = convenienceMethod!.BodyStatements!.ToDisplayString(); + // Verify activity is started with the correct name + StringAssert.Contains("_activitySource.StartActivity(\"TestClient.TestOperation\"", methodBody); + // Verify it uses `using` so it's disposed when the method returns + StringAssert.Contains("using global::System.Diagnostics.Activity activity", methodBody); + } + + [Test] + public void TracingIsNotAddedToConvenienceMethodWhenDisabled() + { + var inputOperation = InputFactory.Operation("TestOperation"); + var inputServiceMethod = InputFactory.BasicServiceMethod("TestOperation", inputOperation); + var inputClient = InputFactory.Client("TestClient", methods: [inputServiceMethod]); + + MockHelpers.LoadMockGenerator(clients: () => [inputClient]); + + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + Assert.IsNotNull(client); + Assert.IsNull(client!.ActivitySourceField); + + var methodCollection = new ScmMethodProviderCollection(inputServiceMethod, client); + var convenienceMethod = methodCollection.FirstOrDefault( + m => !m.Signature.Parameters.Any(p => p.Name == "options") && m.Signature.Name == "TestOperation"); + Assert.IsNotNull(convenienceMethod); + + var methodBody = convenienceMethod!.BodyStatements!.ToDisplayString(); + StringAssert.DoesNotContain("StartActivity", methodBody); + } + + [Test] + public void TracingIsAddedToSubClientConvenienceMethod() + { + var inputOperation = InputFactory.Operation("SubOperation"); + var inputServiceMethod = InputFactory.BasicServiceMethod("SubOperation", inputOperation); + var parentClient = InputFactory.Client("ParentClient"); + var subClient = InputFactory.Client("SubClient", methods: [inputServiceMethod], parent: parentClient); + + MockHelpers.LoadMockGenerator(clients: () => [parentClient, subClient], enableMethodInstrumentation: true); + + var subClientProvider = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(subClient); + Assert.IsNotNull(subClientProvider); + + // Sub-client also has ActivitySourceField when instrumentation is enabled + Assert.IsNotNull(subClientProvider!.ActivitySourceField); + + var methodCollection = new ScmMethodProviderCollection(inputServiceMethod, subClientProvider); + var convenienceMethod = methodCollection.FirstOrDefault( + m => !m.Signature.Parameters.Any(p => p.Name == "options") && m.Signature.Name == "SubOperation"); + Assert.IsNotNull(convenienceMethod); + + var methodBody = convenienceMethod!.BodyStatements!.ToDisplayString(); + // Sub-client methods get the activity with sub-client name + StringAssert.Contains("_activitySource.StartActivity(\"SubClient.SubOperation\"", methodBody); + StringAssert.Contains("using global::System.Diagnostics.Activity activity", methodBody); + } + + [Test] + public void TracingIsAddedToPagingConvenienceMethod() + { + var pagingMetadata = InputFactory.PagingMetadata(["items"], null, null); + var inputModel = InputFactory.Model("cat", properties: + [ + InputFactory.Property("color", InputPrimitiveType.String, isRequired: true), + ]); + + var response = InputFactory.OperationResponse( + [200], + InputFactory.Model( + "page", + properties: [InputFactory.Property("cats", InputFactory.Array(inputModel))])); + var operation = InputFactory.Operation("GetCats", responses: [response]); + var inputServiceMethod = InputFactory.PagingServiceMethod("GetCats", operation, pagingMetadata: pagingMetadata); + var inputClient = InputFactory.Client("TestClient", methods: [inputServiceMethod]); + + MockHelpers.LoadMockGenerator(inputModels: () => [inputModel], clients: () => [inputClient], enableMethodInstrumentation: true); + + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + Assert.IsNotNull(client); + + var methodCollection = new ScmMethodProviderCollection(inputServiceMethod, client!); + var convenienceMethod = methodCollection.FirstOrDefault( + m => !m.Signature.Parameters.Any(p => p.Name == "options") && m.Signature.Name == "GetCats"); + Assert.IsNotNull(convenienceMethod); + + var methodBody = convenienceMethod!.BodyStatements!.ToDisplayString(); + // Paging methods: activity declared without `using` so it's passed to the collection + StringAssert.Contains("_activitySource.StartActivity(\"TestClient.GetCats\"", methodBody); + // Should NOT use `using` for paging - the activity is disposed inside the collection via try-finally + StringAssert.DoesNotContain("using global::System.Diagnostics.Activity activity", methodBody); + StringAssert.Contains("global::System.Diagnostics.Activity activity =", methodBody); + } + + [Test] + public void TracingInPagingCollectionDisposesActivityViaFinally() + { + var pagingMetadata = InputFactory.PagingMetadata(["items"], null, null); + var inputModel = InputFactory.Model("cat", properties: + [ + InputFactory.Property("color", InputPrimitiveType.String, isRequired: true), + ]); + + var response = InputFactory.OperationResponse( + [200], + InputFactory.Model( + "page", + properties: [InputFactory.Property("cats", InputFactory.Array(inputModel))])); + var operation = InputFactory.Operation("GetCats", responses: [response]); + var inputServiceMethod = InputFactory.PagingServiceMethod("GetCats", operation, pagingMetadata: pagingMetadata); + var inputClient = InputFactory.Client("TestClient", methods: [inputServiceMethod]); + + MockHelpers.LoadMockGenerator(inputModels: () => [inputModel], clients: () => [inputClient], enableMethodInstrumentation: true); + + var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient); + Assert.IsNotNull(client); + + // Find the CollectionResultDefinition that was generated + var collectionResultDefinition = ScmCodeModelGenerator.Instance.OutputLibrary.TypeProviders + .OfType() + .FirstOrDefault(); + Assert.IsNotNull(collectionResultDefinition); + + // CollectionResultDefinition should have an _activity field + var activityField = collectionResultDefinition!.Fields.FirstOrDefault(f => f.Name == "_activity"); + Assert.IsNotNull(activityField, "CollectionResultDefinition should have an _activity field"); + + // The constructor should accept an activity parameter + var ctor = collectionResultDefinition.Constructors.FirstOrDefault(); + Assert.IsNotNull(ctor); + var activityParam = ctor!.Signature.Parameters.FirstOrDefault(p => p.Name == "activity"); + Assert.IsNotNull(activityParam, "Constructor should have an 'activity' parameter"); + + // GetRawPages method should have try-finally to dispose the activity + var getRawPagesMethod = collectionResultDefinition.Methods.FirstOrDefault(m => m.Signature.Name == "GetRawPages"); + Assert.IsNotNull(getRawPagesMethod); + var rawPagesBody = getRawPagesMethod!.BodyStatements!.ToDisplayString(); + StringAssert.Contains("try", rawPagesBody); + StringAssert.Contains("finally", rawPagesBody); + StringAssert.Contains("_activity?.Dispose()", rawPagesBody); + } + } } diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/MockHelpers.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/MockHelpers.cs index 08dc4e30676..b66bc9ee2a0 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/MockHelpers.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/MockHelpers.cs @@ -76,7 +76,8 @@ public static Mock LoadMockGenerator( Func? createOutputLibrary = null, bool includeXmlDocs = false, Func? createCSharpTypeCoreFallback = null, - Func? createModelCore = null) + Func? createModelCore = null, + bool enableMethodInstrumentation = false) { IReadOnlyList inputNsApiVersions = apiVersions?.Invoke() ?? []; IReadOnlyList inputNsLiterals = inputLiterals?.Invoke() ?? []; @@ -150,6 +151,10 @@ public static Mock LoadMockGenerator( { configuration = "{\"disable-xml-docs\": false, \"package-name\": \"Sample.Namespace\", \"generate-method-instrumentation\": false}"; } + else if (enableMethodInstrumentation) + { + configuration = "{\"package-name\": \"SampleLibrary\", \"generate-method-instrumentation\": true}"; + } object?[] parameters = [_configFilePath, configuration]; var config = loadMethod?.Invoke(null, parameters); var mockGeneratorContext = new Mock(config!); diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/AnimalOperations.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/AnimalOperations.cs index 10d3b31fe97..fec37ba0248 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/AnimalOperations.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/AnimalOperations.cs @@ -8,6 +8,7 @@ using System; using System.ClientModel; using System.ClientModel.Primitives; +using System.Diagnostics; using System.Threading; using System.Threading.Tasks; @@ -17,6 +18,7 @@ namespace SampleTypeSpec public partial class AnimalOperations { private readonly Uri _endpoint; + private static readonly ActivitySource _activitySource = new ActivitySource("SampleTypeSpec"); /// Initializes a new instance of AnimalOperations for mocking. protected AnimalOperations() @@ -86,6 +88,7 @@ public virtual ClientResult UpdatePetAsAnimal(Animal animal, Cancellatio { Argument.AssertNotNull(animal, nameof(animal)); + using Activity activity = _activitySource.StartActivity("AnimalOperations.UpdatePetAsAnimal", ActivityKind.Client); ClientResult result = UpdatePetAsAnimal(animal, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Animal)result, result.GetRawResponse()); } @@ -99,6 +102,7 @@ public virtual async Task> UpdatePetAsAnimalAsync(Animal an { Argument.AssertNotNull(animal, nameof(animal)); + using Activity activity = _activitySource.StartActivity("AnimalOperations.UpdatePetAsAnimal", ActivityKind.Client); ClientResult result = await UpdatePetAsAnimalAsync(animal, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Animal)result, result.GetRawResponse()); } @@ -154,6 +158,7 @@ public virtual ClientResult UpdateDogAsAnimal(Animal animal, Cancellatio { Argument.AssertNotNull(animal, nameof(animal)); + using Activity activity = _activitySource.StartActivity("AnimalOperations.UpdateDogAsAnimal", ActivityKind.Client); ClientResult result = UpdateDogAsAnimal(animal, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Animal)result, result.GetRawResponse()); } @@ -167,6 +172,7 @@ public virtual async Task> UpdateDogAsAnimalAsync(Animal an { Argument.AssertNotNull(animal, nameof(animal)); + using Activity activity = _activitySource.StartActivity("AnimalOperations.UpdateDogAsAnimal", ActivityKind.Client); ClientResult result = await UpdateDogAsAnimalAsync(animal, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Animal)result, result.GetRawResponse()); } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult.cs index 89c5702b006..82a64a287e0 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult.cs @@ -9,12 +9,14 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly string _token; private readonly RequestOptions _options; @@ -22,30 +24,39 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenAsyncCollecti /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) { _client = client; _token = token; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); - string nextToken = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; - - nextToken = ((ListWithContinuationTokenResponse)result).NextToken; - if (string.IsNullOrEmpty(nextToken)) + PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); + string nextToken = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + yield return result; + + nextToken = ((ListWithContinuationTokenResponse)result).NextToken; + if (string.IsNullOrEmpty(nextToken)) + { + yield break; + } + message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); } - message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT.cs index 75eb97d46bb..9183bd6e275 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT.cs @@ -9,6 +9,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; namespace SampleTypeSpec @@ -16,6 +17,7 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly string _token; private readonly RequestOptions _options; @@ -23,30 +25,39 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenAsyncCollecti /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) { _client = client; _token = token; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); - string nextToken = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; - - nextToken = ((ListWithContinuationTokenResponse)result).NextToken; - if (string.IsNullOrEmpty(nextToken)) + PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); + string nextToken = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + yield return result; + + nextToken = ((ListWithContinuationTokenResponse)result).NextToken; + if (string.IsNullOrEmpty(nextToken)) + { + yield break; + } + message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); } - message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResult.cs index 0c52e02a228..703f16ae262 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResult.cs @@ -9,12 +9,14 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithContinuationTokenCollectionResult : CollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly string _token; private readonly RequestOptions _options; @@ -22,30 +24,39 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenCollectionRes /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithContinuationTokenCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) { _client = client; _token = token; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); - string nextToken = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; - - nextToken = ((ListWithContinuationTokenResponse)result).NextToken; - if (string.IsNullOrEmpty(nextToken)) + PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); + string nextToken = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + yield return result; + + nextToken = ((ListWithContinuationTokenResponse)result).NextToken; + if (string.IsNullOrEmpty(nextToken)) + { + yield break; + } + message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); } - message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT.cs index d5eabeb8a8e..4e6862904af 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT.cs @@ -9,12 +9,14 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT : CollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly string _token; private readonly RequestOptions _options; @@ -22,30 +24,39 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenCollectionRes /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) { _client = client; _token = token; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); - string nextToken = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; - - nextToken = ((ListWithContinuationTokenResponse)result).NextToken; - if (string.IsNullOrEmpty(nextToken)) + PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); + string nextToken = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + yield return result; + + nextToken = ((ListWithContinuationTokenResponse)result).NextToken; + if (string.IsNullOrEmpty(nextToken)) + { + yield break; + } + message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); } - message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult.cs index ca9ff340edf..81dd0453a0c 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult.cs @@ -9,12 +9,14 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly string _token; private readonly RequestOptions _options; @@ -22,33 +24,42 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderRespons /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) { _client = client; _token = token; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); - string nextToken = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; - - if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) - { - nextToken = value; - } - else + PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); + string nextToken = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + yield return result; + + if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) + { + nextToken = value; + } + else + { + yield break; + } + message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); } - message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT.cs index ebdce57e71a..082cfde21bf 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT.cs @@ -9,6 +9,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; namespace SampleTypeSpec @@ -16,6 +17,7 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly string _token; private readonly RequestOptions _options; @@ -23,33 +25,42 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderRespons /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) { _client = client; _token = token; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); - string nextToken = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; - - if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) - { - nextToken = value; - } - else + PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); + string nextToken = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + yield return result; + + if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) + { + nextToken = value; + } + else + { + yield break; + } + message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); } - message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult.cs index f50dab79b40..eda78df0981 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult.cs @@ -9,12 +9,14 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult : CollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly string _token; private readonly RequestOptions _options; @@ -22,33 +24,42 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderRespons /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) { _client = client; _token = token; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); - string nextToken = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; - - if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) - { - nextToken = value; - } - else + PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); + string nextToken = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + yield return result; + + if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) + { + nextToken = value; + } + else + { + yield break; + } + message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); } - message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT.cs index b4e10128332..3e4f084d674 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT.cs @@ -9,12 +9,14 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT : CollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly string _token; private readonly RequestOptions _options; @@ -22,33 +24,42 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderRespons /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) { _client = client; _token = token; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); - string nextToken = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; - - if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) - { - nextToken = value; - } - else + PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); + string nextToken = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + yield return result; + + if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) + { + nextToken = value; + } + else + { + yield break; + } + message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); } - message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult.cs index 034facf2353..5213302b1e0 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult.cs @@ -9,40 +9,51 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; - - nextPageUri = ((ListWithNextLinkResponse)result).Next; - if (nextPageUri == null) + PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + yield return result; + + nextPageUri = ((ListWithNextLinkResponse)result).Next; + if (nextPageUri == null) + { + yield break; + } + message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); } - message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT.cs index 6ecde199e1c..da5f87e3ad3 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT.cs @@ -9,6 +9,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; namespace SampleTypeSpec @@ -16,34 +17,44 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; - - nextPageUri = ((ListWithNextLinkResponse)result).Next; - if (nextPageUri == null) + PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + yield return result; + + nextPageUri = ((ListWithNextLinkResponse)result).Next; + if (nextPageUri == null) + { + yield break; + } + message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); } - message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResult.cs index e43d87a042a..e3ac7441096 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResult.cs @@ -9,40 +9,51 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithNextLinkCollectionResult : CollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithNextLinkCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithNextLinkCollectionResult(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithNextLinkCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; - - nextPageUri = ((ListWithNextLinkResponse)result).Next; - if (nextPageUri == null) + PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + yield return result; + + nextPageUri = ((ListWithNextLinkResponse)result).Next; + if (nextPageUri == null) + { + yield break; + } + message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); } - message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResultOfT.cs index 51e1a2f5494..4a51a7ef510 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResultOfT.cs @@ -9,40 +9,51 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithNextLinkCollectionResultOfT : CollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithNextLinkCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithNextLinkCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithNextLinkCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; - - nextPageUri = ((ListWithNextLinkResponse)result).Next; - if (nextPageUri == null) + PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + yield return result; + + nextPageUri = ((ListWithNextLinkResponse)result).Next; + if (nextPageUri == null) + { + yield break; + } + message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); } - message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResult.cs index b62bc4e45b0..07b1be5960e 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResult.cs @@ -8,29 +8,40 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithPagingAsyncCollectionResult : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithPagingAsyncCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithPagingAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithPagingAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - PipelineMessage message = _client.CreateGetWithPagingRequest(_options); - yield return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + try + { + PipelineMessage message = _client.CreateGetWithPagingRequest(_options); + yield return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + finally + { + _activity?.Dispose(); + } } /// Gets the continuation token from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT.cs index fa5e727fd26..95d8c2acebb 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT.cs @@ -8,6 +8,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; namespace SampleTypeSpec @@ -15,23 +16,33 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - PipelineMessage message = _client.CreateGetWithPagingRequest(_options); - yield return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + try + { + PipelineMessage message = _client.CreateGetWithPagingRequest(_options); + yield return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + finally + { + _activity?.Dispose(); + } } /// Gets the continuation token from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResult.cs index 9fb94135cc9..a8718edf8e7 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResult.cs @@ -8,29 +8,40 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithPagingCollectionResult : CollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithPagingCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithPagingCollectionResult(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithPagingCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - PipelineMessage message = _client.CreateGetWithPagingRequest(_options); - yield return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + try + { + PipelineMessage message = _client.CreateGetWithPagingRequest(_options); + yield return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + finally + { + _activity?.Dispose(); + } } /// Gets the continuation token from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResultOfT.cs index 6031b02f615..06ac83140d4 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResultOfT.cs @@ -8,29 +8,40 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithPagingCollectionResultOfT : CollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithPagingCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithPagingCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithPagingCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - PipelineMessage message = _client.CreateGetWithPagingRequest(_options); - yield return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + try + { + PipelineMessage message = _client.CreateGetWithPagingRequest(_options); + yield return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + finally + { + _activity?.Dispose(); + } } /// Gets the continuation token from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult.cs index 5f2f970edc6..14123f5b2b7 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult.cs @@ -9,41 +9,52 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; - - string nextPageString = ((ListWithStringNextLinkResponse)result).Next; - if (string.IsNullOrEmpty(nextPageString)) + PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + yield return result; + + string nextPageString = ((ListWithStringNextLinkResponse)result).Next; + if (string.IsNullOrEmpty(nextPageString)) + { + yield break; + } + nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); + message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); } - nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); - message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT.cs index b8b6c3524a1..aff41c31dc9 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT.cs @@ -9,6 +9,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; namespace SampleTypeSpec @@ -16,35 +17,45 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; - - string nextPageString = ((ListWithStringNextLinkResponse)result).Next; - if (string.IsNullOrEmpty(nextPageString)) + PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + yield return result; + + string nextPageString = ((ListWithStringNextLinkResponse)result).Next; + if (string.IsNullOrEmpty(nextPageString)) + { + yield break; + } + nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); + message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); } - nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); - message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResult.cs index 041284bd988..2b76ffc64a2 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResult.cs @@ -9,41 +9,52 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithStringNextLinkCollectionResult : CollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithStringNextLinkCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithStringNextLinkCollectionResult(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithStringNextLinkCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; - - string nextPageString = ((ListWithStringNextLinkResponse)result).Next; - if (string.IsNullOrEmpty(nextPageString)) + PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + yield return result; + + string nextPageString = ((ListWithStringNextLinkResponse)result).Next; + if (string.IsNullOrEmpty(nextPageString)) + { + yield break; + } + nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); + message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); } - nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); - message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT.cs index 5d4b31a80f6..480594534fb 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT.cs @@ -9,41 +9,52 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; +using System.Diagnostics; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT : CollectionResult { private readonly SampleTypeSpecClient _client; + private readonly Activity _activity; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - public SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options) + /// The activity for distributed tracing. + public SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) { _client = client; _options = options; + _activity = activity; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) + try { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; - - string nextPageString = ((ListWithStringNextLinkResponse)result).Next; - if (string.IsNullOrEmpty(nextPageString)) + PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - yield break; + ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + yield return result; + + string nextPageString = ((ListWithStringNextLinkResponse)result).Next; + if (string.IsNullOrEmpty(nextPageString)) + { + yield break; + } + nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); + message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); } - nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); - message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); + } + finally + { + _activity?.Dispose(); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/DogOperations.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/DogOperations.cs index a45d14cb50f..c7265e69bc2 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/DogOperations.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/DogOperations.cs @@ -8,6 +8,7 @@ using System; using System.ClientModel; using System.ClientModel.Primitives; +using System.Diagnostics; using System.Threading; using System.Threading.Tasks; @@ -17,6 +18,7 @@ namespace SampleTypeSpec public partial class DogOperations { private readonly Uri _endpoint; + private static readonly ActivitySource _activitySource = new ActivitySource("SampleTypeSpec"); /// Initializes a new instance of DogOperations for mocking. protected DogOperations() @@ -86,6 +88,7 @@ public virtual ClientResult UpdateDogAsDog(Dog dog, CancellationToken cance { Argument.AssertNotNull(dog, nameof(dog)); + using Activity activity = _activitySource.StartActivity("DogOperations.UpdateDogAsDog", ActivityKind.Client); ClientResult result = UpdateDogAsDog(dog, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Dog)result, result.GetRawResponse()); } @@ -99,6 +102,7 @@ public virtual async Task> UpdateDogAsDogAsync(Dog dog, Cancel { Argument.AssertNotNull(dog, nameof(dog)); + using Activity activity = _activitySource.StartActivity("DogOperations.UpdateDogAsDog", ActivityKind.Client); ClientResult result = await UpdateDogAsDogAsync(dog, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Dog)result, result.GetRawResponse()); } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Metrics.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Metrics.cs index 8f9c0ffffb6..53bce735105 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Metrics.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Metrics.cs @@ -8,6 +8,7 @@ using System; using System.ClientModel; using System.ClientModel.Primitives; +using System.Diagnostics; using System.Threading; using System.Threading.Tasks; @@ -17,6 +18,7 @@ namespace SampleTypeSpec public partial class Metrics { private readonly Uri _endpoint; + private static readonly ActivitySource _activitySource = new ActivitySource("SampleTypeSpec"); /// Initializes a new instance of Metrics for mocking. protected Metrics() @@ -98,6 +100,7 @@ public virtual async Task GetWidgetMetricsAsync(string day, Reques /// Service returned a non-success status code. public virtual ClientResult GetWidgetMetrics(DaysOfWeekExtensibleEnum day, CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("Metrics.GetWidgetMetrics", ActivityKind.Client); ClientResult result = GetWidgetMetrics(day.ToString(), cancellationToken.ToRequestOptions()); return ClientResult.FromValue((GetWidgetMetricsResponse)result, result.GetRawResponse()); } @@ -108,6 +111,7 @@ public virtual ClientResult GetWidgetMetrics(DaysOfWee /// Service returned a non-success status code. public virtual async Task> GetWidgetMetricsAsync(DaysOfWeekExtensibleEnum day, CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("Metrics.GetWidgetMetrics", ActivityKind.Client); ClientResult result = await GetWidgetMetricsAsync(day.ToString(), cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((GetWidgetMetricsResponse)result, result.GetRawResponse()); } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PetOperations.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PetOperations.cs index 7f7531baa9d..75141edef9c 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PetOperations.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PetOperations.cs @@ -8,6 +8,7 @@ using System; using System.ClientModel; using System.ClientModel.Primitives; +using System.Diagnostics; using System.Threading; using System.Threading.Tasks; @@ -17,6 +18,7 @@ namespace SampleTypeSpec public partial class PetOperations { private readonly Uri _endpoint; + private static readonly ActivitySource _activitySource = new ActivitySource("SampleTypeSpec"); /// Initializes a new instance of PetOperations for mocking. protected PetOperations() @@ -86,6 +88,7 @@ public virtual ClientResult UpdatePetAsPet(Pet pet, CancellationToken cance { Argument.AssertNotNull(pet, nameof(pet)); + using Activity activity = _activitySource.StartActivity("PetOperations.UpdatePetAsPet", ActivityKind.Client); ClientResult result = UpdatePetAsPet(pet, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Pet)result, result.GetRawResponse()); } @@ -99,6 +102,7 @@ public virtual async Task> UpdatePetAsPetAsync(Pet pet, Cancel { Argument.AssertNotNull(pet, nameof(pet)); + using Activity activity = _activitySource.StartActivity("PetOperations.UpdatePetAsPet", ActivityKind.Client); ClientResult result = await UpdatePetAsPetAsync(pet, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Pet)result, result.GetRawResponse()); } @@ -154,6 +158,7 @@ public virtual ClientResult UpdateDogAsPet(Pet pet, CancellationToken cance { Argument.AssertNotNull(pet, nameof(pet)); + using Activity activity = _activitySource.StartActivity("PetOperations.UpdateDogAsPet", ActivityKind.Client); ClientResult result = UpdateDogAsPet(pet, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Pet)result, result.GetRawResponse()); } @@ -167,6 +172,7 @@ public virtual async Task> UpdateDogAsPetAsync(Pet pet, Cancel { Argument.AssertNotNull(pet, nameof(pet)); + using Activity activity = _activitySource.StartActivity("PetOperations.UpdateDogAsPet", ActivityKind.Client); ClientResult result = await UpdateDogAsPetAsync(pet, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Pet)result, result.GetRawResponse()); } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PlantOperations.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PlantOperations.cs index 6db6d1ce51e..c006101ba72 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PlantOperations.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PlantOperations.cs @@ -8,6 +8,7 @@ using System; using System.ClientModel; using System.ClientModel.Primitives; +using System.Diagnostics; using System.Threading; using System.Threading.Tasks; @@ -17,6 +18,7 @@ namespace SampleTypeSpec public partial class PlantOperations { private readonly Uri _endpoint; + private static readonly ActivitySource _activitySource = new ActivitySource("SampleTypeSpec"); /// Initializes a new instance of PlantOperations for mocking. protected PlantOperations() @@ -74,6 +76,7 @@ public virtual async Task GetTreeAsync(RequestOptions options) /// Service returned a non-success status code. public virtual ClientResult GetTree(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("PlantOperations.GetTree", ActivityKind.Client); ClientResult result = GetTree(cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Tree)result, result.GetRawResponse()); } @@ -83,6 +86,7 @@ public virtual ClientResult GetTree(CancellationToken cancellationToken = /// Service returned a non-success status code. public virtual async Task> GetTreeAsync(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("PlantOperations.GetTree", ActivityKind.Client); ClientResult result = await GetTreeAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Tree)result, result.GetRawResponse()); } @@ -126,6 +130,7 @@ public virtual async Task GetTreeAsJsonAsync(RequestOptions option /// Service returned a non-success status code. public virtual ClientResult GetTreeAsJson(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("PlantOperations.GetTreeAsJson", ActivityKind.Client); ClientResult result = GetTreeAsJson(cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Tree)result, result.GetRawResponse()); } @@ -135,6 +140,7 @@ public virtual ClientResult GetTreeAsJson(CancellationToken cancellationTo /// Service returned a non-success status code. public virtual async Task> GetTreeAsJsonAsync(CancellationToken cancellationToken = default) { + using Activity activity = _activitySource.StartActivity("PlantOperations.GetTreeAsJson", ActivityKind.Client); ClientResult result = await GetTreeAsJsonAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Tree)result, result.GetRawResponse()); } @@ -190,6 +196,7 @@ public virtual ClientResult UpdateTree(Tree tree, CancellationToken cancel { Argument.AssertNotNull(tree, nameof(tree)); + using Activity activity = _activitySource.StartActivity("PlantOperations.UpdateTree", ActivityKind.Client); using BinaryContent content = tree.ToBinaryContent("X"); ClientResult result = UpdateTree(content, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Tree)result, result.GetRawResponse()); @@ -204,6 +211,7 @@ public virtual async Task> UpdateTreeAsync(Tree tree, Cancell { Argument.AssertNotNull(tree, nameof(tree)); + using Activity activity = _activitySource.StartActivity("PlantOperations.UpdateTree", ActivityKind.Client); using BinaryContent content = tree.ToBinaryContent("X"); ClientResult result = await UpdateTreeAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Tree)result, result.GetRawResponse()); @@ -260,6 +268,7 @@ public virtual ClientResult UpdateTreeAsJson(Tree tree, CancellationToken { Argument.AssertNotNull(tree, nameof(tree)); + using Activity activity = _activitySource.StartActivity("PlantOperations.UpdateTreeAsJson", ActivityKind.Client); using BinaryContent content = tree.ToBinaryContent("J"); ClientResult result = UpdateTreeAsJson(content, cancellationToken.ToRequestOptions()); return ClientResult.FromValue((Tree)result, result.GetRawResponse()); @@ -274,6 +283,7 @@ public virtual async Task> UpdateTreeAsJsonAsync(Tree tree, C { Argument.AssertNotNull(tree, nameof(tree)); + using Activity activity = _activitySource.StartActivity("PlantOperations.UpdateTreeAsJson", ActivityKind.Client); using BinaryContent content = tree.ToBinaryContent("J"); ClientResult result = await UpdateTreeAsJsonAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue((Tree)result, result.GetRawResponse()); diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs index 1a4d8093b78..a897e72c58f 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs @@ -1441,8 +1441,8 @@ public virtual AsyncCollectionResult GetWithNextLinkAsync(RequestOptions options /// Service returned a non-success status code. public virtual CollectionResult GetWithNextLink(CancellationToken cancellationToken = default) { - using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); - return new SampleTypeSpecClientGetWithNextLinkCollectionResultOfT(this, cancellationToken.ToRequestOptions()); + Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); + return new SampleTypeSpecClientGetWithNextLinkCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); } /// List things with nextlink. @@ -1450,8 +1450,8 @@ public virtual CollectionResult GetWithNextLink(CancellationToken cancell /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithNextLinkAsync(CancellationToken cancellationToken = default) { - using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); - return new SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions()); + Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); + return new SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); } /// @@ -1491,8 +1491,8 @@ public virtual AsyncCollectionResult GetWithStringNextLinkAsync(RequestOptions o /// Service returned a non-success status code. public virtual CollectionResult GetWithStringNextLink(CancellationToken cancellationToken = default) { - using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); - return new SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT(this, cancellationToken.ToRequestOptions()); + Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); + return new SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); } /// List things with nextlink. @@ -1500,8 +1500,8 @@ public virtual CollectionResult GetWithStringNextLink(CancellationToken c /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithStringNextLinkAsync(CancellationToken cancellationToken = default) { - using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); - return new SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions()); + Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); + return new SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); } /// @@ -1544,8 +1544,8 @@ public virtual AsyncCollectionResult GetWithContinuationTokenAsync(string token, /// Service returned a non-success status code. public virtual CollectionResult GetWithContinuationToken(string token = default, CancellationToken cancellationToken = default) { - using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); - return new SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT(this, token, cancellationToken.ToRequestOptions()); + Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); + return new SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), activity); } /// List things with continuation token. @@ -1554,8 +1554,8 @@ public virtual CollectionResult GetWithContinuationToken(string token = d /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithContinuationTokenAsync(string token = default, CancellationToken cancellationToken = default) { - using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); - return new SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT(this, token, cancellationToken.ToRequestOptions()); + Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); + return new SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), activity); } /// @@ -1598,8 +1598,8 @@ public virtual AsyncCollectionResult GetWithContinuationTokenHeaderResponseAsync /// Service returned a non-success status code. public virtual CollectionResult GetWithContinuationTokenHeaderResponse(string token = default, CancellationToken cancellationToken = default) { - using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); - return new SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT(this, token, cancellationToken.ToRequestOptions()); + Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); + return new SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), activity); } /// List things with continuation token header response. @@ -1608,8 +1608,8 @@ public virtual CollectionResult GetWithContinuationTokenHeaderResponse(st /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithContinuationTokenHeaderResponseAsync(string token = default, CancellationToken cancellationToken = default) { - using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); - return new SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT(this, token, cancellationToken.ToRequestOptions()); + Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); + return new SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), activity); } /// @@ -1649,8 +1649,8 @@ public virtual AsyncCollectionResult GetWithPagingAsync(RequestOptions options) /// Service returned a non-success status code. public virtual CollectionResult GetWithPaging(CancellationToken cancellationToken = default) { - using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); - return new SampleTypeSpecClientGetWithPagingCollectionResultOfT(this, cancellationToken.ToRequestOptions()); + Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); + return new SampleTypeSpecClientGetWithPagingCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); } /// List things with paging. @@ -1658,8 +1658,8 @@ public virtual CollectionResult GetWithPaging(CancellationToken cancellat /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithPagingAsync(CancellationToken cancellationToken = default) { - using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); - return new SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions()); + Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); + return new SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); } /// From f719be48ddf5cb6f1cbc108814aa0aae91fcdec7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 4 Mar 2026 22:08:38 +0000 Subject: [PATCH 5/6] feat: add exception tracking to match Azure pattern (try-catch with SetStatus) Non-paging convenience methods now wrap the HTTP call in try-catch to mark activity spans as error when exceptions are thrown, matching the Azure SDK pattern. Paging iterator methods can only use try-finally due to C# CS1626 (yield return cannot be inside a try-catch block). Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> --- .../Providers/CollectionResultDefinition.cs | 5 +- .../Providers/ScmMethodProviderCollection.cs | 11 +- .../ScmMethodProviderCollectionTests.cs | 9 +- .../src/Generated/AnimalOperations.cs | 48 +- .../src/Generated/DogOperations.cs | 24 +- .../Sample-TypeSpec/src/Generated/Metrics.cs | 24 +- .../src/Generated/PetOperations.cs | 48 +- .../src/Generated/PlantOperations.cs | 104 +++- .../src/Generated/SampleTypeSpecClient.cs | 564 ++++++++++++++---- 9 files changed, 674 insertions(+), 163 deletions(-) diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs index fd3eeb2b703..39a8a31c72e 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs @@ -284,7 +284,10 @@ protected override MethodProvider[] BuildMethods() (_, not null) => BuildGetRawPagesForContinuationToken() }; - // Wrap with try-finally to dispose the activity when enumeration completes + // Wrap with try-finally to dispose the activity when enumeration completes. + // Note: A catch clause cannot be added here because C# does not allow 'yield return' inside + // a try block that has a catch clause (CS1626). Exception tracking is handled in the + // non-paging convenience methods instead. if (ActivityField != null) { getRawPagesMethodBody = diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs index b3fe25dbc5b..113e74977c7 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs @@ -181,7 +181,7 @@ .. GetStackVariablesForReturnValueConversion(result, responseBodyType, isAsync, } // Prepend activity instrumentation statement if enabled (non-paging methods only; - // paging methods handle the activity in the collection via try-finally) + // paging methods handle the activity disposal in the collection via try-finally) if (Client.ActivitySourceField != null && _pagingServiceMethod == null) { var activityStatement = UsingDeclare( @@ -190,8 +190,13 @@ .. GetStackVariablesForReturnValueConversion(result, responseBodyType, isAsync, Client.ActivitySourceField.Invoke( nameof(ActivitySource.StartActivity), [Literal($"{Client.Name}.{ServiceMethod.Name}"), FrameworkEnumValue(ActivityKind.Client)]), - out _); - methodBody = [activityStatement, .. methodBody]; + out var activityVar); + var exceptionDeclaration = new DeclarationExpression(typeof(Exception), "ex", out var exVar); + var catchBlock = new CatchExpression(exceptionDeclaration, + activityVar.NullConditional().Invoke(nameof(Activity.SetStatus), + [FrameworkEnumValue(ActivityStatusCode.Error), exVar.Property(nameof(Exception.Message))]).Terminate(), + Throw()); + methodBody = [activityStatement, new TryCatchFinallyStatement(new TryExpression(methodBody), catchBlock)]; } var convenienceMethod = new ScmMethodProvider(methodSignature, methodBody, EnclosingType, ScmMethodKind.Convenience, collectionDefinition: collection, serviceMethod: ServiceMethod); diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs index f1d9d74d964..0e25b508a7f 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs @@ -2022,6 +2022,10 @@ public void TracingIsAddedToConvenienceMethod() StringAssert.Contains("_activitySource.StartActivity(\"TestClient.TestOperation\"", methodBody); // Verify it uses `using` so it's disposed when the method returns StringAssert.Contains("using global::System.Diagnostics.Activity activity", methodBody); + // Verify exception tracking: catch block sets error status before rethrowing + StringAssert.Contains("catch", methodBody); + StringAssert.Contains("SetStatus", methodBody); + StringAssert.Contains("ActivityStatusCode.Error", methodBody); } [Test] @@ -2148,13 +2152,16 @@ public void TracingInPagingCollectionDisposesActivityViaFinally() var activityParam = ctor!.Signature.Parameters.FirstOrDefault(p => p.Name == "activity"); Assert.IsNotNull(activityParam, "Constructor should have an 'activity' parameter"); - // GetRawPages method should have try-finally to dispose the activity + // GetRawPages method should have try-finally to dispose the activity. + // Note: C# does not allow 'yield return' inside a try-catch (CS1626), so only try-finally is used here. var getRawPagesMethod = collectionResultDefinition.Methods.FirstOrDefault(m => m.Signature.Name == "GetRawPages"); Assert.IsNotNull(getRawPagesMethod); var rawPagesBody = getRawPagesMethod!.BodyStatements!.ToDisplayString(); StringAssert.Contains("try", rawPagesBody); StringAssert.Contains("finally", rawPagesBody); StringAssert.Contains("_activity?.Dispose()", rawPagesBody); + // No catch block in iterator methods (CS1626 restriction) + StringAssert.DoesNotContain("catch", rawPagesBody); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/AnimalOperations.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/AnimalOperations.cs index fec37ba0248..523a82747ea 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/AnimalOperations.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/AnimalOperations.cs @@ -89,8 +89,16 @@ public virtual ClientResult UpdatePetAsAnimal(Animal animal, Cancellatio Argument.AssertNotNull(animal, nameof(animal)); using Activity activity = _activitySource.StartActivity("AnimalOperations.UpdatePetAsAnimal", ActivityKind.Client); - ClientResult result = UpdatePetAsAnimal(animal, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Animal)result, result.GetRawResponse()); + try + { + ClientResult result = UpdatePetAsAnimal(animal, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Animal)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Update a pet as an animal. @@ -103,8 +111,16 @@ public virtual async Task> UpdatePetAsAnimalAsync(Animal an Argument.AssertNotNull(animal, nameof(animal)); using Activity activity = _activitySource.StartActivity("AnimalOperations.UpdatePetAsAnimal", ActivityKind.Client); - ClientResult result = await UpdatePetAsAnimalAsync(animal, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Animal)result, result.GetRawResponse()); + try + { + ClientResult result = await UpdatePetAsAnimalAsync(animal, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Animal)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -159,8 +175,16 @@ public virtual ClientResult UpdateDogAsAnimal(Animal animal, Cancellatio Argument.AssertNotNull(animal, nameof(animal)); using Activity activity = _activitySource.StartActivity("AnimalOperations.UpdateDogAsAnimal", ActivityKind.Client); - ClientResult result = UpdateDogAsAnimal(animal, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Animal)result, result.GetRawResponse()); + try + { + ClientResult result = UpdateDogAsAnimal(animal, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Animal)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Update a dog as an animal. @@ -173,8 +197,16 @@ public virtual async Task> UpdateDogAsAnimalAsync(Animal an Argument.AssertNotNull(animal, nameof(animal)); using Activity activity = _activitySource.StartActivity("AnimalOperations.UpdateDogAsAnimal", ActivityKind.Client); - ClientResult result = await UpdateDogAsAnimalAsync(animal, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Animal)result, result.GetRawResponse()); + try + { + ClientResult result = await UpdateDogAsAnimalAsync(animal, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Animal)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/DogOperations.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/DogOperations.cs index c7265e69bc2..b8f404e70b3 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/DogOperations.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/DogOperations.cs @@ -89,8 +89,16 @@ public virtual ClientResult UpdateDogAsDog(Dog dog, CancellationToken cance Argument.AssertNotNull(dog, nameof(dog)); using Activity activity = _activitySource.StartActivity("DogOperations.UpdateDogAsDog", ActivityKind.Client); - ClientResult result = UpdateDogAsDog(dog, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Dog)result, result.GetRawResponse()); + try + { + ClientResult result = UpdateDogAsDog(dog, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Dog)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Update a dog as a dog. @@ -103,8 +111,16 @@ public virtual async Task> UpdateDogAsDogAsync(Dog dog, Cancel Argument.AssertNotNull(dog, nameof(dog)); using Activity activity = _activitySource.StartActivity("DogOperations.UpdateDogAsDog", ActivityKind.Client); - ClientResult result = await UpdateDogAsDogAsync(dog, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Dog)result, result.GetRawResponse()); + try + { + ClientResult result = await UpdateDogAsDogAsync(dog, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Dog)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Metrics.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Metrics.cs index 53bce735105..167a0453524 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Metrics.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Metrics.cs @@ -101,8 +101,16 @@ public virtual async Task GetWidgetMetricsAsync(string day, Reques public virtual ClientResult GetWidgetMetrics(DaysOfWeekExtensibleEnum day, CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("Metrics.GetWidgetMetrics", ActivityKind.Client); - ClientResult result = GetWidgetMetrics(day.ToString(), cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((GetWidgetMetricsResponse)result, result.GetRawResponse()); + try + { + ClientResult result = GetWidgetMetrics(day.ToString(), cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((GetWidgetMetricsResponse)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Get Widget metrics for given day of week. @@ -112,8 +120,16 @@ public virtual ClientResult GetWidgetMetrics(DaysOfWee public virtual async Task> GetWidgetMetricsAsync(DaysOfWeekExtensibleEnum day, CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("Metrics.GetWidgetMetrics", ActivityKind.Client); - ClientResult result = await GetWidgetMetricsAsync(day.ToString(), cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((GetWidgetMetricsResponse)result, result.GetRawResponse()); + try + { + ClientResult result = await GetWidgetMetricsAsync(day.ToString(), cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((GetWidgetMetricsResponse)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PetOperations.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PetOperations.cs index 75141edef9c..b4b1c64e420 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PetOperations.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PetOperations.cs @@ -89,8 +89,16 @@ public virtual ClientResult UpdatePetAsPet(Pet pet, CancellationToken cance Argument.AssertNotNull(pet, nameof(pet)); using Activity activity = _activitySource.StartActivity("PetOperations.UpdatePetAsPet", ActivityKind.Client); - ClientResult result = UpdatePetAsPet(pet, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Pet)result, result.GetRawResponse()); + try + { + ClientResult result = UpdatePetAsPet(pet, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Pet)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Update a pet as a pet. @@ -103,8 +111,16 @@ public virtual async Task> UpdatePetAsPetAsync(Pet pet, Cancel Argument.AssertNotNull(pet, nameof(pet)); using Activity activity = _activitySource.StartActivity("PetOperations.UpdatePetAsPet", ActivityKind.Client); - ClientResult result = await UpdatePetAsPetAsync(pet, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Pet)result, result.GetRawResponse()); + try + { + ClientResult result = await UpdatePetAsPetAsync(pet, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Pet)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -159,8 +175,16 @@ public virtual ClientResult UpdateDogAsPet(Pet pet, CancellationToken cance Argument.AssertNotNull(pet, nameof(pet)); using Activity activity = _activitySource.StartActivity("PetOperations.UpdateDogAsPet", ActivityKind.Client); - ClientResult result = UpdateDogAsPet(pet, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Pet)result, result.GetRawResponse()); + try + { + ClientResult result = UpdateDogAsPet(pet, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Pet)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Update a dog as a pet. @@ -173,8 +197,16 @@ public virtual async Task> UpdateDogAsPetAsync(Pet pet, Cancel Argument.AssertNotNull(pet, nameof(pet)); using Activity activity = _activitySource.StartActivity("PetOperations.UpdateDogAsPet", ActivityKind.Client); - ClientResult result = await UpdateDogAsPetAsync(pet, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Pet)result, result.GetRawResponse()); + try + { + ClientResult result = await UpdateDogAsPetAsync(pet, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Pet)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PlantOperations.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PlantOperations.cs index c006101ba72..4fb0d30d0d1 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PlantOperations.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/PlantOperations.cs @@ -77,8 +77,16 @@ public virtual async Task GetTreeAsync(RequestOptions options) public virtual ClientResult GetTree(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("PlantOperations.GetTree", ActivityKind.Client); - ClientResult result = GetTree(cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + try + { + ClientResult result = GetTree(cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Get a tree as a plant. @@ -87,8 +95,16 @@ public virtual ClientResult GetTree(CancellationToken cancellationToken = public virtual async Task> GetTreeAsync(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("PlantOperations.GetTree", ActivityKind.Client); - ClientResult result = await GetTreeAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + try + { + ClientResult result = await GetTreeAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -131,8 +147,16 @@ public virtual async Task GetTreeAsJsonAsync(RequestOptions option public virtual ClientResult GetTreeAsJson(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("PlantOperations.GetTreeAsJson", ActivityKind.Client); - ClientResult result = GetTreeAsJson(cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + try + { + ClientResult result = GetTreeAsJson(cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Get a tree as a plant. @@ -141,8 +165,16 @@ public virtual ClientResult GetTreeAsJson(CancellationToken cancellationTo public virtual async Task> GetTreeAsJsonAsync(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("PlantOperations.GetTreeAsJson", ActivityKind.Client); - ClientResult result = await GetTreeAsJsonAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + try + { + ClientResult result = await GetTreeAsJsonAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -197,9 +229,17 @@ public virtual ClientResult UpdateTree(Tree tree, CancellationToken cancel Argument.AssertNotNull(tree, nameof(tree)); using Activity activity = _activitySource.StartActivity("PlantOperations.UpdateTree", ActivityKind.Client); - using BinaryContent content = tree.ToBinaryContent("X"); - ClientResult result = UpdateTree(content, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + try + { + using BinaryContent content = tree.ToBinaryContent("X"); + ClientResult result = UpdateTree(content, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Update a tree as a plant. @@ -212,9 +252,17 @@ public virtual async Task> UpdateTreeAsync(Tree tree, Cancell Argument.AssertNotNull(tree, nameof(tree)); using Activity activity = _activitySource.StartActivity("PlantOperations.UpdateTree", ActivityKind.Client); - using BinaryContent content = tree.ToBinaryContent("X"); - ClientResult result = await UpdateTreeAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + try + { + using BinaryContent content = tree.ToBinaryContent("X"); + ClientResult result = await UpdateTreeAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -269,9 +317,17 @@ public virtual ClientResult UpdateTreeAsJson(Tree tree, CancellationToken Argument.AssertNotNull(tree, nameof(tree)); using Activity activity = _activitySource.StartActivity("PlantOperations.UpdateTreeAsJson", ActivityKind.Client); - using BinaryContent content = tree.ToBinaryContent("J"); - ClientResult result = UpdateTreeAsJson(content, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + try + { + using BinaryContent content = tree.ToBinaryContent("J"); + ClientResult result = UpdateTreeAsJson(content, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Update a tree as a plant. @@ -284,9 +340,17 @@ public virtual async Task> UpdateTreeAsJsonAsync(Tree tree, C Argument.AssertNotNull(tree, nameof(tree)); using Activity activity = _activitySource.StartActivity("PlantOperations.UpdateTreeAsJson", ActivityKind.Client); - using BinaryContent content = tree.ToBinaryContent("J"); - ClientResult result = await UpdateTreeAsJsonAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + try + { + using BinaryContent content = tree.ToBinaryContent("J"); + ClientResult result = await UpdateTreeAsJsonAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Tree)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs index a897e72c58f..4328acff449 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs @@ -160,8 +160,16 @@ public virtual ClientResult SayHi(string headParameter, string queryParam Argument.AssertNotNullOrEmpty(queryParameter, nameof(queryParameter)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.SayHi", ActivityKind.Client); - ClientResult result = SayHi(headParameter, queryParameter, optionalQuery, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = SayHi(headParameter, queryParameter, optionalQuery, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Return hi. @@ -178,8 +186,16 @@ public virtual async Task> SayHiAsync(string headParameter, Argument.AssertNotNullOrEmpty(queryParameter, nameof(queryParameter)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.SayHi", ActivityKind.Client); - ClientResult result = await SayHiAsync(headParameter, queryParameter, optionalQuery, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = await SayHiAsync(headParameter, queryParameter, optionalQuery, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -249,8 +265,16 @@ public virtual ClientResult HelloAgain(string p2, string p1, Rou Argument.AssertNotNull(action, nameof(action)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloAgain", ActivityKind.Client); - ClientResult result = HelloAgain(p2, p1, action, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); + try + { + ClientResult result = HelloAgain(p2, p1, action, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Return hi again. @@ -268,8 +292,16 @@ public virtual async Task> HelloAgainAsync(string p Argument.AssertNotNull(action, nameof(action)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloAgain", ActivityKind.Client); - ClientResult result = await HelloAgainAsync(p2, p1, action, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); + try + { + ClientResult result = await HelloAgainAsync(p2, p1, action, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -334,8 +366,16 @@ public virtual ClientResult NoContentType(Wrapper info, Cancella Argument.AssertNotNull(info, nameof(info)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.NoContentType", ActivityKind.Client); - ClientResult result = NoContentType(info.P2, info.P1, info.Action, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); + try + { + ClientResult result = NoContentType(info.P2, info.P1, info.Action, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Return hi again. @@ -348,8 +388,16 @@ public virtual async Task> NoContentTypeAsync(Wrapp Argument.AssertNotNull(info, nameof(info)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.NoContentType", ActivityKind.Client); - ClientResult result = await NoContentTypeAsync(info.P2, info.P1, info.Action, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); + try + { + ClientResult result = await NoContentTypeAsync(info.P2, info.P1, info.Action, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((RoundTripModel)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -392,8 +440,16 @@ public virtual async Task HelloDemo2Async(RequestOptions options) public virtual ClientResult HelloDemo2(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloDemo2", ActivityKind.Client); - ClientResult result = HelloDemo2(cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = HelloDemo2(cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Return hi in demo2. @@ -402,8 +458,16 @@ public virtual ClientResult HelloDemo2(CancellationToken cancellationToke public virtual async Task> HelloDemo2Async(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloDemo2", ActivityKind.Client); - ClientResult result = await HelloDemo2Async(cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = await HelloDemo2Async(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -458,8 +522,16 @@ public virtual ClientResult CreateLiteral(Thing body, CancellationToken c Argument.AssertNotNull(body, nameof(body)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.CreateLiteral", ActivityKind.Client); - ClientResult result = CreateLiteral(body, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = CreateLiteral(body, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Create with literal value. @@ -472,8 +544,16 @@ public virtual async Task> CreateLiteralAsync(Thing body, Ca Argument.AssertNotNull(body, nameof(body)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.CreateLiteral", ActivityKind.Client); - ClientResult result = await CreateLiteralAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = await CreateLiteralAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -516,8 +596,16 @@ public virtual async Task HelloLiteralAsync(RequestOptions options public virtual ClientResult HelloLiteral(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloLiteral", ActivityKind.Client); - ClientResult result = HelloLiteral(cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = HelloLiteral(cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Send literal parameters. @@ -526,8 +614,16 @@ public virtual ClientResult HelloLiteral(CancellationToken cancellationTo public virtual async Task> HelloLiteralAsync(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HelloLiteral", ActivityKind.Client); - ClientResult result = await HelloLiteralAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = await HelloLiteralAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -573,8 +669,16 @@ public virtual async Task TopActionAsync(DateTimeOffset action, Re public virtual ClientResult TopAction(DateTimeOffset action, CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.TopAction", ActivityKind.Client); - ClientResult result = TopAction(action, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = TopAction(action, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// top level method. @@ -584,8 +688,16 @@ public virtual ClientResult TopAction(DateTimeOffset action, Cancellation public virtual async Task> TopActionAsync(DateTimeOffset action, CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.TopAction", ActivityKind.Client); - ClientResult result = await TopActionAsync(action, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = await TopActionAsync(action, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -735,27 +847,35 @@ public virtual ClientResult AnonymousBody(string name, BinaryData require Argument.AssertNotNullOrEmpty(propertyWithSpecialDocs, nameof(propertyWithSpecialDocs)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.AnonymousBody", ActivityKind.Client); - Thing spreadModel = new Thing( - default, - requiredUnion, - "accept", - requiredNullableString, - optionalNullableString, - 123, - 1.23F, - false, - optionalLiteralString, - requiredNullableLiteralString, - optionalLiteralInt, - optionalLiteralFloat, - optionalLiteralBool, - requiredBadDescription, - optionalNullableList?.ToList() as IList ?? new ChangeTrackingList(), - requiredNullableList?.ToList() as IList ?? new ChangeTrackingList(), - propertyWithSpecialDocs, - default); - ClientResult result = AnonymousBody(spreadModel, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + Thing spreadModel = new Thing( + default, + requiredUnion, + "accept", + requiredNullableString, + optionalNullableString, + 123, + 1.23F, + false, + optionalLiteralString, + requiredNullableLiteralString, + optionalLiteralInt, + optionalLiteralFloat, + optionalLiteralBool, + requiredBadDescription, + optionalNullableList?.ToList() as IList ?? new ChangeTrackingList(), + requiredNullableList?.ToList() as IList ?? new ChangeTrackingList(), + propertyWithSpecialDocs, + default); + ClientResult result = AnonymousBody(spreadModel, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// body parameter without body decorator. @@ -787,27 +907,35 @@ public virtual async Task> AnonymousBodyAsync(string name, B Argument.AssertNotNullOrEmpty(propertyWithSpecialDocs, nameof(propertyWithSpecialDocs)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.AnonymousBody", ActivityKind.Client); - Thing spreadModel = new Thing( - default, - requiredUnion, - "accept", - requiredNullableString, - optionalNullableString, - 123, - 1.23F, - false, - optionalLiteralString, - requiredNullableLiteralString, - optionalLiteralInt, - optionalLiteralFloat, - optionalLiteralBool, - requiredBadDescription, - optionalNullableList?.ToList() as IList ?? new ChangeTrackingList(), - requiredNullableList?.ToList() as IList ?? new ChangeTrackingList(), - propertyWithSpecialDocs, - default); - ClientResult result = await AnonymousBodyAsync(spreadModel, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + Thing spreadModel = new Thing( + default, + requiredUnion, + "accept", + requiredNullableString, + optionalNullableString, + 123, + 1.23F, + false, + optionalLiteralString, + requiredNullableLiteralString, + optionalLiteralInt, + optionalLiteralFloat, + optionalLiteralBool, + requiredBadDescription, + optionalNullableList?.ToList() as IList ?? new ChangeTrackingList(), + requiredNullableList?.ToList() as IList ?? new ChangeTrackingList(), + propertyWithSpecialDocs, + default); + ClientResult result = await AnonymousBodyAsync(spreadModel, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -863,9 +991,17 @@ public virtual ClientResult FriendlyModel(string name, CancellationToken Argument.AssertNotNullOrEmpty(name, nameof(name)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.FriendlyModel", ActivityKind.Client); - Friend spreadModel = new Friend(name, default); - ClientResult result = FriendlyModel(spreadModel, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Friend)result, result.GetRawResponse()); + try + { + Friend spreadModel = new Friend(name, default); + ClientResult result = FriendlyModel(spreadModel, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Friend)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Model can have its friendly name. @@ -879,9 +1015,17 @@ public virtual async Task> FriendlyModelAsync(string name, Argument.AssertNotNullOrEmpty(name, nameof(name)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.FriendlyModel", ActivityKind.Client); - Friend spreadModel = new Friend(name, default); - ClientResult result = await FriendlyModelAsync(spreadModel, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Friend)result, result.GetRawResponse()); + try + { + Friend spreadModel = new Friend(name, default); + ClientResult result = await FriendlyModelAsync(spreadModel, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Friend)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -924,7 +1068,15 @@ public virtual async Task AddTimeHeaderAsync(RequestOptions option public virtual ClientResult AddTimeHeader(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.AddTimeHeader", ActivityKind.Client); - return AddTimeHeader(cancellationToken.ToRequestOptions()); + try + { + return AddTimeHeader(cancellationToken.ToRequestOptions()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// AddTimeHeader. @@ -933,7 +1085,15 @@ public virtual ClientResult AddTimeHeader(CancellationToken cancellationToken = public virtual async Task AddTimeHeaderAsync(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.AddTimeHeader", ActivityKind.Client); - return await AddTimeHeaderAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + try + { + return await AddTimeHeaderAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -989,9 +1149,17 @@ public virtual ClientResult ProjectedNameModel(string otherN Argument.AssertNotNullOrEmpty(otherName, nameof(otherName)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.ProjectedNameModel", ActivityKind.Client); - RenamedModelCustom spreadModel = new RenamedModelCustom(default, otherName); - ClientResult result = ProjectedNameModel(spreadModel, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((RenamedModelCustom)result, result.GetRawResponse()); + try + { + RenamedModelCustom spreadModel = new RenamedModelCustom(default, otherName); + ClientResult result = ProjectedNameModel(spreadModel, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((RenamedModelCustom)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Model can have its projected name. @@ -1005,9 +1173,17 @@ public virtual async Task> ProjectedNameModelAs Argument.AssertNotNullOrEmpty(otherName, nameof(otherName)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.ProjectedNameModel", ActivityKind.Client); - RenamedModelCustom spreadModel = new RenamedModelCustom(default, otherName); - ClientResult result = await ProjectedNameModelAsync(spreadModel, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((RenamedModelCustom)result, result.GetRawResponse()); + try + { + RenamedModelCustom spreadModel = new RenamedModelCustom(default, otherName); + ClientResult result = await ProjectedNameModelAsync(spreadModel, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((RenamedModelCustom)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -1050,8 +1226,16 @@ public virtual async Task ReturnsAnonymousModelAsync(RequestOption public virtual ClientResult ReturnsAnonymousModel(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.ReturnsAnonymousModel", ActivityKind.Client); - ClientResult result = ReturnsAnonymousModel(cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((ReturnsAnonymousModelResponse)result, result.GetRawResponse()); + try + { + ClientResult result = ReturnsAnonymousModel(cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((ReturnsAnonymousModelResponse)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// return anonymous model. @@ -1060,8 +1244,16 @@ public virtual ClientResult ReturnsAnonymousModel public virtual async Task> ReturnsAnonymousModelAsync(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.ReturnsAnonymousModel", ActivityKind.Client); - ClientResult result = await ReturnsAnonymousModelAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((ReturnsAnonymousModelResponse)result, result.GetRawResponse()); + try + { + ClientResult result = await ReturnsAnonymousModelAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((ReturnsAnonymousModelResponse)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -1119,8 +1311,16 @@ public virtual ClientResult GetUnknownValue(string accept, CancellationT Argument.AssertNotNullOrEmpty(accept, nameof(accept)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetUnknownValue", ActivityKind.Client); - ClientResult result = GetUnknownValue(accept, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue(result.GetRawResponse().Content.ToString(), result.GetRawResponse()); + try + { + ClientResult result = GetUnknownValue(accept, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue(result.GetRawResponse().Content.ToString(), result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// get extensible enum. @@ -1134,8 +1334,16 @@ public virtual async Task> GetUnknownValueAsync(string acce Argument.AssertNotNullOrEmpty(accept, nameof(accept)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetUnknownValue", ActivityKind.Client); - ClientResult result = await GetUnknownValueAsync(accept, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue(result.GetRawResponse().Content.ToString(), result.GetRawResponse()); + try + { + ClientResult result = await GetUnknownValueAsync(accept, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue(result.GetRawResponse().Content.ToString(), result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -1190,8 +1398,16 @@ public virtual ClientResult InternalProtocol(Thing body, CancellationToke Argument.AssertNotNull(body, nameof(body)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.InternalProtocol", ActivityKind.Client); - ClientResult result = InternalProtocol(body, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = InternalProtocol(body, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// When set protocol false and convenient true, then the protocol method should be internal. @@ -1204,8 +1420,16 @@ public virtual async Task> InternalProtocolAsync(Thing body, Argument.AssertNotNull(body, nameof(body)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.InternalProtocol", ActivityKind.Client); - ClientResult result = await InternalProtocolAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + try + { + ClientResult result = await InternalProtocolAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((Thing)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -1248,7 +1472,15 @@ public virtual async Task StillConvenientAsync(RequestOptions opti public virtual ClientResult StillConvenient(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.StillConvenient", ActivityKind.Client); - return StillConvenient(cancellationToken.ToRequestOptions()); + try + { + return StillConvenient(cancellationToken.ToRequestOptions()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// When set protocol false and convenient true, the convenient method should be generated even it has the same signature as protocol one. @@ -1257,7 +1489,15 @@ public virtual ClientResult StillConvenient(CancellationToken cancellationToken public virtual async Task StillConvenientAsync(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.StillConvenient", ActivityKind.Client); - return await StillConvenientAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + try + { + return await StillConvenientAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -1315,7 +1555,15 @@ public virtual ClientResult HeadAsBoolean(string id, CancellationToken cancellat Argument.AssertNotNullOrEmpty(id, nameof(id)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HeadAsBoolean", ActivityKind.Client); - return HeadAsBoolean(id, cancellationToken.ToRequestOptions()); + try + { + return HeadAsBoolean(id, cancellationToken.ToRequestOptions()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// head as boolean. @@ -1329,7 +1577,15 @@ public virtual async Task HeadAsBooleanAsync(string id, Cancellati Argument.AssertNotNullOrEmpty(id, nameof(id)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.HeadAsBoolean", ActivityKind.Client); - return await HeadAsBooleanAsync(id, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + try + { + return await HeadAsBooleanAsync(id, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -1387,7 +1643,15 @@ public virtual ClientResult WithApiVersion(string p1, CancellationToken cancella Argument.AssertNotNullOrEmpty(p1, nameof(p1)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.WithApiVersion", ActivityKind.Client); - return WithApiVersion(p1, cancellationToken.ToRequestOptions()); + try + { + return WithApiVersion(p1, cancellationToken.ToRequestOptions()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Return hi again. @@ -1401,7 +1665,15 @@ public virtual async Task WithApiVersionAsync(string p1, Cancellat Argument.AssertNotNullOrEmpty(p1, nameof(p1)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.WithApiVersion", ActivityKind.Client); - return await WithApiVersionAsync(p1, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + try + { + return await WithApiVersionAsync(p1, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -1728,7 +2000,15 @@ public virtual ClientResult EmbeddedParameters(ModelWithEmbeddedNonBodyParameter Argument.AssertNotNull(body, nameof(body)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.EmbeddedParameters", ActivityKind.Client); - return EmbeddedParameters(body.RequiredHeader, body.RequiredQuery, body, body.OptionalHeader, body.OptionalQuery, cancellationToken.ToRequestOptions()); + try + { + return EmbeddedParameters(body.RequiredHeader, body.RequiredQuery, body, body.OptionalHeader, body.OptionalQuery, cancellationToken.ToRequestOptions()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// An operation with embedded parameters within the body. @@ -1741,7 +2021,15 @@ public virtual async Task EmbeddedParametersAsync(ModelWithEmbedde Argument.AssertNotNull(body, nameof(body)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.EmbeddedParameters", ActivityKind.Client); - return await EmbeddedParametersAsync(body.RequiredHeader, body.RequiredQuery, body, body.OptionalHeader, body.OptionalQuery, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + try + { + return await EmbeddedParametersAsync(body.RequiredHeader, body.RequiredQuery, body, body.OptionalHeader, body.OptionalQuery, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -1796,7 +2084,15 @@ public virtual ClientResult DynamicModelOperation(DynamicModel body, Cancellatio Argument.AssertNotNull(body, nameof(body)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.DynamicModelOperation", ActivityKind.Client); - return DynamicModelOperation(body, cancellationToken.ToRequestOptions()); + try + { + return DynamicModelOperation(body, cancellationToken.ToRequestOptions()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// An operation with a dynamic model. @@ -1809,7 +2105,15 @@ public virtual async Task DynamicModelOperationAsync(DynamicModel Argument.AssertNotNull(body, nameof(body)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.DynamicModelOperation", ActivityKind.Client); - return await DynamicModelOperationAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + try + { + return await DynamicModelOperationAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -1852,8 +2156,16 @@ public virtual async Task GetXmlAdvancedModelAsync(RequestOptions public virtual ClientResult GetXmlAdvancedModel(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetXmlAdvancedModel", ActivityKind.Client); - ClientResult result = GetXmlAdvancedModel(cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); + try + { + ClientResult result = GetXmlAdvancedModel(cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Get an advanced XML model with various property types. @@ -1862,8 +2174,16 @@ public virtual ClientResult GetXmlAdvancedModel(CancellationTo public virtual async Task> GetXmlAdvancedModelAsync(CancellationToken cancellationToken = default) { using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetXmlAdvancedModel", ActivityKind.Client); - ClientResult result = await GetXmlAdvancedModelAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); + try + { + ClientResult result = await GetXmlAdvancedModelAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// @@ -1918,8 +2238,16 @@ public virtual ClientResult UpdateXmlAdvancedModel(XmlAdvanced Argument.AssertNotNull(body, nameof(body)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.UpdateXmlAdvancedModel", ActivityKind.Client); - ClientResult result = UpdateXmlAdvancedModel(body, cancellationToken.ToRequestOptions()); - return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); + try + { + ClientResult result = UpdateXmlAdvancedModel(body, cancellationToken.ToRequestOptions()); + return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Update an advanced XML model with various property types. @@ -1932,8 +2260,16 @@ public virtual async Task> UpdateXmlAdvancedModel Argument.AssertNotNull(body, nameof(body)); using Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.UpdateXmlAdvancedModel", ActivityKind.Client); - ClientResult result = await UpdateXmlAdvancedModelAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); - return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); + try + { + ClientResult result = await UpdateXmlAdvancedModelAsync(body, cancellationToken.ToRequestOptions()).ConfigureAwait(false); + return ClientResult.FromValue((XmlAdvancedModel)result, result.GetRawResponse()); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } } /// Initializes a new instance of AnimalOperations. From 480bfbe2aed5238cc4a2ee98d216da478d4f7fa4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 5 Mar 2026 02:55:26 +0000 Subject: [PATCH 6/6] feat: start activity per page request in paging collections (Azure pattern) Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> --- .../Providers/CollectionResultDefinition.cs | 128 +++++++++++------- .../Providers/ScmMethodProviderCollection.cs | 21 +-- .../ScmMethodProviderCollectionTests.cs | 48 ++++--- ...hContinuationTokenAsyncCollectionResult.cs | 51 ++++--- ...ntinuationTokenAsyncCollectionResultOfT.cs | 50 ++++--- ...etWithContinuationTokenCollectionResult.cs | 50 ++++--- ...ithContinuationTokenCollectionResultOfT.cs | 50 ++++--- ...okenHeaderResponseAsyncCollectionResult.cs | 57 ++++---- ...nHeaderResponseAsyncCollectionResultOfT.cs | 56 ++++---- ...tionTokenHeaderResponseCollectionResult.cs | 56 ++++---- ...nTokenHeaderResponseCollectionResultOfT.cs | 56 ++++---- ...entGetWithNextLinkAsyncCollectionResult.cs | 51 ++++--- ...GetWithNextLinkAsyncCollectionResultOfT.cs | 50 ++++--- ...ecClientGetWithNextLinkCollectionResult.cs | 50 ++++--- ...lientGetWithNextLinkCollectionResultOfT.cs | 50 ++++--- ...lientGetWithPagingAsyncCollectionResult.cs | 36 +++-- ...ntGetWithPagingAsyncCollectionResultOfT.cs | 35 +++-- ...SpecClientGetWithPagingCollectionResult.cs | 35 +++-- ...cClientGetWithPagingCollectionResultOfT.cs | 35 +++-- ...WithStringNextLinkAsyncCollectionResult.cs | 53 +++++--- ...hStringNextLinkAsyncCollectionResultOfT.cs | 52 ++++--- ...ntGetWithStringNextLinkCollectionResult.cs | 52 ++++--- ...etWithStringNextLinkCollectionResultOfT.cs | 52 ++++--- .../src/Generated/SampleTypeSpecClient.cs | 30 ++-- 24 files changed, 697 insertions(+), 507 deletions(-) diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs index 39a8a31c72e..5ff2dd79808 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/CollectionResultDefinition.cs @@ -28,7 +28,9 @@ public class CollectionResultDefinition : TypeProvider protected bool IsAsync { get; } protected ClientProvider Client { get; } protected FieldProvider ClientField { get; } - protected FieldProvider? ActivityField { get; } + protected FieldProvider? ActivitySourceField { get; } + private string? _executePageRequestMethodName; + private string ExecutePageRequestMethodName => _executePageRequestMethodName ??= IsAsync ? "ExecutePageRequestAsync" : "ExecutePageRequest"; protected InputOperation Operation { get; } protected InputPagingServiceMetadata Paging { get; } @@ -78,10 +80,10 @@ public CollectionResultDefinition(ClientProvider client, InputPagingServiceMetho if (Client.ActivitySourceField != null) { - ActivityField = new FieldProvider( + ActivitySourceField = new FieldProvider( FieldModifiers.Private | FieldModifiers.ReadOnly, - new CSharpType(typeof(Activity), isNullable: true), - "_activity", + new CSharpType(typeof(ActivitySource), isNullable: true), + "_activitySource", this); } @@ -200,8 +202,8 @@ protected override TypeSignatureModifiers BuildDeclarationModifiers() => TypeSignatureModifiers.Internal | TypeSignatureModifiers.Partial | TypeSignatureModifiers.Class; protected override FieldProvider[] BuildFields() - => ActivityField != null - ? [ClientField, ActivityField, .. RequestFields] + => ActivitySourceField != null + ? [ClientField, ActivitySourceField, .. RequestFields] : [ClientField, .. RequestFields]; protected override CSharpType[] BuildImplements() => @@ -221,15 +223,15 @@ protected override ConstructorProvider[] BuildConstructors() Client.Type); var parameters = new List { clientParameter }; parameters.AddRange(CreateRequestParameters); - ParameterProvider? activityParameter = null; - if (ActivityField != null) + ParameterProvider? activitySourceParameter = null; + if (ActivitySourceField != null) { - activityParameter = new ParameterProvider( - "activity", - $"The activity for distributed tracing.", - new CSharpType(typeof(Activity), isNullable: true), + activitySourceParameter = new ParameterProvider( + "activitySource", + $"The activity source for distributed tracing.", + new CSharpType(typeof(ActivitySource), isNullable: true), defaultValue: Null); - parameters.Add(activityParameter); + parameters.Add(activitySourceParameter); } return [ @@ -239,7 +241,7 @@ protected override ConstructorProvider[] BuildConstructors() $"Initializes a new instance of {Name}, which is used to iterate over the pages of a collection.", MethodSignatureModifiers.Public, parameters), - BuildConstructorBody(clientParameter, activityParameter), + BuildConstructorBody(clientParameter, activitySourceParameter), this) ]; } @@ -254,7 +256,7 @@ protected ValueExpression BuildGetPropertyExpression(IReadOnlyList segme return ResponseModel.GetPropertyExpression(responseModel, segments); } - private MethodBodyStatement[] BuildConstructorBody(ParameterProvider clientParameter, ParameterProvider? activityParameter) + private MethodBodyStatement[] BuildConstructorBody(ParameterProvider clientParameter, ParameterProvider? activitySourceParameter) { var statements = new List(CreateRequestParameters.Count + 1); @@ -267,9 +269,9 @@ private MethodBodyStatement[] BuildConstructorBody(ParameterProvider clientParam statements.Add(field.Assign(parameter).Terminate()); } - if (ActivityField != null && activityParameter != null) + if (ActivitySourceField != null && activitySourceParameter != null) { - statements.Add(ActivityField.Assign(activityParameter).Terminate()); + statements.Add(ActivitySourceField.Assign(activitySourceParameter).Terminate()); } return statements.ToArray(); @@ -284,21 +286,6 @@ protected override MethodProvider[] BuildMethods() (_, not null) => BuildGetRawPagesForContinuationToken() }; - // Wrap with try-finally to dispose the activity when enumeration completes. - // Note: A catch clause cannot be added here because C# does not allow 'yield return' inside - // a try block that has a catch clause (CS1626). Exception tracking is handled in the - // non-paging convenience methods instead. - if (ActivityField != null) - { - getRawPagesMethodBody = - [ - new TryCatchFinallyStatement( - new TryExpression(getRawPagesMethodBody), - [], - new FinallyExpression(ActivityField.AsValueExpression.NullConditional().Invoke(nameof(IDisposable.Dispose), []).Terminate())) - ]; - } - var methods = new List { new MethodProvider( @@ -329,6 +316,11 @@ protected override MethodProvider[] BuildMethods() this) }; + if (ActivitySourceField != null) + { + methods.Add(BuildExecutePageRequestMethod()); + } + if (ItemModelType != null) { methods.Add(new MethodProvider( @@ -349,6 +341,45 @@ protected override MethodProvider[] BuildMethods() return methods.ToArray(); } + private MethodProvider BuildExecutePageRequestMethod() + { + var messageParam = new ParameterProvider("message", $"The pipeline message.", new CSharpType(typeof(PipelineMessage))); + string activityName = $"{Client.Name}.{Operation.Name.ToIdentifierName()}"; + CSharpType returnType = IsAsync + ? new CSharpType(typeof(Task<>), typeof(ClientResult)) + : new CSharpType(typeof(ClientResult)); + MethodSignatureModifiers modifiers = IsAsync + ? MethodSignatureModifiers.Private | MethodSignatureModifiers.Async + : MethodSignatureModifiers.Private; + + // using Activity? activity = _activitySource?.StartActivity("...", ActivityKind.Client); + var activityDecl = UsingDeclare( + "activity", + new CSharpType(typeof(Activity), isNullable: true), + ActivitySourceField!.AsValueExpression.NullConditional().Invoke( + nameof(ActivitySource.StartActivity), + [Literal(activityName), FrameworkEnumValue(ActivityKind.Client)]), + out var activityVar); + + // try { return ...; } catch (Exception ex) { activity?.SetStatus(ActivityStatusCode.Error, ex.Message); throw; } + var pipelineResponse = ScmCodeModelGenerator.Instance.TypeFactory.ClientResponseApi.ToExpression().FromResponse( + ClientField.Property("Pipeline").ToApi().ProcessMessage( + messageParam.ToApi(), + RequestOptionsField.AsValueExpression.ToApi(), + IsAsync)).ToApi(); + var exDecl = new DeclarationExpression(typeof(Exception), "ex", out var exVar); + var catchBlock = new CatchExpression(exDecl, + activityVar.NullConditional().Invoke(nameof(Activity.SetStatus), + [FrameworkEnumValue(ActivityStatusCode.Error), exVar.Property(nameof(Exception.Message))]).Terminate(), + Throw()); + + MethodBodyStatement[] body = [activityDecl, new TryCatchFinallyStatement(new TryExpression(Return(pipelineResponse)), catchBlock)]; + return new MethodProvider( + new MethodSignature(ExecutePageRequestMethodName, null, modifiers, returnType, null, [messageParam]), + body, + this); + } + private MethodBodyStatement[] BuildGetValuesFromPages() { var items = GetPropertyExpression(Paging.ItemPropertySegments, PageParameter.AsVariable()); @@ -425,6 +456,19 @@ private MethodBodyStatement[] BuildGetContinuationToken() } } + private ClientResponseApi InvokePageRequest(ScopedApi message) + { + if (ActivitySourceField != null) + { + return This.Invoke(ExecutePageRequestMethodName, [message], IsAsync).ToApi(); + } + return ScmCodeModelGenerator.Instance.TypeFactory.ClientResponseApi.ToExpression().FromResponse( + ClientField.Property("Pipeline").ToApi().ProcessMessage( + message.ToApi(), + RequestOptionsField.AsValueExpression.ToApi(), + IsAsync)).ToApi(); + } + private MethodBodyStatement[] BuildGetRawPagesForNextLink() { var nextPageVariable = new VariableExpression(typeof(Uri), "nextPageUri"); @@ -444,11 +488,7 @@ private MethodBodyStatement[] BuildGetRawPagesForNextLink() { Declare( "result", - ScmCodeModelGenerator.Instance.TypeFactory.ClientResponseApi.ToExpression().FromResponse( - ClientField.Property("Pipeline").ToApi().ProcessMessage( - message.ToApi(), - RequestOptionsField.AsValueExpression.ToApi(), - IsAsync)).ToApi(), + InvokePageRequest(message), out ClientResponseApi result), // Yield return result @@ -483,11 +523,7 @@ private MethodBodyStatement[] BuildGetRawPagesForContinuationToken() { Declare( "result", - ScmCodeModelGenerator.Instance.TypeFactory.ClientResponseApi.ToExpression().FromResponse( - ClientField.Property("Pipeline").ToApi().ProcessMessage( - message.ToApi(), - RequestOptionsField.AsValueExpression.ToApi(), - IsAsync)).ToApi(), + InvokePageRequest(message), out ClientResponseApi result), // Yield return result @@ -509,16 +545,12 @@ private MethodBodyStatement[] BuildGetRawPagesForSingle() "message", InvokeCreateInitialRequest(), out ScopedApi m); - var pipelineResponse = ScmCodeModelGenerator.Instance.TypeFactory.ClientResponseApi.ToExpression().FromResponse( - ClientField.Property("Pipeline").ToApi().ProcessMessage( - m.ToApi(), - RequestOptionsField.AsValueExpression.ToApi(), - IsAsync)).ToApi(); + var pageRequestResult = InvokePageRequest(m); return [ pipelineMessageDeclaration, // Yield return result - YieldReturn(pipelineResponse), + YieldReturn(pageRequestResult), ]; } diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs index 113e74977c7..1db3ff55207 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs @@ -129,21 +129,10 @@ private ScmMethodProvider BuildConvenienceMethod(MethodProvider protocolMethod, if (_pagingServiceMethod != null) { collection = ScmCodeModelGenerator.Instance.TypeFactory.ClientResponseApi.CreateClientCollectionResultDefinition(Client, _pagingServiceMethod, responseBodyType, isAsync); - // For paging, declare activity WITHOUT `using` so it's passed to the collection and disposed there via try-finally - VariableExpression? activityVar = null; - MethodBodyStatement[] pagingPrefix = []; - if (Client.ActivitySourceField != null) - { - var activityDecl = Declare( - "activity", - new CSharpType(typeof(Activity), isNullable: true), - Client.ActivitySourceField.Invoke( - nameof(ActivitySource.StartActivity), - [Literal($"{Client.Name}.{ServiceMethod.Name}"), FrameworkEnumValue(ActivityKind.Client)]), - out activityVar); - pagingPrefix = [activityDecl]; - } - methodBody = [.. pagingPrefix, .. GetPagingMethodBody(collection, ConvenienceMethodParameters, true, activityVar)]; + // Pass the ActivitySource to the collection so each page request can start its own activity. + // No activity is started here - tracing is per-page in the collection's ExecutePageRequest helper. + ValueExpression? activitySourceExpr = Client.ActivitySourceField?.AsValueExpression; + methodBody = [.. GetPagingMethodBody(collection, ConvenienceMethodParameters, true, activitySourceExpr)]; } else if (responseBodyType is null) { @@ -181,7 +170,7 @@ .. GetStackVariablesForReturnValueConversion(result, responseBodyType, isAsync, } // Prepend activity instrumentation statement if enabled (non-paging methods only; - // paging methods handle the activity disposal in the collection via try-finally) + // paging methods start a new activity per page request in ExecutePageRequest/ExecutePageRequestAsync) if (Client.ActivitySourceField != null && _pagingServiceMethod == null) { var activityStatement = UsingDeclare( diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs index 0e25b508a7f..d93d53ff423 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs @@ -2106,15 +2106,16 @@ public void TracingIsAddedToPagingConvenienceMethod() Assert.IsNotNull(convenienceMethod); var methodBody = convenienceMethod!.BodyStatements!.ToDisplayString(); - // Paging methods: activity declared without `using` so it's passed to the collection - StringAssert.Contains("_activitySource.StartActivity(\"TestClient.GetCats\"", methodBody); - // Should NOT use `using` for paging - the activity is disposed inside the collection via try-finally - StringAssert.DoesNotContain("using global::System.Diagnostics.Activity activity", methodBody); - StringAssert.Contains("global::System.Diagnostics.Activity activity =", methodBody); + // Paging methods: ActivitySource is passed to the collection, no activity started here. + // Each page request starts its own activity inside ExecutePageRequest in the collection. + StringAssert.DoesNotContain("StartActivity", methodBody); + StringAssert.DoesNotContain("Activity activity", methodBody); + // The _activitySource field should be passed to the collection constructor + StringAssert.Contains("_activitySource", methodBody); } [Test] - public void TracingInPagingCollectionDisposesActivityViaFinally() + public void TracingInPagingCollectionStartsActivityPerPage() { var pagingMetadata = InputFactory.PagingMetadata(["items"], null, null); var inputModel = InputFactory.Model("cat", properties: @@ -2142,26 +2143,35 @@ public void TracingInPagingCollectionDisposesActivityViaFinally() .FirstOrDefault(); Assert.IsNotNull(collectionResultDefinition); - // CollectionResultDefinition should have an _activity field - var activityField = collectionResultDefinition!.Fields.FirstOrDefault(f => f.Name == "_activity"); - Assert.IsNotNull(activityField, "CollectionResultDefinition should have an _activity field"); + // CollectionResultDefinition should have an _activitySource field (not _activity) + var activitySourceField = collectionResultDefinition!.Fields.FirstOrDefault(f => f.Name == "_activitySource"); + Assert.IsNotNull(activitySourceField, "CollectionResultDefinition should have an _activitySource field"); + Assert.IsNull(collectionResultDefinition.Fields.FirstOrDefault(f => f.Name == "_activity"), + "CollectionResultDefinition should NOT have an _activity field"); - // The constructor should accept an activity parameter + // The constructor should accept an activitySource parameter var ctor = collectionResultDefinition.Constructors.FirstOrDefault(); Assert.IsNotNull(ctor); - var activityParam = ctor!.Signature.Parameters.FirstOrDefault(p => p.Name == "activity"); - Assert.IsNotNull(activityParam, "Constructor should have an 'activity' parameter"); + var activitySourceParam = ctor!.Signature.Parameters.FirstOrDefault(p => p.Name == "activitySource"); + Assert.IsNotNull(activitySourceParam, "Constructor should have an 'activitySource' parameter"); - // GetRawPages method should have try-finally to dispose the activity. - // Note: C# does not allow 'yield return' inside a try-catch (CS1626), so only try-finally is used here. + // GetRawPages method should NOT have try-finally (no per-collection activity). + // It should call ExecutePageRequest which handles per-page activity. var getRawPagesMethod = collectionResultDefinition.Methods.FirstOrDefault(m => m.Signature.Name == "GetRawPages"); Assert.IsNotNull(getRawPagesMethod); var rawPagesBody = getRawPagesMethod!.BodyStatements!.ToDisplayString(); - StringAssert.Contains("try", rawPagesBody); - StringAssert.Contains("finally", rawPagesBody); - StringAssert.Contains("_activity?.Dispose()", rawPagesBody); - // No catch block in iterator methods (CS1626 restriction) - StringAssert.DoesNotContain("catch", rawPagesBody); + StringAssert.Contains("ExecutePageRequest", rawPagesBody); + StringAssert.DoesNotContain("_activitySource.StartActivity", rawPagesBody); + + // ExecutePageRequest helper should have per-page activity with try-catch + var executePageMethod = collectionResultDefinition.Methods.FirstOrDefault(m => m.Signature.Name == "ExecutePageRequest"); + Assert.IsNotNull(executePageMethod, "CollectionResultDefinition should have an ExecutePageRequest method"); + var executePageBody = executePageMethod!.BodyStatements!.ToDisplayString(); + StringAssert.Contains("_activitySource?.StartActivity(\"TestClient.GetCats\"", executePageBody); + StringAssert.Contains("using global::System.Diagnostics.Activity activity", executePageBody); + StringAssert.Contains("catch", executePageBody); + StringAssert.Contains("SetStatus", executePageBody); + StringAssert.Contains("ActivityStatusCode.Error", executePageBody); } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult.cs index 82a64a287e0..e8ed77ee71e 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult.cs @@ -10,13 +10,14 @@ using System.ClientModel.Primitives; using System.Collections.Generic; using System.Diagnostics; +using System.Threading.Tasks; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly string _token; private readonly RequestOptions _options; @@ -24,39 +25,32 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenAsyncCollecti /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, ActivitySource activitySource = null) { _client = client; _token = token; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - try + PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); + string nextToken = null; + while (true) { - PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); - string nextToken = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; + ClientResult result = await ExecutePageRequestAsync(message).ConfigureAwait(false); + yield return result; - nextToken = ((ListWithContinuationTokenResponse)result).NextToken; - if (string.IsNullOrEmpty(nextToken)) - { - yield break; - } - message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); + nextToken = ((ListWithContinuationTokenResponse)result).NextToken; + if (string.IsNullOrEmpty(nextToken)) + { + yield break; } - } - finally - { - _activity?.Dispose(); + message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); } } @@ -75,5 +69,20 @@ public override ContinuationToken GetContinuationToken(ClientResult page) return null; } } + + /// The pipeline message. + private async Task ExecutePageRequestAsync(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); + try + { + return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT.cs index 9183bd6e275..51934caa899 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT.cs @@ -17,7 +17,7 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly string _token; private readonly RequestOptions _options; @@ -25,39 +25,32 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenAsyncCollecti /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, ActivitySource activitySource = null) { _client = client; _token = token; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - try + PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); + string nextToken = null; + while (true) { - PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); - string nextToken = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; + ClientResult result = await ExecutePageRequestAsync(message).ConfigureAwait(false); + yield return result; - nextToken = ((ListWithContinuationTokenResponse)result).NextToken; - if (string.IsNullOrEmpty(nextToken)) - { - yield break; - } - message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); + nextToken = ((ListWithContinuationTokenResponse)result).NextToken; + if (string.IsNullOrEmpty(nextToken)) + { + yield break; } - } - finally - { - _activity?.Dispose(); + message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); } } @@ -77,6 +70,21 @@ public override ContinuationToken GetContinuationToken(ClientResult page) } } + /// The pipeline message. + private async Task ExecutePageRequestAsync(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); + try + { + return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } + /// Gets the values from the specified page. /// /// The values from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResult.cs index 703f16ae262..0e0aeb81884 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResult.cs @@ -16,7 +16,7 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithContinuationTokenCollectionResult : CollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly string _token; private readonly RequestOptions _options; @@ -24,39 +24,32 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenCollectionRes /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithContinuationTokenCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, ActivitySource activitySource = null) { _client = client; _token = token; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - try + PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); + string nextToken = null; + while (true) { - PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); - string nextToken = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; + ClientResult result = ExecutePageRequest(message); + yield return result; - nextToken = ((ListWithContinuationTokenResponse)result).NextToken; - if (string.IsNullOrEmpty(nextToken)) - { - yield break; - } - message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); + nextToken = ((ListWithContinuationTokenResponse)result).NextToken; + if (string.IsNullOrEmpty(nextToken)) + { + yield break; } - } - finally - { - _activity?.Dispose(); + message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); } } @@ -75,5 +68,20 @@ public override ContinuationToken GetContinuationToken(ClientResult page) return null; } } + + /// The pipeline message. + private ClientResult ExecutePageRequest(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); + try + { + return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT.cs index 4e6862904af..c11a0643c9d 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT.cs @@ -16,7 +16,7 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT : CollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly string _token; private readonly RequestOptions _options; @@ -24,39 +24,32 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenCollectionRes /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, ActivitySource activitySource = null) { _client = client; _token = token; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - try + PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); + string nextToken = null; + while (true) { - PipelineMessage message = _client.CreateGetWithContinuationTokenRequest(_token, _options); - string nextToken = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; + ClientResult result = ExecutePageRequest(message); + yield return result; - nextToken = ((ListWithContinuationTokenResponse)result).NextToken; - if (string.IsNullOrEmpty(nextToken)) - { - yield break; - } - message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); + nextToken = ((ListWithContinuationTokenResponse)result).NextToken; + if (string.IsNullOrEmpty(nextToken)) + { + yield break; } - } - finally - { - _activity?.Dispose(); + message = _client.CreateGetWithContinuationTokenRequest(nextToken, _options); } } @@ -76,6 +69,21 @@ public override ContinuationToken GetContinuationToken(ClientResult page) } } + /// The pipeline message. + private ClientResult ExecutePageRequest(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); + try + { + return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } + /// Gets the values from the specified page. /// /// The values from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult.cs index 81dd0453a0c..7910703fcf3 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult.cs @@ -10,13 +10,14 @@ using System.ClientModel.Primitives; using System.Collections.Generic; using System.Diagnostics; +using System.Threading.Tasks; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly string _token; private readonly RequestOptions _options; @@ -24,42 +25,35 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderRespons /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, ActivitySource activitySource = null) { _client = client; _token = token; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - try + PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); + string nextToken = null; + while (true) { - PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); - string nextToken = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; + ClientResult result = await ExecutePageRequestAsync(message).ConfigureAwait(false); + yield return result; - if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) - { - nextToken = value; - } - else - { - yield break; - } - message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); + if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) + { + nextToken = value; } - } - finally - { - _activity?.Dispose(); + else + { + yield break; + } + message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); } } @@ -77,5 +71,20 @@ public override ContinuationToken GetContinuationToken(ClientResult page) return null; } } + + /// The pipeline message. + private async Task ExecutePageRequestAsync(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); + try + { + return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT.cs index 082cfde21bf..35a6f1d5788 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT.cs @@ -17,7 +17,7 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly string _token; private readonly RequestOptions _options; @@ -25,42 +25,35 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderRespons /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, ActivitySource activitySource = null) { _client = client; _token = token; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - try + PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); + string nextToken = null; + while (true) { - PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); - string nextToken = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; + ClientResult result = await ExecutePageRequestAsync(message).ConfigureAwait(false); + yield return result; - if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) - { - nextToken = value; - } - else - { - yield break; - } - message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); + if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) + { + nextToken = value; } - } - finally - { - _activity?.Dispose(); + else + { + yield break; + } + message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); } } @@ -79,6 +72,21 @@ public override ContinuationToken GetContinuationToken(ClientResult page) } } + /// The pipeline message. + private async Task ExecutePageRequestAsync(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); + try + { + return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } + /// Gets the values from the specified page. /// /// The values from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult.cs index eda78df0981..f9aaca34ac7 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult.cs @@ -16,7 +16,7 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult : CollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly string _token; private readonly RequestOptions _options; @@ -24,42 +24,35 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderRespons /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResult(SampleTypeSpecClient client, string token, RequestOptions options, ActivitySource activitySource = null) { _client = client; _token = token; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - try + PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); + string nextToken = null; + while (true) { - PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); - string nextToken = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; + ClientResult result = ExecutePageRequest(message); + yield return result; - if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) - { - nextToken = value; - } - else - { - yield break; - } - message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); + if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) + { + nextToken = value; } - } - finally - { - _activity?.Dispose(); + else + { + yield break; + } + message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); } } @@ -77,5 +70,20 @@ public override ContinuationToken GetContinuationToken(ClientResult page) return null; } } + + /// The pipeline message. + private ClientResult ExecutePageRequest(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); + try + { + return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT.cs index 3e4f084d674..f021bd69d86 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT.cs @@ -16,7 +16,7 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT : CollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly string _token; private readonly RequestOptions _options; @@ -24,42 +24,35 @@ internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderRespons /// The SampleTypeSpecClient client used to send requests. /// /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options, ActivitySource activitySource = null) { _client = client; _token = token; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - try + PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); + string nextToken = null; + while (true) { - PipelineMessage message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options); - string nextToken = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; + ClientResult result = ExecutePageRequest(message); + yield return result; - if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) - { - nextToken = value; - } - else - { - yield break; - } - message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); + if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value)) + { + nextToken = value; } - } - finally - { - _activity?.Dispose(); + else + { + yield break; + } + message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _options); } } @@ -78,6 +71,21 @@ public override ContinuationToken GetContinuationToken(ClientResult page) } } + /// The pipeline message. + private ClientResult ExecutePageRequest(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); + try + { + return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } + /// Gets the values from the specified page. /// /// The values from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult.cs index 5213302b1e0..0de5aaa3247 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult.cs @@ -10,50 +10,44 @@ using System.ClientModel.Primitives; using System.Collections.Generic; using System.Diagnostics; +using System.Threading.Tasks; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithNextLinkAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - try + PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; + ClientResult result = await ExecutePageRequestAsync(message).ConfigureAwait(false); + yield return result; - nextPageUri = ((ListWithNextLinkResponse)result).Next; - if (nextPageUri == null) - { - yield break; - } - message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); + nextPageUri = ((ListWithNextLinkResponse)result).Next; + if (nextPageUri == null) + { + yield break; } - } - finally - { - _activity?.Dispose(); + message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); } } @@ -72,5 +66,20 @@ public override ContinuationToken GetContinuationToken(ClientResult page) return null; } } + + /// The pipeline message. + private async Task ExecutePageRequestAsync(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); + try + { + return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT.cs index da5f87e3ad3..7aefbc2446b 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT.cs @@ -17,44 +17,37 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - try + PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; + ClientResult result = await ExecutePageRequestAsync(message).ConfigureAwait(false); + yield return result; - nextPageUri = ((ListWithNextLinkResponse)result).Next; - if (nextPageUri == null) - { - yield break; - } - message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); + nextPageUri = ((ListWithNextLinkResponse)result).Next; + if (nextPageUri == null) + { + yield break; } - } - finally - { - _activity?.Dispose(); + message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); } } @@ -74,6 +67,21 @@ public override ContinuationToken GetContinuationToken(ClientResult page) } } + /// The pipeline message. + private async Task ExecutePageRequestAsync(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); + try + { + return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } + /// Gets the values from the specified page. /// /// The values from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResult.cs index e3ac7441096..bfe752cacc2 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResult.cs @@ -16,44 +16,37 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithNextLinkCollectionResult : CollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithNextLinkCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithNextLinkCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithNextLinkCollectionResult(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - try + PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; + ClientResult result = ExecutePageRequest(message); + yield return result; - nextPageUri = ((ListWithNextLinkResponse)result).Next; - if (nextPageUri == null) - { - yield break; - } - message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); + nextPageUri = ((ListWithNextLinkResponse)result).Next; + if (nextPageUri == null) + { + yield break; } - } - finally - { - _activity?.Dispose(); + message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); } } @@ -72,5 +65,20 @@ public override ContinuationToken GetContinuationToken(ClientResult page) return null; } } + + /// The pipeline message. + private ClientResult ExecutePageRequest(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); + try + { + return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResultOfT.cs index 4a51a7ef510..ef0e550f403 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithNextLinkCollectionResultOfT.cs @@ -16,44 +16,37 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithNextLinkCollectionResultOfT : CollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithNextLinkCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithNextLinkCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithNextLinkCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - try + PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - PipelineMessage message = _client.CreateGetWithNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; + ClientResult result = ExecutePageRequest(message); + yield return result; - nextPageUri = ((ListWithNextLinkResponse)result).Next; - if (nextPageUri == null) - { - yield break; - } - message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); + nextPageUri = ((ListWithNextLinkResponse)result).Next; + if (nextPageUri == null) + { + yield break; } - } - finally - { - _activity?.Dispose(); + message = _client.CreateNextGetWithNextLinkRequest(nextPageUri, _options); } } @@ -73,6 +66,21 @@ public override ContinuationToken GetContinuationToken(ClientResult page) } } + /// The pipeline message. + private ClientResult ExecutePageRequest(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); + try + { + return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } + /// Gets the values from the specified page. /// /// The values from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResult.cs index 07b1be5960e..dcdebb780c7 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResult.cs @@ -5,43 +5,38 @@ #nullable disable +using System; using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; using System.Diagnostics; +using System.Threading.Tasks; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithPagingAsyncCollectionResult : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithPagingAsyncCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithPagingAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithPagingAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - try - { - PipelineMessage message = _client.CreateGetWithPagingRequest(_options); - yield return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - } - finally - { - _activity?.Dispose(); - } + PipelineMessage message = _client.CreateGetWithPagingRequest(_options); + yield return await ExecutePageRequestAsync(message).ConfigureAwait(false); } /// Gets the continuation token from the specified page. @@ -51,5 +46,20 @@ public override ContinuationToken GetContinuationToken(ClientResult page) { return null; } + + /// The pipeline message. + private async Task ExecutePageRequestAsync(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); + try + { + return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT.cs index 95d8c2acebb..1204c2db352 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT.cs @@ -5,6 +5,7 @@ #nullable disable +using System; using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; @@ -16,33 +17,26 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - try - { - PipelineMessage message = _client.CreateGetWithPagingRequest(_options); - yield return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - } - finally - { - _activity?.Dispose(); - } + PipelineMessage message = _client.CreateGetWithPagingRequest(_options); + yield return await ExecutePageRequestAsync(message).ConfigureAwait(false); } /// Gets the continuation token from the specified page. @@ -53,6 +47,21 @@ public override ContinuationToken GetContinuationToken(ClientResult page) return null; } + /// The pipeline message. + private async Task ExecutePageRequestAsync(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); + try + { + return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } + /// Gets the values from the specified page. /// /// The values from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResult.cs index a8718edf8e7..3ea77a9384d 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResult.cs @@ -5,6 +5,7 @@ #nullable disable +using System; using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; @@ -15,33 +16,26 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithPagingCollectionResult : CollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithPagingCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithPagingCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithPagingCollectionResult(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - try - { - PipelineMessage message = _client.CreateGetWithPagingRequest(_options); - yield return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - } - finally - { - _activity?.Dispose(); - } + PipelineMessage message = _client.CreateGetWithPagingRequest(_options); + yield return ExecutePageRequest(message); } /// Gets the continuation token from the specified page. @@ -51,5 +45,20 @@ public override ContinuationToken GetContinuationToken(ClientResult page) { return null; } + + /// The pipeline message. + private ClientResult ExecutePageRequest(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); + try + { + return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResultOfT.cs index 06ac83140d4..74d1ab985f4 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithPagingCollectionResultOfT.cs @@ -5,6 +5,7 @@ #nullable disable +using System; using System.ClientModel; using System.ClientModel.Primitives; using System.Collections.Generic; @@ -15,33 +16,26 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithPagingCollectionResultOfT : CollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithPagingCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithPagingCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithPagingCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - try - { - PipelineMessage message = _client.CreateGetWithPagingRequest(_options); - yield return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - } - finally - { - _activity?.Dispose(); - } + PipelineMessage message = _client.CreateGetWithPagingRequest(_options); + yield return ExecutePageRequest(message); } /// Gets the continuation token from the specified page. @@ -52,6 +46,21 @@ public override ContinuationToken GetContinuationToken(ClientResult page) return null; } + /// The pipeline message. + private ClientResult ExecutePageRequest(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); + try + { + return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } + /// Gets the values from the specified page. /// /// The values from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult.cs index 14123f5b2b7..86adf4d2544 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult.cs @@ -10,51 +10,45 @@ using System.ClientModel.Primitives; using System.Collections.Generic; using System.Diagnostics; +using System.Threading.Tasks; namespace SampleTypeSpec { internal partial class SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResult(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - try + PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; + ClientResult result = await ExecutePageRequestAsync(message).ConfigureAwait(false); + yield return result; - string nextPageString = ((ListWithStringNextLinkResponse)result).Next; - if (string.IsNullOrEmpty(nextPageString)) - { - yield break; - } - nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); - message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); + string nextPageString = ((ListWithStringNextLinkResponse)result).Next; + if (string.IsNullOrEmpty(nextPageString)) + { + yield break; } - } - finally - { - _activity?.Dispose(); + nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); + message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); } } @@ -73,5 +67,20 @@ public override ContinuationToken GetContinuationToken(ClientResult page) return null; } } + + /// The pipeline message. + private async Task ExecutePageRequestAsync(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); + try + { + return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT.cs index aff41c31dc9..9357309c3b4 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT.cs @@ -17,45 +17,38 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT : AsyncCollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override async IAsyncEnumerable GetRawPagesAsync() { - try + PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); - yield return result; + ClientResult result = await ExecutePageRequestAsync(message).ConfigureAwait(false); + yield return result; - string nextPageString = ((ListWithStringNextLinkResponse)result).Next; - if (string.IsNullOrEmpty(nextPageString)) - { - yield break; - } - nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); - message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); + string nextPageString = ((ListWithStringNextLinkResponse)result).Next; + if (string.IsNullOrEmpty(nextPageString)) + { + yield break; } - } - finally - { - _activity?.Dispose(); + nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); + message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); } } @@ -75,6 +68,21 @@ public override ContinuationToken GetContinuationToken(ClientResult page) } } + /// The pipeline message. + private async Task ExecutePageRequestAsync(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); + try + { + return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } + /// Gets the values from the specified page. /// /// The values from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResult.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResult.cs index 2b76ffc64a2..f15bc911235 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResult.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResult.cs @@ -16,45 +16,38 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithStringNextLinkCollectionResult : CollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithStringNextLinkCollectionResult, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithStringNextLinkCollectionResult(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithStringNextLinkCollectionResult(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - try + PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; + ClientResult result = ExecutePageRequest(message); + yield return result; - string nextPageString = ((ListWithStringNextLinkResponse)result).Next; - if (string.IsNullOrEmpty(nextPageString)) - { - yield break; - } - nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); - message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); + string nextPageString = ((ListWithStringNextLinkResponse)result).Next; + if (string.IsNullOrEmpty(nextPageString)) + { + yield break; } - } - finally - { - _activity?.Dispose(); + nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); + message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); } } @@ -73,5 +66,20 @@ public override ContinuationToken GetContinuationToken(ClientResult page) return null; } } + + /// The pipeline message. + private ClientResult ExecutePageRequest(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); + try + { + return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } } } diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT.cs index 480594534fb..67aac7b1557 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/CollectionResults/SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT.cs @@ -16,45 +16,38 @@ namespace SampleTypeSpec internal partial class SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT : CollectionResult { private readonly SampleTypeSpecClient _client; - private readonly Activity _activity; + private readonly ActivitySource _activitySource; private readonly RequestOptions _options; /// Initializes a new instance of SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT, which is used to iterate over the pages of a collection. /// The SampleTypeSpecClient client used to send requests. /// The request options, which can override default behaviors of the client pipeline on a per-call basis. - /// The activity for distributed tracing. - public SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, Activity activity = null) + /// The activity source for distributed tracing. + public SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT(SampleTypeSpecClient client, RequestOptions options, ActivitySource activitySource = null) { _client = client; _options = options; - _activity = activity; + _activitySource = activitySource; } /// Gets the raw pages of the collection. /// The raw pages of the collection. public override IEnumerable GetRawPages() { - try + PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); + Uri nextPageUri = null; + while (true) { - PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options); - Uri nextPageUri = null; - while (true) - { - ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); - yield return result; + ClientResult result = ExecutePageRequest(message); + yield return result; - string nextPageString = ((ListWithStringNextLinkResponse)result).Next; - if (string.IsNullOrEmpty(nextPageString)) - { - yield break; - } - nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); - message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); + string nextPageString = ((ListWithStringNextLinkResponse)result).Next; + if (string.IsNullOrEmpty(nextPageString)) + { + yield break; } - } - finally - { - _activity?.Dispose(); + nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute); + message = _client.CreateNextGetWithStringNextLinkRequest(nextPageUri, _options); } } @@ -74,6 +67,21 @@ public override ContinuationToken GetContinuationToken(ClientResult page) } } + /// The pipeline message. + private ClientResult ExecutePageRequest(PipelineMessage message) + { + using Activity activity = _activitySource?.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); + try + { + return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options)); + } + catch (Exception ex) + { + activity?.SetStatus(ActivityStatusCode.Error, ex.Message); + throw; + } + } + /// Gets the values from the specified page. /// /// The values from the specified page. diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs index 4328acff449..95f59346336 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs @@ -1713,8 +1713,7 @@ public virtual AsyncCollectionResult GetWithNextLinkAsync(RequestOptions options /// Service returned a non-success status code. public virtual CollectionResult GetWithNextLink(CancellationToken cancellationToken = default) { - Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); - return new SampleTypeSpecClientGetWithNextLinkCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); + return new SampleTypeSpecClientGetWithNextLinkCollectionResultOfT(this, cancellationToken.ToRequestOptions(), _activitySource); } /// List things with nextlink. @@ -1722,8 +1721,7 @@ public virtual CollectionResult GetWithNextLink(CancellationToken cancell /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithNextLinkAsync(CancellationToken cancellationToken = default) { - Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithNextLink", ActivityKind.Client); - return new SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); + return new SampleTypeSpecClientGetWithNextLinkAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions(), _activitySource); } /// @@ -1763,8 +1761,7 @@ public virtual AsyncCollectionResult GetWithStringNextLinkAsync(RequestOptions o /// Service returned a non-success status code. public virtual CollectionResult GetWithStringNextLink(CancellationToken cancellationToken = default) { - Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); - return new SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); + return new SampleTypeSpecClientGetWithStringNextLinkCollectionResultOfT(this, cancellationToken.ToRequestOptions(), _activitySource); } /// List things with nextlink. @@ -1772,8 +1769,7 @@ public virtual CollectionResult GetWithStringNextLink(CancellationToken c /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithStringNextLinkAsync(CancellationToken cancellationToken = default) { - Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithStringNextLink", ActivityKind.Client); - return new SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); + return new SampleTypeSpecClientGetWithStringNextLinkAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions(), _activitySource); } /// @@ -1816,8 +1812,7 @@ public virtual AsyncCollectionResult GetWithContinuationTokenAsync(string token, /// Service returned a non-success status code. public virtual CollectionResult GetWithContinuationToken(string token = default, CancellationToken cancellationToken = default) { - Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); - return new SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), activity); + return new SampleTypeSpecClientGetWithContinuationTokenCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), _activitySource); } /// List things with continuation token. @@ -1826,8 +1821,7 @@ public virtual CollectionResult GetWithContinuationToken(string token = d /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithContinuationTokenAsync(string token = default, CancellationToken cancellationToken = default) { - Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationToken", ActivityKind.Client); - return new SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), activity); + return new SampleTypeSpecClientGetWithContinuationTokenAsyncCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), _activitySource); } /// @@ -1870,8 +1864,7 @@ public virtual AsyncCollectionResult GetWithContinuationTokenHeaderResponseAsync /// Service returned a non-success status code. public virtual CollectionResult GetWithContinuationTokenHeaderResponse(string token = default, CancellationToken cancellationToken = default) { - Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); - return new SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), activity); + return new SampleTypeSpecClientGetWithContinuationTokenHeaderResponseCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), _activitySource); } /// List things with continuation token header response. @@ -1880,8 +1873,7 @@ public virtual CollectionResult GetWithContinuationTokenHeaderResponse(st /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithContinuationTokenHeaderResponseAsync(string token = default, CancellationToken cancellationToken = default) { - Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithContinuationTokenHeaderResponse", ActivityKind.Client); - return new SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), activity); + return new SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT(this, token, cancellationToken.ToRequestOptions(), _activitySource); } /// @@ -1921,8 +1913,7 @@ public virtual AsyncCollectionResult GetWithPagingAsync(RequestOptions options) /// Service returned a non-success status code. public virtual CollectionResult GetWithPaging(CancellationToken cancellationToken = default) { - Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); - return new SampleTypeSpecClientGetWithPagingCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); + return new SampleTypeSpecClientGetWithPagingCollectionResultOfT(this, cancellationToken.ToRequestOptions(), _activitySource); } /// List things with paging. @@ -1930,8 +1921,7 @@ public virtual CollectionResult GetWithPaging(CancellationToken cancellat /// Service returned a non-success status code. public virtual AsyncCollectionResult GetWithPagingAsync(CancellationToken cancellationToken = default) { - Activity activity = _activitySource.StartActivity("SampleTypeSpecClient.GetWithPaging", ActivityKind.Client); - return new SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions(), activity); + return new SampleTypeSpecClientGetWithPagingAsyncCollectionResultOfT(this, cancellationToken.ToRequestOptions(), _activitySource); } ///