Skip to content

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

Closed

Description

@bruno-garcia

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

Activity

  1. added
    Feature
    on Dec 22, 2020
  2. bruno-garcia commented on Dec 7, 2021

    @bruno-garcia
    MemberAuthor

    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

  3. tengl commented on Feb 21, 2022

    @tengl

    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?

  4. added
    Question
    on May 4, 2022
  5. bruno-garcia commented on May 10, 2022

    @bruno-garcia
    MemberAuthor

    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

  6. tengl commented on May 11, 2022

    @tengl

    @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

Metadata

Metadata

Labels

FeatureQuestion

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions