Skip to content

Commit

Permalink
Implement timeSinceHttpRequest attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
asllop committed Apr 22, 2022
1 parent 6d35775 commit 03f01c4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# CHANGELOG
All notable changes to this project will be documented in this file.

## [2.1.2] - 2022/04/21
### Add
- `timeSinceHttpRequest` attribute into `HTTP_RESPONSE` events.

## [2.1.1] - 2022/03/08
### Add
- `counter` attribute into `HTTP_ERROR` events, with value 1, to simplify HTTP related NRQL requests.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ There is a set of attributes common to all actions sent over a `RokuSystem` and
| `http*` | Multiple attributes. All the header keys. | `HTTP_RESPONSE` |
| `transferIdentity` | HTTP request identificator. | `HTTP_REQUEST`, `HTTP_RESPONSE` |
| `sceneName` | Identifier of the scene. | `SCENE_LOADED` |
| `timeSinceHttpRequest` | Time since `HTTP_REQUEST` in milliseconds. | `HTTP_RESPONSE` |

<a name="roku-video"></a>

Expand Down
33 changes: 31 additions & 2 deletions components/NewRelicAgent/NRAgent.brs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ end sub
'=========================='

function NewRelicInit(account as String, apikey as String, region as String) as Void
'Session
m.nrAccountNumber = account
m.nrInsightsApiKey = apikey
m.nrRegion = region
m.nrSessionId = nrGenerateId()

'Reservoir sampling for events
m.nrEventArray = []
m.nrEventArrayIndex = 0
Expand All @@ -49,14 +49,16 @@ function NewRelicInit(account as String, apikey as String, region as String) as
m.nrLogHarvestTimeNormal = 60
m.nrLogHarvestTimeMax = 600
m.nrLogHarvestTimeDelta = 60

'Groups and attributes
m.nrEventGroupsConnect = CreateObject("roAssociativeArray")
m.nrEventGroupsComplete = CreateObject("roAssociativeArray")
m.nrGroupingPatternCallback = invalid
m.nrBackupAttributes = CreateObject("roAssociativeArray")
m.nrCustomAttributes = CreateObject("roAssociativeArray")
m.nrLastTimestamp = 0
m.nrTicks = 0
'HTTP Request/Response IDs
m.nrRequestIdentifiers = CreateObject("roAssociativeArray")

date = CreateObject("roDateTime")
m.nrInitTimestamp = date.AsSeconds()
Expand Down Expand Up @@ -204,6 +206,33 @@ function nrSendVideoEvent(actionName as String, attr = invalid) as Void
m.nrBackupAttributes.Append(ev)
end function

function nrSendHttpRequest(attr as Object) as Void
transId = stri(attr["transferIdentity"])
m.nrRequestIdentifiers[transId] = nrTimestamp()
'Clean up old transfers
toDeleteKeys = []
for each item in m.nrRequestIdentifiers.Items()
'More than 2 minutes without a response
if nrTimestamp() - item.value > 120000
toDeleteKeys.Push(item.key)
end if
end for
for each key in toDeleteKeys
m.nrRequestIdentifiers.Delete(key)
end for
nrSendCustomEvent("RokuSystem", "HTTP_REQUEST", attr)
end function

function nrSendHttpResponse(attr as Object) as Void
transId = stri(attr["transferIdentity"])
if m.nrRequestIdentifiers[transId] <> invalid
deltaMs = nrTimestamp() - m.nrRequestIdentifiers[transId]
attr["timeSinceHttpRequest"] = deltaMs
m.nrRequestIdentifiers.Delete(transId)
end if
nrSendCustomEvent("RokuSystem", "HTTP_RESPONSE", attr)
end function

function nrSetCustomAttribute(key as String, value as Object, actionName = "" as String) as Void
dict = CreateObject("roAssociativeArray")
dict[key] = value
Expand Down
4 changes: 3 additions & 1 deletion components/NewRelicAgent/NRAgent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<interface>
<!-- Properties -->
<field id="version" type="string" value="2.1.0"/>
<field id="version" type="string" value="2.1.2"/>
<field id="patternGen" type="node"/>
<!-- Public Methods (wrapped) -->
<function name="NewRelicInit"/>
Expand All @@ -25,6 +25,8 @@
<function name="nrSendCustomEvent"/>
<function name="nrSendSystemEvent"/>
<function name="nrSendVideoEvent"/>
<function name="nrSendHttpRequest"/>
<function name="nrSendHttpResponse"/>
<function name="nrSetCustomAttribute"/>
<function name="nrSetCustomAttributeList"/>
<function name="nrSetHarvestTime"/>
Expand Down
70 changes: 34 additions & 36 deletions source/NewRelicAgent.brs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,14 @@ end function
' @param nr New Relic Agent object.
' @param urlReq URL request, roUrlTransfer object.
function nrSendHttpRequest(nr as Object, urlReq as Object) as Void
if type(urlReq) <> "roUrlTransfer" return

attr = {
"origUrl": urlReq.GetUrl(),
"transferIdentity": urlReq.GetIdentity(),
"method": urlReq.GetRequest()
}

nrSendCustomEvent(nr, "RokuSystem", "HTTP_REQUEST", attr)
if type(urlReq) = "roUrlTransfer"
attr = {
"origUrl": urlReq.GetUrl(),
"transferIdentity": urlReq.GetIdentity(),
"method": urlReq.GetRequest()
}
nr.callFunc("nrSendHttpRequest", attr)
end if
end function

' Send an HTTP_RESPONSE event of type RokuSystem.
Expand All @@ -156,34 +155,33 @@ end function
' @param _url Request URL.
' @param msg A message of type roUrlEvent.
function nrSendHttpResponse(nr as Object, _url as String, msg as Object) as Void

if type(msg) <> "roUrlEvent" return

attr = {
"origUrl": _url
}

attr.AddReplace("httpCode", msg.GetResponseCode())
attr.AddReplace("httpResult", msg.GetFailureReason())
attr.AddReplace("transferIdentity", msg.GetSourceIdentity())

header = msg.GetResponseHeaders()

for each key in header
parts = key.Tokenize("-")
finalKey = "http"
finalValue = header[key]
for each part in parts
firstChar = Left(part, 1)
firstChar = UCase(firstChar)
restStr = Right(part, Len(part) - 1)
restStr = LCase(restStr)
finalKey = finalKey + firstChar + restStr
if type(msg) = "roUrlEvent"
attr = {
"origUrl": _url
}

attr.AddReplace("httpCode", msg.GetResponseCode())
attr.AddReplace("httpResult", msg.GetFailureReason())
attr.AddReplace("transferIdentity", msg.GetSourceIdentity())
header = msg.GetResponseHeaders()
for each key in header
parts = key.Tokenize("-")
finalKey = "http"
finalValue = header[key]
for each part in parts
firstChar = Left(part, 1)
firstChar = UCase(firstChar)
restStr = Right(part, Len(part) - 1)
restStr = LCase(restStr)
finalKey = finalKey + firstChar + restStr
end for
attr.AddReplace(finalKey, finalValue)
end for
attr.AddReplace(finalKey, finalValue)
end for

nrSendCustomEvent(nr, "RokuSystem", "HTTP_RESPONSE", attr)

nr.callFunc("nrSendHttpResponse", attr)
end if
end function

' Set harvest time, the time the events are buffered before being sent to Insights.
Expand Down

0 comments on commit 03f01c4

Please sign in to comment.