Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ iOSInjectionProject/
.DS_Store
Package.resolved
*.xcuserdatad
/.swiftpm/xcode/xcshareddata
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
46871693270E16080028B595 /* NewRelicErrorReporting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4687168D270E16080028B595 /* NewRelicErrorReporting.swift */; };
46871694270E16080028B595 /* NotificationTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4687168E270E16080028B595 /* NotificationTracking.swift */; };
46871695270E16080028B595 /* UIKitScreenTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4687168F270E16080028B595 /* UIKitScreenTracking.swift */; };
46871696270E16080028B595 /* ConsentTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46871690270E16080028B595 /* ConsentTracking.swift */; };
46871697270E16080028B595 /* IDFACollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46871691270E16080028B595 /* IDFACollection.swift */; };
46871698270E16080028B595 /* ConsoleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46871692270E16080028B595 /* ConsoleLogger.swift */; };
469EC8D0266066130068F9E3 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 469EC8CF266066130068F9E3 /* SystemConfiguration.framework */; };
469EC8E0266828860068F9E3 /* FlurryAnalyticsSPM in Frameworks */ = {isa = PBXBuildFile; productRef = 469EC8DF266828860068F9E3 /* FlurryAnalyticsSPM */; };
469F7B08266011690038E773 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 469F7B07266011690038E773 /* AppDelegate.swift */; };
Expand Down Expand Up @@ -36,6 +42,12 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
4687168D270E16080028B595 /* NewRelicErrorReporting.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NewRelicErrorReporting.swift; sourceTree = "<group>"; };
4687168E270E16080028B595 /* NotificationTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationTracking.swift; sourceTree = "<group>"; };
4687168F270E16080028B595 /* UIKitScreenTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKitScreenTracking.swift; sourceTree = "<group>"; };
46871690270E16080028B595 /* ConsentTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsentTracking.swift; sourceTree = "<group>"; };
46871691270E16080028B595 /* IDFACollection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IDFACollection.swift; sourceTree = "<group>"; };
46871692270E16080028B595 /* ConsoleLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsoleLogger.swift; sourceTree = "<group>"; };
469EC8CF266066130068F9E3 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
469EC8E1266828AF0068F9E3 /* DestinationsExample-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "DestinationsExample-Bridging-Header.h"; sourceTree = "<group>"; };
469F7B04266011690038E773 /* DestinationsExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DestinationsExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -80,6 +92,20 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
4687168C270E16080028B595 /* other_plugins */ = {
isa = PBXGroup;
children = (
4687168D270E16080028B595 /* NewRelicErrorReporting.swift */,
4687168E270E16080028B595 /* NotificationTracking.swift */,
4687168F270E16080028B595 /* UIKitScreenTracking.swift */,
46871690270E16080028B595 /* ConsentTracking.swift */,
46871691270E16080028B595 /* IDFACollection.swift */,
46871692270E16080028B595 /* ConsoleLogger.swift */,
);
name = other_plugins;
path = ../../../other_plugins;
sourceTree = "<group>";
};
469F7AFB266011690038E773 = {
isa = PBXGroup;
children = (
Expand All @@ -101,6 +127,7 @@
isa = PBXGroup;
children = (
BA384C9D2686609000AFEA1B /* DestinationsExample.entitlements */,
4687168C270E16080028B595 /* other_plugins */,
469F7B1E266012CB0038E773 /* destination_plugins */,
469F7B07266011690038E773 /* AppDelegate.swift */,
469F7B09266011690038E773 /* SceneDelegate.swift */,
Expand Down Expand Up @@ -241,6 +268,7 @@
buildActionMask = 2147483647;
files = (
BA384C9A2682973300AFEA1B /* AppsFlyerDestination.swift in Sources */,
46871698270E16080028B595 /* ConsoleLogger.swift in Sources */,
469F7B20266012CB0038E773 /* FlurryDestination.swift in Sources */,
469F7B0C266011690038E773 /* ViewController.swift in Sources */,
96469A9B270279A600AC5772 /* IntercomDestination.swift in Sources */,
Expand All @@ -249,9 +277,14 @@
96DBF37D26FA943300724B0B /* ComscoreDestination.swift in Sources */,
965DC0FB2668077400DDF9C7 /* AmplitudeSession.swift in Sources */,
469F7B08266011690038E773 /* AppDelegate.swift in Sources */,
46871694270E16080028B595 /* NotificationTracking.swift in Sources */,
469F7B25266013320038E773 /* AdjustDestination.swift in Sources */,
469F7B0A266011690038E773 /* SceneDelegate.swift in Sources */,
965DC1232669947F00DDF9C7 /* FirebaseDestination.swift in Sources */,
46871693270E16080028B595 /* NewRelicErrorReporting.swift in Sources */,
46871695270E16080028B595 /* UIKitScreenTracking.swift in Sources */,
46871696270E16080028B595 /* ConsentTracking.swift in Sources */,
46871697270E16080028B595 /* IDFACollection.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
91 changes: 91 additions & 0 deletions Examples/other_plugins/NewRelicErrorReporting.swift
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])

// 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]?) {
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)
}
}
}
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 33 additions & 9 deletions Sources/Segment/Plugins/Logger/SegmentLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/Segment/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ extension Settings: Equatable {
}

extension Analytics {
public func manuallyEnableDestination(plugin: DestinationPlugin) {
self.store.dispatch(action: System.AddIntegrationAction(key: plugin.key))
}

internal func update(settings: Settings, type: UpdateType) {
apply { (plugin) in
// tell all top level plugins to update.
Expand Down
Loading