Skip to content

Conversation

dolbin-prime
Copy link
Contributor

In #15277, TracesSamplerSamplingContext is added to and exported from types-hoist, but is not being exported from index.ts.

I'm following the instruction, and I want to inject (samplingContext: TracesSamplerSamplingContext) => number | boolean to a function that is wrapping Sentry.init, but I can't because TracesSamplerSamplingContext is not recognized :(

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Hey @dolbin-prime thanks for opening this PR! Before we think about merging it, can you show me what use case you can't do with the currently exported types and functions?

Would some TS type extraction logic like this work for you?

type TracesSamplerOption = Exclude<Parameters<typeof Sentry.init>[0], undefined>['tracesSampler']
type TracesSamplerSamplingContext = Parameters<Exclude<TracesSamplerOption, undefined>>[0]

function myCustomSampler(ctx: TracesSamplerSamplingContext): number {
  return ctx.inheritOrSampleWith(0.3);
}

^ this is obviously not easy to come up with so we can likely just directly export the type. It's public API anyway. Just wanted to check that this is what we're going for.

Also, is it enough for you if this type is only exported from @sentry/core?

Heads-up: I assigned myself to the PR because I'm reviewing it. Also changed the scope of the PR to feat since it adds a public export to a package.

@Lms24 Lms24 self-assigned this Sep 12, 2025
@Lms24 Lms24 changed the title chore(core): export TracesSamplerSamplingContext of types-hoist feat(core): Export TracesSamplerSamplingContext type Sep 12, 2025
@dolbin-prime
Copy link
Contributor Author

dolbin-prime commented Sep 13, 2025

Hi @Lms24, thanks for the review!

Sorry for the long comment. Any feedback would be greatly appreciated.

can you show me what use case you can't do with the currently exported types and functions?

Ok, my team is maintaining a monorepo, and there are two kinds of workspace: apps and common, and I'm about to make a package @common/sentry, that is wrapping some Sentry APIs and reused inside the repo. And of course I need a function(let's say it initializeSentry) that is wrapping Sentry.init, and I want each app in the repo to call this function and set the sampling rate according to its own specific conditions. So initializeSentry needs a parameter of type (samplingContext: TracesSamplerSamplingContext) => number | boolean to be set as traceSampler of BrowserOptions:

import { initializeSentry } from '@common/sentry';

import type { TracesSamplerSamplingContext } from '@sentry/core';

function myTraceSampler(ctx: TracesSamplerSamplingContext): number | boolean {
  const { name, attributes, inheritOrSampleWith } = ctx;
  // app-specific logic
  return inheritOrSampleWith(0.1);
}

initializeSentry({
  // ...
  traceSampler: myTraceSampler,
  // ...
});

createRoot(document.getElementById("root")!).render(
  <StrictMode>
    <App />
  </StrictMode>
);

Would some TS type extraction logic like this work for you?

Yeah it works, thank you for your suggestion, but I'm a little concerned about the hard-coded string or magic numbers. I know the name or the number of parameters are unlikey to change, but maybe I'm probably being overly cautious. 😄

Also, is it enough for you if this type is only exported from @sentry/core?

I needed the type for my use case and discovered it's declared in @sentry/core, so for now, yes. I'm new to Sentry, so I don't know yet if it will be needed from other packages later on. Please understand. 😁

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Thanks for explaining your use case!

Yeah it works, thank you for your suggestion, but I'm a little concerned about the hard-coded string or magic numbers. I know the name or the number of parameters are unlikey to change, but maybe I'm probably being overly cautious

So, the type extraction is public API (just like TracesSamplerSamplingContext would be) so they're guaranteed to be stable within this major. However, the type extraction indeed goes through a few other properties so it's a little bit more likely to break in a future major version.

That being said, since TracesSamplerSamplingContext was implicitly already public API, I think it's fine to make it public explicitly now. For now though, let's just export it from @sentry/core since we rarely export types from core in higher level packages.

I'll merge this PR in a bit. Thanks for contributing!

@Lms24 Lms24 enabled auto-merge (squash) September 15, 2025 09:32
@Lms24 Lms24 merged commit 28d183a into getsentry:develop Sep 15, 2025
60 checks passed
@dolbin-prime
Copy link
Contributor Author

Thank you for reviewing and merging my PR @Lms24 ! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants