Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Allow service names #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ The Fastly to Insights image is configured by environment variables. These are m
* `INSERT_KEY`
* `SERVICES`

`SERVICES` needs to be a string with the ids of the Fastly services you want to see data for in Insights, separated by a space. I know that's not ideal. A limitation of Fastly is that you have to query one service at a time, so I chose to create an array of service ids and loop through them to query Fastly. A limitation of Docker is that you can't pass an array via the command line, so I chose to split a string on "` `". If you have a better idea, I would love to hear it - please contribute!
`SERVICES` needs to be a string with the ids of the Fastly services, or SERVICE_NAME:SERVICE_ID pairs (Insights will receive `service_name`) you want to see data for in Insights, separated by a space. I know that's not ideal. A limitation of Fastly is that you have to query one service at a time, so I chose to create an array of service ids and loop through them to query Fastly. A limitation of Docker is that you can't pass an array via the command line, so I chose to split a string on "` `". If you have a better idea, I would love to hear it - please contribute!

### Example

34 changes: 23 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
@@ -9,17 +9,25 @@ const FASTLY_KEY = process.env.FASTLY_KEY
const LIST_OF_SERVICES = process.env.SERVICES.split(" ")

const request = require('request')
const call_frequency = 60

let timestamp = 0
let timestamp = Math.round(Date.now()/1000) - call_frequency
let send_count = 0

get_send_count = ( () =>
send_count = send_count + 1
)

setInterval( () => {
//This will create 1920 events per day, or 13440 events per week.
LIST_OF_SERVICES.map( (service) => {
pollFromFastly(service)
LIST_OF_SERVICES.map( (service_entry) => {
pollFromFastly(service_entry)
})
}, 180000) // 3 minutes
}, 1000 * call_frequency)

pollFromFastly = (service) => {
pollFromFastly = (service_entry) => {
let [service_name, service] = service_entry.split(":")
service = service || service_name; // Make it backward compatible
let fastly_url = "https://rt.fastly.com/v1/channel/" + service + "/ts/" + timestamp
request({
method: "GET",
@@ -32,19 +40,22 @@ pollFromFastly = (service) => {
if (response) {
let valuableData = JSON.parse(response.body)
timestamp = valuableData.Timestamp
console.log("New timestamp for " + service_name + " is " + timestamp)
console.log("SIZE=" + valuableData.Data.length + " for " + fastly_url )
valuableData.Data.map( (data) => {
batchAndSend(data.aggregated, service)
if(data.aggregated) batchAndSend(data.aggregated, service, service_name)
})
}
if (err) console.log(err)
})
}

let batchAndSend = (aggregate, service) => {
let batchAndSend = (aggregate, service, service_name) => {

let message = {
"eventType": "LogAggregate",
"eventType": "LogAggregateFastly",
"service": service,
"service_name": service_name,
/*
These are all the attributes that Fastly returns. I included every one; if there are any that are less interesting to you feel free to delete those.

@@ -160,6 +171,7 @@ let batchAndSend = (aggregate, service) => {
}

sendToInsights = (logMessages) => {
const my_send_count = get_send_count()
let insights_url = 'https://insights-collector.newrelic.com/v1/accounts/'+ ACCOUNT_ID +'/events'
request({
method: "POST",
@@ -170,7 +182,7 @@ sendToInsights = (logMessages) => {
},
body: JSON.stringify(logMessages)
}, (err, response, body) => {
console.log(body)
console.log(err)
console.log("Sent message (" + my_send_count + ") for " + logMessages.service_name + " at " + Date().toLocaleString() + ". Response: " + body)
if(err) console.log("Error: " + err);
})
}
}
8 changes: 8 additions & 0 deletions build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash -x
set -e

BRANCH=$(git branch | grep \* | cut -d ' ' -f2)
GIT_SHA=$(git rev-parse --short HEAD)
VERSION="0.1.0-${BRANCH}-${GIT_SHA}"
docker build -t quay.io/weightwatchers/fastly-to-insights:${VERSION} .
docker push quay.io/weightwatchers/fastly-to-insights:${VERSION}