Instabug / Instabug-iOS

In-app feedback and bug reporting tool for apps.
https://instabug.com/
Other
288 stars 67 forks source link

Omitting Requests from Logs not working #474

Closed hajducak closed 8 months ago

hajducak commented 9 months ago

Steps to Reproduce the Problem

https://docs.instabug.com/reference/network-logging-ios#omitting-requests-from-logs Based on your documentation, implement this code

let path = "/products"
let requestPredicate = NSPredicate(format: "URL.path MATCHES %@", path)
let responsePredicate = NSPredicate(format: "statusCode >= %d AND statusCode <= %d", 200, 399)
NetworkLogger.setNetworkLoggingRequestFilterPredicate(requestPredicate, responseFilterPredicate: responsePredicate)

Expected Behavior

The logs should contain just api calls with UrlPath "/products"

Actual Behavior

All api calls are logged

Instabug integration code

We are sending only token as I see in code.

SDK Version

11.13.3

iOS Version

iOS 16.4

Device Model

iPhone14 Pro

My code

On project we need omitting requests based on URL host, we have collection of host that are allowed to log into instabug. So I came up with this predicate : let predicate = NSPredicate(format: "URL.host IN %@", hostCollection) where hostCollection is just array of url hosts.

let hostCollection = [
    "https://www.google.com"
     // ... more different hosts alowing to log in instabug
  ]
let requestPredicate =  NSPredicate(format: "URL.host IN %@", hostCollection)
NetworkLogger.setNetworkLoggingRequestFilterPredicate(requestPredicate, responseFilterPredicate: nil)

This code is not working, after making instabug, I check logs and there are all request as before implementation. I try even your approach in documentation and try filtering jut path. Either not working.

Then I tried make some tests for NSPredicate to test my Predicates if there were wrong. Of course they where. First I tried test path filtering:

    func test_Predicate_1() {
        let path = "/products"
        let predicate = NSPredicate(format: "URL.path MATCHES %@", path)

        let testURL: URL = URL(string: "https://www.google.com/products")!
        let evaluatedUrl: Bool = predicate.evaluate(with: testURL)
        XCTAssertTrue(evaluatedUrl)

        expectation(for: predicate, evaluatedWith: testURL, handler: nil)
        waitForExpectations(timeout: 10) { error in
            if let error = error {
                print("expectations has not been fulfullied. Failed with error \(error)")
            } else {
                print("expectations has been fullfilled.")
            }
        }
    }

Evaluation error: test_Predicate_1(): [<NSURL 0x600000bdee60> valueForUndefinedKey:]: this class is not key value coding-compliant for the key URL. (NSUnknownKeyException)

Then I try the host In sollution:

    func test_predicate_2() {
        let hostCollection = [
            "https://www.google.com"
            // ... more different hosts alowing to log in instabug
        ]
        let predicate = NSPredicate(format: "URL.host IN %@", hostCollection)
        let testURL: URL = URL(string: "https://www.google.com/products")!
        let evaluatedUrl: Bool = predicate.evaluate(with: testURL)
        XCTAssertTrue(evaluatedUrl)

        expectation(for: predicate, evaluatedWith: testURL, handler: nil)
        waitForExpectations(timeout: 10) { error in
            if let error = error {
                print("expectations has not been fulfullied. Failed with error \(error)")
            } else {
                print("expectations has been fullfilled.")
            }
        }
    }

Same error occurs as first time. Okey that error should said that the URL is not compatible with my predicate. As I looking for answer I found out that the NSPredicate doesn't support URL so I tried another approach with no URL inside predicate.

func test_predicate_3() {
        let hostCollectionURLS = [
            URL(string: "https://www.google.com")
            // ... more different hosts alowing to log in instabug
        ]
        let hostCollection = hostCollectionURLS.compactMap { $0?.host }

        let predicateFormat = "SELF.host IN %@"
        let predicate = NSPredicate(format: predicateFormat, argumentArray: hostCollection)

        let testURL: URL = URL(string: "https://www.google.com/products")!
        let evaluatedUrl: Bool = predicate.evaluate(with: testURL)
        XCTAssertTrue(evaluatedUrl)

        expectation(for: predicate, evaluatedWith: testURL, handler: nil)
        waitForExpectations(timeout: 10) { error in
            if let error = error {
                print("expectations has not been fulfullied. Failed with error \(error)")
            } else {
                print("expectations has been fullfilled.")
            }
        }
    }

This test was successful and correct. So I implanted inside our instabug and try it.

    static func setOmittingRequests() {
        let hostCollectionURLS = [
            URL(string: "https://...."),
            // ... another URLs
        ]
        let hostCollection = hostCollectionURLS.compactMap { $0?.host }
        let predicateFormat = "SELF.host IN %@"
        let predicate = NSPredicate(format: predicateFormat, argumentArray: hostCollection)
        NetworkLogger.setNetworkLoggingRequestFilterPredicate(predicate, responseFilterPredicate: nil)
    }

Instabug I created after implementing this code was omitting all request as before without any changes. After this steps I assume that framework have issue. I can't see inside your code so I don't know what you are doing with that NSPredicate.

Thanks for any suggestions further.

KerollosNabil commented 9 months ago

Hi @hajducak i am kerollos nabil iOS developer at instabug thank you for communicating with us

at your snippet of code

let hostCollection = [
    "https://www.google.com/"
     // ... more different hosts alowing to log in instabug
  ]
let requestPredicate =  NSPredicate(format: "URL.host IN %@", hostCollection)
NetworkLogger.setNetworkLoggingRequestFilterPredicate(requestPredicate, responseFilterPredicate: nil)

you are writing the host in the hostCollection list like that "https://www.google.com" and we are running this predicate against an object of type NSURLRequest and the NSURL object inside NSURLRequest (URL) breaks this link into

so when we run the predicate he try to find the host " www.Google.com" in your list of hosts it fails to find it so you only need to remove "https://" from the list of hosts to be like that

let hostCollection = [
    "www.google.com"
     // ... more different hosts alowing to log in instabug
  ]
let requestPredicate =  NSPredicate(format: "URL.host IN %@", hostCollection)
NetworkLogger.setNetworkLoggingRequestFilterPredicate(requestPredicate, responseFilterPredicate: nil)

this will work as expected

and for the tests you just need to use the predicate against NSURLRequest object instead of URL

this test for the path will pass

func test_Predicate_1() {
    let path = "/products"
    let predicate = NSPredicate(format: "URL.path MATCHES %@", path)

    let testURL: URL = URL(string: "https://www.google.com/products")!
    let urlRequest = NSURLRequest(url: testURL)
    let evaluatedUrl: Bool = predicate.evaluate(with: urlRequest)
    XCTAssertTrue(evaluatedUrl)

    expectation(for: predicate, evaluatedWith: urlRequest, handler: nil)
    waitForExpectations(timeout: 10) { error in
      if let error = error {
        print("expectations has not been fulfullied. Failed with error \(error)")
      } else {
        print("expectations has been fullfilled.")
      }
    }
  }

and this test for hosts will pass as well

func test_predicate_2() {
    let hostCollection = [
      "www.google.com"
      // ... more different hosts alowing to log in instabug
    ]
    let predicate = NSPredicate(format: "URL.host IN %@", hostCollection)
    let testURL: URL = URL(string: "https://www.google.com/products")!
    let urlRequest = NSURLRequest(url: testURL)
    let evaluatedUrl: Bool = predicate.evaluate(with: urlRequest)
    XCTAssertTrue(evaluatedUrl)

    expectation(for: predicate, evaluatedWith: urlRequest, handler: nil)
    waitForExpectations(timeout: 10) { error in
      if let error = error {
        print("expectations has not been fulfullied. Failed with error \(error)")
      } else {
        print("expectations has been fullfilled.")
      }
    }
  }
hajducak commented 9 months ago

Hello @KerollosNabil thanks for explanation. Unfortunately after editing the code Instabug still logging hosts which we don't want to log. Yeah test passes correctly as you write. This is my code I am sending to Instabug:

static func setOmittingRequests() {
        let hostCollection = [
            "login-ppt-a-moje.o2.cz",
            "login-a-moje.o2.cz",
            "login-e3-a-moje.o2.cz",
            "api-ppt-a-moje.o2.cz",
            "api-a-moje.o2.cz",
            "api-e3-a-moje.o2.cz",
            "api-eme-ppt-a-moje.o2.cz",
            "api-eme-a-moje.o2.cz",
            "api-eme-e3-a-moje.o2.cz",
            "api-az-a-moje.o2.cz",
            "api-az-ppt-a-moje.o2.cz",
            "api-az-e3-a-moje.o2.cz"
        ]
        let predicateFormat = "URL.host IN %@"
        let predicate = NSPredicate(format: predicateFormat, argumentArray: hostCollection)
        NetworkLogger.setNetworkLoggingRequestFilterPredicate(predicate, responseFilterPredicate: nil)
    }

I checked if the code is calling at the beginning of our app. It is. I don't know what is wrong in this case. Can you help with this also?

stale[bot] commented 9 months ago

This issue has been automatically marked as stale due to inactivity. It will be closed if no further activity occurs. Thank you for your contributions.

hajducak commented 9 months ago

@KerollosNabil Instabug SDK still not working in this issue, pls reopen this issue. I would love to see repair from your side. Thanks

KerollosNabil commented 9 months ago

@hajducak Could you kindly get in touch with our customer support team so that they can assist you in initiating a ticket for you to able to investigate more in your problem

stale[bot] commented 9 months ago

This issue has been automatically marked as stale due to inactivity. It will be closed if no further activity occurs. Thank you for your contributions.

hajducak commented 9 months ago

@KerollosNabil I contact customer support 3 days ago, nobody send me any response.

stale[bot] commented 8 months ago

This issue has been automatically marked as stale due to inactivity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 8 months ago

This issue has been automatically closed due to inactivity.