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

feat(@opentelemetry/semantic-conventions): change enum to object literals #2532

Merged

Conversation

echoontheway
Copy link
Contributor

@echoontheway echoontheway commented Oct 12, 2021

Which problem is this PR solving?

  • the transpile outputs of enums seem a little weird,which build lock-in to TS and not that straightforward

TS:
image
outputs:
image

  • transpile outputs are hard to be treeshaked, which increases bundle size of a Web App.
    As shown above, TS enums will be transpiled to self-executing anonymous functions, which won't be regarded as dead code and got eliminated by pack tools. So all these enum transpile outputs are left in the bundle even if they're not imported by the app.

bundle size of enum:
wecom-temp-b346baaaaa06dd094a63e25bf4d4a67e
bundle size of object literal
d1661c3a-7f6d-41c4-80f4-9fe1364af713
(bundle size statistics are got with the help of rollup-plugin-visualizer . Detail pack config can be found in the simplified example-app in my repo)

Short description of the changes

  • modify template and regenerate semantic conventions
  • the benefits of using enums for code completions and type checking are retained
  • the bundle size is decreased

@echoontheway echoontheway requested a review from a team October 12, 2021 10:01
@echoontheway echoontheway changed the title feat(@opentelemetry/semantic-conventions): change enum to object lite… feat(@opentelemetry/semantic-conventions): change enum to object literals Oct 12, 2021
@codecov
Copy link

codecov bot commented Oct 12, 2021

Codecov Report

Merging #2532 (2a50988) into main (f9f43e5) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main    #2532   +/-   ##
=======================================
  Coverage   93.05%   93.05%           
=======================================
  Files         139      139           
  Lines        5172     5172           
  Branches     1110     1110           
=======================================
  Hits         4813     4813           
  Misses        359      359           

@rauno56
Copy link
Member

rauno56 commented Oct 12, 2021

Would const enums behave much differently?
May I ask how did you get the bundle size statistics? I'd like to understand the situation a little better myself. :)

@weyert
Copy link
Contributor

weyert commented Oct 12, 2021

Sorry, how does this impair debuggability? Wouldn't the benefits of using enums for code completions and type checking getting lost?

Just found: https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums
Maybe it could work if as const gets added as a postfix 🤔

@echoontheway
Copy link
Contributor Author

echoontheway commented Oct 14, 2021

Would const enums behave much differently? May I ask how did you get the bundle size statistics? I'd like to understand the situation a little better myself. :)

Const enum members are inlined at use sites, and they will be completely removed during compilation. We don't use the enums in semantic-conventions, so they won't exist in semantic-convention transpile outputs. Const enums seem not suitable for our 'export' scenario.

I pack my app using vite , tree-shaking is enabled by default for vite. And I get the bundle size statistics with the help of rollup-plugin-visualizer . I have uploaded the simplified example-app just now to my repo,you may have a look :)
pack config:
image
pack command:
image
TS enums will be transpiled to self-executing anonymous functions, which won't be regarded as dead code and got eliminated by pack tools. So all these enum transpile outputs are left in the bundle even if they're not imported by the app.

@echoontheway
Copy link
Contributor Author

echoontheway commented Oct 14, 2021

Sorry, how does this impair debuggability? Wouldn't the benefits of using enums for code completions and type checking getting lost?

Just found: https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums Maybe it could work if as const gets added as a postfix 🤔

It seems object is more straightforward and won't impair code completion.

Do you mean 'const enums'?I tried const enums, however , they seem not suitable for our 'export' scenario, for const enums will be removed during compilation. Then the other app that imported semantic-convention (transpiled code) can't find the 'const enums'.

I have tried to add the 'as const' as postfix for object to keep the original 'type checking' ability and it works, thanks for your advice!

@weyert
Copy link
Contributor

weyert commented Oct 14, 2021

Great @echoontheway , thanks for taking the time to try it out :)

I now also understand this is problem for when using it for client-side browsers

Copy link
Contributor

@weyert weyert left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@obecny obecny left a comment

Choose a reason for hiding this comment

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

this is really nice finding, lgtm

@Flarna
Copy link
Member

Flarna commented Oct 15, 2021

Line 17 tells // DO NOT EDIT, this is an Auto-generated file from scripts/semconv/templates//templates/SemanticAttributes.ts.j2.

if this is true this change will vanish on next generator run. Maybe the generator needs some fix?

@weyert
Copy link
Contributor

weyert commented Oct 15, 2021

Line 17 tells // DO NOT EDIT, this is an Auto-generated file from scripts/semconv/templates//templates/SemanticAttributes.ts.j2.

if this is true this change will vanish on next generator run. Maybe the generator needs some fix?

The template file has been updated too. Should be all good

@vmarchaud vmarchaud merged commit 043067f into open-telemetry:main Oct 17, 2021
@vmarchaud vmarchaud added the enhancement New feature or request label Oct 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants