-
Notifications
You must be signed in to change notification settings - Fork 109
Made working with and modifying payloads within plugins much simpler. #83
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
Changes from 10 commits
7876e92
3ecf832
a29dea8
609a173
885dab9
a78b0c0
4f34209
ab13c32
67f84fd
a40ba36
dd6e260
dba6e05
1959ae7
73ef6cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,3 +91,4 @@ iOSInjectionProject/ | |
| .DS_Store | ||
| Package.resolved | ||
| *.xcuserdatad | ||
| /.swiftpm/xcode/xcshareddata | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // | ||
| // NewRelicErrorReporting.swift | ||
| // | ||
| // | ||
| // Created by Brandon Sneed on 10/6/21. | ||
| // | ||
|
|
||
| // NOTE: You can see this plugin in use in the SwiftUIKitExample application. | ||
| // | ||
| // This plugin is NOT SUPPORTED by Segment. It is here merely as an example, | ||
| // and for your convenience should you find it useful. | ||
|
|
||
| // MIT License | ||
| // | ||
| // Copyright (c) 2021 Segment | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in all | ||
| // copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| // SOFTWARE. | ||
|
|
||
| import Foundation | ||
| import Segment | ||
|
|
||
| protocol NewRelicConversion { | ||
| var dictionaryValue: [String: Any]? { get } | ||
| } | ||
|
|
||
| class NewRelicErrorReporting: Plugin { | ||
| let type = PluginType.enrichment | ||
| var analytics: Analytics? = nil | ||
|
|
||
| internal static let trackName = "__newrelic_error_report" | ||
| internal static let errorKey = "__newrelic_error" | ||
|
|
||
| internal let integrationKey = "New Relic" | ||
|
|
||
| func execute<T: RawEvent>(event: T?) -> T? { | ||
| // is it a track event? if not, ignore it. | ||
| guard let trackEvent = event as? TrackEvent else { return event } | ||
| // is it the track event we're looking for? If not, ignore it. | ||
| guard trackEvent.event == Self.trackName else { return event } | ||
| // we need to make a new integrations object to keep it from going anywhere | ||
| // other than new relic | ||
| guard let newIntegrations = try? JSON([integrationKey: true, "all": false]) | ||
bsneed marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // it's what we're looking for, so lets make sure it *only* goes | ||
| // to New Relic. | ||
| trackEvent.integrations = newIntegrations | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| extension Analytics { | ||
| func reportError(_ error: Error, attributes: [String: Any]?) { | ||
bsneed marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var newAttrs = [String: Any]() | ||
|
|
||
| if let a = attributes { | ||
| newAttrs = a | ||
| } | ||
|
|
||
| if let e = error as? NewRelicConversion, let value = e.dictionaryValue { | ||
| newAttrs[NewRelicErrorReporting.errorKey] = value | ||
| } else { | ||
| newAttrs[NewRelicErrorReporting.errorKey] = error.localizedDescription | ||
| } | ||
|
|
||
| track(name: NewRelicErrorReporting.trackName, properties: newAttrs) | ||
| } | ||
|
|
||
| func reportError<A: Codable>(_ error: Error, attributes: A?) { | ||
| if let a = attributes, let json = try? JSON(with: a) { | ||
| reportError(error, attributes: json.dictionaryValue) | ||
| } else { | ||
| reportError(error, attributes: nil) | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,15 +20,32 @@ internal class SegmentLog: UtilityPlugin { | |
|
|
||
| // For internal use only. Note: This will contain the last created instance | ||
| // of analytics when used in a multi-analytics environment. | ||
| internal static var sharedAnalytics: Analytics? | ||
| internal static var sharedAnalytics: Analytics? = nil | ||
|
|
||
| #if DEBUG | ||
| internal static var globalLogger: SegmentLog { | ||
| get { | ||
| let logger = SegmentLog() | ||
| logger.addTargets() | ||
| return logger | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| required init() { } | ||
|
|
||
| func configure(analytics: Analytics) { | ||
| self.analytics = analytics | ||
| SegmentLog.sharedAnalytics = analytics | ||
| addTargets() | ||
| } | ||
|
|
||
| internal func addTargets() { | ||
| #if !os(Linux) | ||
| try? add(target: SystemTarget(), for: LoggingType.log) | ||
| #if DEBUG | ||
| try? add(target: ConsoleTarget(), for: LoggingType.log) | ||
| #endif | ||
| #else | ||
| try? add(target: ConsoleTarget(), for: LoggingType.log) | ||
| #endif | ||
|
|
@@ -146,16 +163,23 @@ internal extension Analytics { | |
| /// - function: The name of the function the log came from. This will be captured automatically. | ||
| /// - line: The line number in the function the log came from. This will be captured automatically. | ||
| static func segmentLog(message: String, kind: LogFilterKind? = nil, function: String = #function, line: Int = #line) { | ||
| SegmentLog.sharedAnalytics?.apply { plugin in | ||
| if let loggerPlugin = plugin as? SegmentLog { | ||
| var filterKind = loggerPlugin.filterKind | ||
| if let logKind = kind { | ||
| filterKind = logKind | ||
| if let shared = SegmentLog.sharedAnalytics { | ||
| shared.apply { plugin in | ||
| if let loggerPlugin = plugin as? SegmentLog { | ||
| var filterKind = loggerPlugin.filterKind | ||
| if let logKind = kind { | ||
| filterKind = logKind | ||
| } | ||
|
|
||
| let log = LogFactory.buildLog(destination: .log, title: "", message: message, kind: filterKind, function: function, line: line) | ||
| loggerPlugin.log(log, destination: .log) | ||
| } | ||
|
|
||
| let log = LogFactory.buildLog(destination: .log, title: "", message: message, kind: filterKind, function: function, line: line) | ||
| loggerPlugin.log(log, destination: .log) | ||
| } | ||
| } else { | ||
| #if DEBUG | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm debating if we should change this from DEBUG to DEBUGLOG, that way our console doesn't get spammed and we can easily disable this and use Console as a backup. Thoughts?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not really following what you mean here. Please hit me up on slack. |
||
| let log = LogFactory.buildLog(destination: .log, title: "", message: message, kind: .debug, function: function, line: line) | ||
| SegmentLog.globalLogger.log(log, destination: .log) | ||
| #endif | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.