Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Make result a global field in C# Telemetry Generator #825

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ build
# C# Telemetry Client Generator Related
telemetry/csharp/buildtemp/**
telemetry/csharp/AwsToolkit.Telemetry.SDK/Generated/**
telemetry/csharp/client-generator-inputs/telemetry-2017-07-25.normal.json
telemetry/csharp/client-generator-inputs/telemetry-2017-07-25.normal.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

datum = datum.InvokeTransform(transformDatum);

Expand Down Expand Up @@ -121,6 +122,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

datum = datum.InvokeTransform(transformDatum);

Expand Down Expand Up @@ -176,6 +178,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

datum.AddMetadata("attempts", payload.Attempts);

Expand Down Expand Up @@ -239,6 +242,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

datum.AddMetadata("attempts", payload.Attempts);

Expand Down Expand Up @@ -302,6 +306,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

if (payload.Attempts.HasValue)
{
Expand Down Expand Up @@ -374,6 +379,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

datum = datum.InvokeTransform(transformDatum);

Expand All @@ -389,7 +395,7 @@ namespace Test
}

/// Emits milliseconds
public sealed class EmitSomeIntMs : BaseTelemetryEvent
public sealed class EmitSomeIntMs : BaseGeneratedTelemetryEvent
{

public EmitSomeIntMs()
Expand All @@ -399,7 +405,7 @@ namespace Test
}

/// Emits something without a unit defined
public sealed class EmitNoUnit : BaseTelemetryEvent
public sealed class EmitNoUnit : BaseGeneratedTelemetryEvent
{

public EmitNoUnit()
Expand All @@ -409,7 +415,7 @@ namespace Test
}

/// Emits a bunch of metadata varieties
public sealed class EmitSomeBigBlob : BaseTelemetryEvent
public sealed class EmitSomeBigBlob : BaseGeneratedTelemetryEvent
{

/// A generic number of attempts
Expand All @@ -431,7 +437,7 @@ namespace Test
}

/// Emits a bunch of metadata varieties - required not set
public sealed class EmitSomeBigBlob2 : BaseTelemetryEvent
public sealed class EmitSomeBigBlob2 : BaseGeneratedTelemetryEvent
{

/// A generic number of attempts
Expand All @@ -453,7 +459,7 @@ namespace Test
}

/// Emits a bunch of optional metadata varieties
public sealed class EmitSomeBigOptionalBlob : BaseTelemetryEvent
public sealed class EmitSomeBigOptionalBlob : BaseGeneratedTelemetryEvent
{

/// Optional - A generic number of attempts
Expand All @@ -475,7 +481,7 @@ namespace Test
}

/// Emit a passive metric
public sealed class EmitPassive : BaseTelemetryEvent
public sealed class EmitPassive : BaseGeneratedTelemetryEvent
{

public EmitPassive()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

datum = datum.InvokeTransform(transformDatum);

Expand Down Expand Up @@ -121,6 +122,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

datum = datum.InvokeTransform(transformDatum);

Expand Down Expand Up @@ -176,6 +178,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

datum.AddMetadata("attempts", payload.Attempts);

Expand Down Expand Up @@ -239,6 +242,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

datum.AddMetadata("attempts", payload.Attempts);

Expand Down Expand Up @@ -302,6 +306,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

if (payload.Attempts.HasValue)
{
Expand Down Expand Up @@ -374,6 +379,7 @@ namespace Test
datum.AddMetadata("duration", payload.Duration.Value);
}
datum.AddMetadata("locale", payload.Locale);
datum.AddResultToMetadata(payload.Result);

datum = datum.InvokeTransform(transformDatum);

Expand Down Expand Up @@ -413,7 +419,7 @@ namespace Test
}

/// Emits milliseconds
public sealed class EmitSomeIntMs : BaseTelemetryEvent
public sealed class EmitSomeIntMs : BaseGeneratedTelemetryEvent
{

public EmitSomeIntMs()
Expand All @@ -423,7 +429,7 @@ namespace Test
}

/// Emits something without a unit defined
public sealed class EmitNoUnit : BaseTelemetryEvent
public sealed class EmitNoUnit : BaseGeneratedTelemetryEvent
{

public EmitNoUnit()
Expand All @@ -433,7 +439,7 @@ namespace Test
}

/// Emits a bunch of metadata varieties
public sealed class EmitSomeBigBlob : BaseTelemetryEvent
public sealed class EmitSomeBigBlob : BaseGeneratedTelemetryEvent
{

/// A generic number of attempts
Expand All @@ -455,7 +461,7 @@ namespace Test
}

/// Emits a bunch of metadata varieties - required not set
public sealed class EmitSomeBigBlob2 : BaseTelemetryEvent
public sealed class EmitSomeBigBlob2 : BaseGeneratedTelemetryEvent
{

/// A generic number of attempts
Expand All @@ -477,7 +483,7 @@ namespace Test
}

/// Emits a bunch of optional metadata varieties
public sealed class EmitSomeBigOptionalBlob : BaseTelemetryEvent
public sealed class EmitSomeBigOptionalBlob : BaseGeneratedTelemetryEvent
{

/// Optional - A generic number of attempts
Expand All @@ -499,7 +505,7 @@ namespace Test
}

/// Emit a passive metric
public sealed class EmitPassive : BaseTelemetryEvent
public sealed class EmitPassive : BaseGeneratedTelemetryEvent
{

public EmitPassive()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<!--<TargetFrameworks>net8.0;net472</TargetFrameworks>-->
<RootNamespace>Amazon.AwsToolkit.Telemetry.Events.Generator</RootNamespace>
<AssemblyName>Amazon.AwsToolkit.Telemetry.Events.Generator</AssemblyName>
<Authors>Amazon Web Services</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ public class DefinitionsBuilder
private const string MetadataEntryFullName = "MetadataEntry";
private const string MetricDatumFullName = "MetricDatum";
private const string AddMetadataMethodName = "AddMetadata";
private const string AddResultToMetadataMethodName = "AddResultToMetadata";
private const string InvokeTransformMethodName = "InvokeTransform";

// contains metadata fields that should be skipped when generating code.
// These fields are covered by the class BaseTelemetryEvent
// These fields are covered by the BaseTelemetryEvent and BaseGeneratedTelemetryEvent classes
private static readonly string[] ImplicitFields =
{
"result",
"reason",
"errorCode",
"causedBy",
Expand Down Expand Up @@ -283,7 +285,7 @@ private CodeTypeDeclaration CreateMetricDataClass(Metric metric)
TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed
};

cls.BaseTypes.Add("BaseTelemetryEvent");
cls.BaseTypes.Add("BaseGeneratedTelemetryEvent");

if (!string.IsNullOrWhiteSpace(metric.description))
{
Expand Down Expand Up @@ -455,7 +457,7 @@ private CodeMemberMethod CreateRecordMetricMethodByDataClass(Metric metric)
// {
// datum.AddMetadata("duration", payload.Duration.Value);
// }
var payloadDuration= new CodeFieldReferenceExpression(payload, "Duration");
var payloadDuration = new CodeFieldReferenceExpression(payload, "Duration");
var hasValueDuration = new CodeFieldReferenceExpression(payloadDuration, "HasValue");
var durationMetadata = new CodeMethodInvokeExpression(datumAddData,
new CodePrimitiveExpression("duration"), new CodeFieldReferenceExpression(payloadDuration, "Value"));
Expand All @@ -467,6 +469,12 @@ private CodeMemberMethod CreateRecordMetricMethodByDataClass(Metric metric)
tryStatements.Add(new CodeExpressionStatement(new CodeMethodInvokeExpression(datumAddData,
new CodePrimitiveExpression("locale"), payloadLocale)));

// Generate: datum.AddResultToMetadata(payload.Result);
var payloadResult = new CodeFieldReferenceExpression(payload, "Result");
tryStatements.Add(new CodeExpressionStatement(new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(datum, AddResultToMetadataMethodName),
payloadResult)));

// Set MetricDatum Metadata values
metric.metadata?
.Where(metadata => !ImplicitFields.Contains(metadata.type))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>Amazon.AwsToolkit.Telemetry.Events.SourceGenerator</RootNamespace>
<AssemblyName>Amazon.AwsToolkit.Telemetry.Events.SourceGenerator</AssemblyName>
<!--<Authors>Amazon Web Services</Authors>-->
<!--<Product>AWS Toolkit Telemetry Events Generator</Product>-->
<!--<Description>Code generator that produces Telemetry events for AWS Toolkits</Description>-->
<!--<Copyright>Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.</Copyright>-->
<!--<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>-->
<!--<RepositoryUrl>https://github.com/aws/aws-toolkit-common</RepositoryUrl>-->
<!--<PackageTags>AWS AWSToolkit</PackageTags>-->
<!--<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>-->

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeBuildOutput>false</IncludeBuildOutput>

<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

<ItemGroup>
<Content Include="..\..\definitions\*.json">
<Link>Definitions\%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<Content Remove="..\..\definitions\commonDefinitions.json" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="..\..\definitions\commonDefinitions.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" PrivateAssets="all" />

<!-- Take a private dependency on Newtonsoft.Json (PrivateAssets=all) Consumers of this generator will not reference it.
Set GeneratePathProperty=true so we can reference the binaries via the PKGNewtonsoft_Json property -->
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="System.CodeDom" Version="8.0.0" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<Folder Include="Definitions\" />
</ItemGroup>


<ItemGroup>

<!-- https://github.com/dotnet/roslyn/discussions/47517#discussioncomment-64145 -->
<!-- https://github.com/dotnet/roslyn-sdk/blob/0313c80ed950ac4f4eef11bb2e1c6d1009b328c4/samples/CSharp/SourceGenerators/SourceGeneratorSamples/SourceGeneratorSamples.csproj#L13-L30 -->
<!-- https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md#use-functionality-from-nuget-packages -->
<!-- https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview -->


<!-- Package the generator in the analyzer directory of the nuget package -->
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />

<!-- Package the Newtonsoft.Json dependency alongside the generator assembly -->
<!-- Seems like wildcard "*.dll" does not work -->

<TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_CodeDom)\lib\netstandard2.0\System.CodeDom.dll" IncludeRuntimeDependency="false" />
</ItemGroup>
</Project>
Loading