Skip to content

Commit

Permalink
add request data to bugsnag payload
Browse files Browse the repository at this point in the history
  • Loading branch information
jdmcd committed Jul 18, 2018
1 parent fb32e81 commit 6a1b8e1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
18 changes: 17 additions & 1 deletion Sources/Bugsnag/BugsnagPayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ public final class BugsnagPayload: Content {
let severity: String
let user: User
let metadata: Metadata
let request: Request

init(payloadVersion: Int, exceptions: [Exception], app: App, severity: String, user: User, metadata: Metadata) {
init(payloadVersion: Int, exceptions: [Exception], app: App, severity: String, user: User, metadata: Metadata, request: Request) {
self.payloadVersion = payloadVersion
self.exceptions = exceptions
self.app = app
self.severity = severity
self.user = user
self.metadata = metadata
self.request = request
}

public final class Exception: Content {
Expand Down Expand Up @@ -96,6 +98,20 @@ public final class BugsnagPayload: Content {
}
}

public final class Request: Content {
let clientIp: String?
let headers: [String: String]?
let httpMethod: String?
let url: String?

init(clientIp: String?, headers: [String: String]?, httpMethod: String?, url: String?) {
self.clientIp = clientIp
self.headers = headers
self.httpMethod = httpMethod
self.url = url
}
}

public final class Metadata: Content {
let url: String

Expand Down
16 changes: 15 additions & 1 deletion Sources/Bugsnag/PayloadTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,26 @@ internal struct PayloadTransformer: PayloadTransformerType {
let metadata = BugsnagPayload.Event.Metadata(url: request?.http.urlString ?? "")
let app = BugsnagPayload.Event.App(releaseStage: environment.name, type: "Vapor")

var headersDict = [String: String]()

if let req = request {
for header in req.http.headers {
headersDict[header.name] = header.value
}
}

let requestContent = BugsnagPayload.Event.Request(clientIp: request?.http.remotePeer.hostname,
headers: headersDict,
httpMethod: request?.http.method.string,
url: request?.http.url.path)

let event = BugsnagPayload.Event(payloadVersion: 2,
exceptions: [exception],
app: app,
severity: severity.rawValue,
user: BugsnagPayload.Event.User(id: userId, name: userName, email: userEmail),
metadata: metadata)
metadata: metadata,
request: requestContent)

let notifier = BugsnagPayload.Notifier(name: "Bugsnag Vapor", version: "2.0.0", url: "https://github.com/gotranseo/bugsnag")
let payload = BugsnagPayload(apiKey: apiKey, notifier: notifier, events: [event])
Expand Down

0 comments on commit 6a1b8e1

Please sign in to comment.