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

Add overload to the public API to take Action<Scope> #675

Closed
bruno-garcia opened this issue Dec 22, 2020 · 4 comments
Closed

Add overload to the public API to take Action<Scope> #675

bruno-garcia opened this issue Dec 22, 2020 · 4 comments
Assignees
Labels
Feature New feature or request Question Further information is requested

Comments

@bruno-garcia
Copy link
Member

bruno-garcia commented Dec 22, 2020

On the IHub methods like CaptureEvent, CaptureMessage and CaptureException should have an overload that takes: Action<Scope>.

The implementation should do what WithScope does, it pushes a scope, and passes the instance to the callback to be mutated. The result is then applied (normal code path of CaptureEvent(event, scope).

This serves as a replacement to the api:

SentrySdk.WithScope(s =>
{
	s...;
	// Odd call to static method in callback. Data passed magically through the callback
	SentrySdk.CaptureX 
});

This exists on sentry-cocoa:
https://github.com/getsentry/sentry-cocoa/blob/29d1e9f2353f1a9514b2549b1de56e83f136658d/Sources/Sentry/SentrySDK.m#L146-L151

@bruno-garcia
Copy link
Member Author

bruno-garcia commented Dec 7, 2021

Suggestion to tackle this:

  1. Start with a draft PR touching only the main static API: SentrySdk.
  2. Add the overloads to all methods
  3. Add Obsolete to WithScope
  4. Amend tests

The goal is to verify the effort to adding the thing and the impact in the API.
Even though this was discussed internally and added to some SDKs, seems like we don't have it on our develop docs:

Please also add to the docs: https://github.com/getsentry/develop

@tengl
Copy link

tengl commented Feb 21, 2022

Hi @bruno-garcia

We just updated to Sentry 3.14.1 and changed from WithScope to CaptureMessege etc. where applicable. However, we had some lines with

SentrySdk.WithScope(scope =>
{
    scope.SetTag("ATag", "This is a tag");
    scope.SetExtra("extra", "This is extra");
    _logger.LogWarning("Printing a warning in the console and logging to Sentry");
});

ILogger _logger prints to output AND logs to Sentry including the tag and extra data.

How do we do that with the new API?

@mattjohnsonpint mattjohnsonpint added the Question Further information is requested label May 4, 2022
@mattjohnsonpint mattjohnsonpint self-assigned this May 4, 2022
@bruno-garcia
Copy link
Member Author

You can use:

public static SentryId CaptureMessage(string message, Action<Scope> configureScope, SentryLevel level = SentryLevel.Info)
=> _hub.CaptureMessage(message, configureScope, level);

SentrySdk.CaptureMessage(
	"Printing a warning in the console and logging to Sentry", 
	scope =>
	  {
	      scope.SetTag("ATag", "This is a tag");
	      scope.SetExtra("extra", "This is extra");
	  },
	SentryLevel.Warning);

Please let us know if you have problems so we can reopen

@tengl
Copy link

tengl commented May 11, 2022

@bruno-garcia

Thanks for the reply! I still can't make it work.

Here is the background, we have a generic host that initializes Sentry with the code below (as shown in the generic host sample).

.ConfigureLogging((hostContext, logging) =>
{
    logging.ClearProviders();
    logging.AddSimpleConsole();

    logging.AddSentry(options =>
    {
        options.Environment = hostContext.HostingEnvironment.EnvironmentName;
        options.ConfigureScope(scope =>
        {
            scope.SetTag("SomeTag", "TagValue");
            scope.SetExtras("Extra", "extra extra");
        });
    
        hostContext.Configuration.GetSection("Sentry").Bind(options);
    });
}

I run this code

logger.LogWarning("LOG THIS");
Sentry.SentrySdk.CaptureMessage("TEST2", scope =>
{
    scope.SetTag("ThisTag", "Testing");
    scope.SetExtra("No", "console log");
}, Sentry.SentryLevel.Warning);

The logger.LogWarning works fine, it logs to Sentry with all "default" tags AND logs to console. In my previous example, using SentrySdk.WithScope it would also include extra tags.

The SentrySdk.CaptureMessage do log to Sentry with "default" and extra tags, but it doesn't log to console.

I might have missed something, but I don't know what.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature or request Question Further information is requested
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants