Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Events not sent to Segment servers: "Unexpected Status Code" #161

@mabead

Description

@mabead

In order to get the fix for #156, I used version 3.6.0 of Analytics.Net. Unfortunately, I still get some errors that tell me that messages are lost.

First, here's what my Failed callback looks like:

private void Initialize()
{
    // ...
    client = new SegmentSdk.Client(...);
    client.Failed += LogFailure;
    // ...
}

private void LogFailure(SegmentSdk.Model.BaseAction action, Exception e)
{
    string details = null;
    if (e != null)
    {
        details = e.Message;

        if (e is SegmentSdk.Exception.APIException apiException)
        {
            details = $"Code={apiException.Code}, message={apiException.message}, Message={apiException.Message}";
        }
    }

    _logger.LogError(LoggingEvents.SegmentFailure, e, $"Failed to send analytic event {action.Type} for user {action.UserId} and message {action.MessageId} to segment.io server. Details: {details}");
}

With this, I got these error logs:


timestamp | "json.message"
-- | --
2021-03-12 02:02:53.419 -05:00 | Failed to send analytic event   track for user e0f68aac-7b92-4703-85d3-d967354a5f0a and message   7559433d-46fb-435a-9c50-3bc550cf1e9b to segment.io server. Details:   Code=Unexpected Status Code, message=, Message=     Segment.Exception.APIException
2021-03-12 02:02:53.338 -05:00 | Failed to send analytic event   identify for user e0f68aac-7b92-4703-85d3-d967354a5f0a and message   25935a15-91e1-4fc9-8e0c-ccadaac0fe30 to segment.io server. Details:   Code=Unexpected Status Code, message=, Message=     Segment.Exception.APIException
2021-03-12 02:02:53.256 -05:00 | Failed to send analytic event   track for user e0f68aac-7b92-4703-85d3-d967354a5f0a and message   a1ab9ca6-a4fc-4231-b635-7df0ca46a9a7 to segment.io server. Details:   Code=Unexpected Status Code, message=, Message=     Segment.Exception.APIException
2021-03-12 02:02:53.172 -05:00 | Failed to send analytic event   identify for user e0f68aac-7b92-4703-85d3-d967354a5f0a and message   4c28eca2-5f10-4918-bec3-713b40db1160 to segment.io server. Details:   Code=Unexpected Status Code, message=, Message=     Segment.Exception.APIException
2021-03-12 00:22:59.675 -05:00 | Failed to send analytic event   track for user 9ef95e43-c067-46f0-be51-d1587824095a and message   8417e505-4dad-4f58-af94-deda74882d30 to segment.io server. Details:   Code=Unexpected Status Code, message=, Message=     Segment.Exception.APIException
2021-03-12 00:22:59.672 -05:00 | Failed to send analytic event   identify for user 9ef95e43-c067-46f0-be51-d1587824095a and message   097f39e5-dd64-4b9f-8ed9-daec648471be to segment.io server. Details:   Code=Unexpected Status Code, message=, Message=     Segment.Exception.APIException
2021-03-11 22:24:33.093 -05:00 | Failed to send analytic event   track for user eb845629-2532-4a27-8786-8c37b8413c7a and message   bf41100a-72b0-4769-adc0-db659de47522 to segment.io server. Details:   Code=Unexpected Status Code, message=, Message=     Segment.Exception.APIException
2021-03-11 22:24:33.091 -05:00 | Failed to send analytic event   identify for user eb845629-2532-4a27-8786-8c37b8413c7a and message   f0052abf-1627-41fd-824b-df59ef6a674c to segment.io server. Details:   Code=Unexpected Status Code, message=, Message=     Segment.Exception.APIException

These errors come from this code:
image

On top of fixing the issue, I highly suggest a few code improvements:

  1. get rid of the message property in APIException. It is a duplicate of the Message property in the base class.
  2. enhance your APIException class so that the value of Code ends up diplayed by ToString(). Here's an example of how to do it:
public class APIException : System.Exception
{
    public string Code { get; set; }

    public APIException(string code, string message) : base($"{code}: {message}")
    {
        this.Code = code;
    }
}
  1. enhance the users of the APIException to include more details. For examples, here's the critical piece of code that should be enhanced:
if (_backo.HasReachedMax || statusCode != (int)HttpStatusCode.OK)
{
    var message = $"has reached max = {_backo.HasReachedMax}, statusCode = {statusCode}, response={responseStr}";
    Fail(batch, new APIException("Failed to send message", message), watch.ElapsedMilliseconds);
    if (_backo.HasReachedMax)
    {
        _backo.Reset();
    }
}
  1. Make sure that responseStr is initialized in all error scenarios (including the missing else):

image

  1. Consider calling Logger.Handlers whenever there is a retry in that loop.

Without these changes, we are in the dark and I can't help you with proper logs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions