Commit 43d8ece
authored
Refactor C# Generator to make adding source generators easier (#828)
This PR is a refactor of the C# Telemetry Generator.
## Problems
1. Some of the core fields ("global metadata properties") are generated types instead of primitive types (the "Result" property is one example). In order to add these into the base classes, we need the generated code. But the generated code is only applied during the package publishing automation.
2. The test classes awkwardly require the generator to be run, and the generated output is compiled into the test projects. This doesn't allow for a clean "build the repo", "test the repo" flow. Currently, we have a hack that force builds and runs the generator as a pre-build step for the test project.
3. There is a test that validates the generator output by placing the entire generated file in the repo. This file becomes very large, and falls out of date quickly, since most repo changes focus on updating the definitions json. Updates to this file are usually only checked in when changes are made to the generator. When this file is updated, the PR's change count is inflated by 17k+ lines.
## Approach
We can take an _incremental_ approach to solving these problems, over a series of changes. The end goal is to use [C# Source Generators](https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview), which will auto-produce the generated code whenever the Events package is compiled.
This allows us to solve problem 1 because the required generated types will be available within the `AwsToolkit.Telemetry.Events` package, even while it is being developed.
Problems 2 and 3 will be solved, because we will be able to delete the large "GeneratedCode.cs" file, and exercise tests directly against `AwsToolkit.Telemetry.Events`, which will now contain the types that are currently only available after the package is produced and published.
Before we introduce the source generator, the generation code is being moved to a "core" package, which will serve the CLI Generator and the (future) source generator. That is all that this change is doing. In follow-up changes, the source generator will be introduced.
Once the source generator is available, the CLI generator will still be used for producing the product specific metrics that are defined in the Toolkit repo, but it will not be used for the purpose of producing `AwsToolkit.Telemetry.Events`.
After the source generator is available, a follow-up change will define `Result` as a global property to all C# events.
The motivation for making these changes incrementally is to rely on existing tests/CI/infrastructure to ensure the generator remains stable throughout the changes.
Before:
```mermaid
flowchart TB
subgraph PkgEvents[Telemetry.Events package]
direction TB
eventsCodeDto["Core DTOs"]
eventsCodeBase["Base Event class"]
genCode["Generated code\n(based on telemetry definitions)"]
end
subgraph PkgEventsGen[Telemetry.Events.Generator package]
direction TB
gCodeDto["definitions json\nloading models"]
gCodeBuilder["Code Builder"]
gCli["Generator CLI\nProgram"]
end
PkgEventsGen -.Generates code\nwhen CLI is run;\nrequired for packaging.-> PkgEvents
```
After:
```mermaid
flowchart TB
subgraph PkgEventsGenCore[Telemetry.Events.Generator.Core package]
direction TB
gcCodeDto["definitions json\nloading models"]
gcCodeBuilder["Code Builder"]
end
subgraph PkgEventsGen[Telemetry.Events.Generator package]
direction TB
gCli["Generator CLI\nProgram"]
end
subgraph PkgEventsSrcGen["Telemetry.Events.SourceGenerator package (FUTURE CHANGE)"]
direction TB
sgCode["Source Generator"]
end
style PkgEventsSrcGen stroke:#0000FF,stroke-dasharray: 5 5
subgraph PkgEvents[Telemetry.Events package]
direction TB
eventsCodeDto["Core DTOs"]
eventsCodeBase["Base Event class"]
genCode["Generated code\n(based on telemetry definitions)"]
end
PkgEventsGen -.Depends on.-> PkgEventsGenCore
PkgEventsSrcGen -.Depends on.-> PkgEventsGenCore
PkgEventsSrcGen -.Generates code\nwhen Events pkg\nis compiled.-> PkgEvents
```1 parent cfc676d commit 43d8ece
File tree
25 files changed
+288
-130
lines changed- telemetry/csharp
- AwsToolkit.Telemetry.Events.Generator.Core
- Models
- Utils
- AwsToolkit.Telemetry.Events.Generator.Tests
- Core
- Generator
- SampleData
- Inputs
- Outcomes
- Utils
- AwsToolkit.Telemetry.Events.Generator
- AwsToolkit.Telemetry.Events.Tests
- Generated
- SampleData
- Inputs
- Outcomes
25 files changed
+288
-130
lines changedLines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
9 | | - | |
10 | | - | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
| 115 | + | |
116 | 116 | | |
117 | | - | |
| 117 | + | |
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
2 | 3 | | |
3 | | - | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
Lines changed: 16 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | | - | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
24 | 36 | | |
25 | 37 | | |
26 | 38 | | |
27 | 39 | | |
28 | | - | |
| 40 | + | |
29 | 41 | | |
30 | 42 | | |
31 | | - | |
| 43 | + | |
32 | 44 | | |
33 | 45 | | |
34 | 46 | | |
| |||
0 commit comments