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

Prevent setUserId nil log #343

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

cdillard-NewRelic
Copy link
Member

No description provided.

@@ -90,6 +90,10 @@ - (BOOL) checkAttribute:(NSString*)name value:(id)value {
BOOL validAttribute = [attributeValidator nameValidator:name];
BOOL validValue = [attributeValidator valueValidator:value];

if (name == kNRMA_Attrib_userId && !value ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want this to continue being an actual nil value, or do we want to put a placeholder value in for when it gets parsed on the backend?

Copy link
Member Author

@cdillard-NewRelic cdillard-NewRelic Jan 30, 2025

Choose a reason for hiding this comment

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

This is a good question. It appears to me that userId is not a real "reserved" NR session attribute. Essentially an additional layer was added on the user attribute named "userId"

In our current impl we set the userId with

    BOOL success = [[NewRelicAgentInternal sharedInstance].analyticsController setSessionAttribute:kNRMA_Attrib_userId
                                                                                             value:userId
                                                                                        persistent:YES];

and if setting to nil we also call to remove

    // If passed userId == NULL , remove UserId attribute.
    if (userId == NULL) {
        success = [[NewRelicAgentInternal sharedInstance].analyticsController removeSessionAttributeNamed:kNRMA_Attrib_userId];
    }
+ (BOOL) setUserId:(NSString* _Nullable)userId {
    NSString *previousUserId = [[NewRelicAgentInternal sharedInstance] getUserId];
    BOOL newSession = false;
    // If the client passes a new userId that is non NULL.
    if (userId != NULL) {
        // A new userId has been set where the previously set one (during this app session (since app launch)) was not NULL or the previous set one was NULL, we start a new session.
        newSession = true;
    }
    // If the client passes a new NULL userId.
    else {
        if (previousUserId != NULL) {
            // end session and harvest.
            newSession = true;
        }
        // Do nothing if passed userId is null and saved userId (for this app session (since app launch)) is null.
    }

    // Update in memory userId.
    [NewRelicAgentInternal sharedInstance].userId = userId;

    if (newSession) {
        [[[NewRelicAgentInternal sharedInstance] analyticsController] newSession];

        // Perform harvest
        [self harvestNow];

        [[NewRelicAgentInternal sharedInstance] sessionStartInitialization];
    }

    BOOL success = [[NewRelicAgentInternal sharedInstance].analyticsController setSessionAttribute:kNRMA_Attrib_userId
                                                                                             value:userId
                                                                                        persistent:YES];
    // If passed userId == NULL , remove UserId attribute.
    if (userId == NULL) {
        success = [[NewRelicAgentInternal sharedInstance].analyticsController removeSessionAttributeNamed:kNRMA_Attrib_userId];
    }

    return success;
}

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