Skip to content

Instantly share code, notes, and snippets.

@erichexter
Created October 22, 2014 19:58
Show Gist options
  • Save erichexter/ca1d3623a7859d512885 to your computer and use it in GitHub Desktop.
Save erichexter/ca1d3623a7859d512885 to your computer and use it in GitHub Desktop.
send SQL AZURE Performance Data to TelemetryApp.com
void Main()
{
do{
try{
var context=new QsContext();
var connection=context.Database.Connection as SqlConnection;
List<dynamic> results= new List<dynamic>();
SqlCommand command = new SqlCommand("SELECT top 1 * FROM sys.dm_db_resource_stats order by end_Time desc",connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
dynamic o = new ExpandoObject();
o.time= reader[0];
o.cpu= reader[1];
o.data= reader[2];
o.log= reader[3];
o.memory= reader[4];
results.Add(o);
}
}
reader.Close();
SendData("sql-cpu-month","SQL CPU 30 days","days",30,results.First().cpu,"CPU");
SendData("sql-cpu-day","SQL CPU 1 week","hours",168,results.First().cpu,"CPU");
SendData("sql-cpu-hour","SQL CPU 9 hours","minutes",540,results.First().cpu,"CPU");
SendData("sql-cpu-now","current","CPU",results.First().cpu);
SendData("sql-data-month","SQL Disk 30 days","days",30,results.First().data,"DISK");
SendData("sql-data-day","SQL Disk 1 week","hours",168,results.First().data,"DISK");
SendData("sql-data-hour","SQL Disk 9 hours","minutes",540,results.First().data,"DISK");
SendData("sql-data-now","current","DISK",results.First().data);
SendData("sql-memory-month","SQL Memory 30 days","days",30,results.First().memory,"MEMORY");
SendData("sql-memory-day","SQL Memory 1 week","hours",168,results.First().memory,"MEMORY");
SendData("sql-memory-hour","SQL Memory 9 hours","minutes",540,results.First().memory,"MEMORY");
SendData("sql-memory-now","current","MEMORY",results.First().memory);
}
catch{
//throw;
}
Thread.Sleep(TimeSpan.FromSeconds(30));
}while(true);
}
void SendData(string flow,string title,string label,object data){
var url = "https://api.telemetryapp.com/flows/"+flow+"/data";
//url.Dump();
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
byte[] authBytes = Encoding.UTF8.GetBytes("APITOKEN___PUT_YOURS_HERE:"); // API Token, don't forget the colon
httpWebRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(authBytes));
httpWebRequest.PreAuthenticate = true;
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json ="{\"title\":\""+title+"\",\"label\":\""+label+"\",\"value\":"+data+"}";
//json.Dump();
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
void SendData(string flow,string title,string interval,int intervalcount,object data,string label){
var url = "https://api.telemetryapp.com/flows/"+flow+"/data";
//url.Dump();
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
byte[] authBytes = Encoding.UTF8.GetBytes("APITOKEN___PUT_YOURS_HERE:"); // API Token, don't forget the colon
httpWebRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(authBytes));
httpWebRequest.PreAuthenticate = true;
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{" +
"\"title\": \""+title+"\"," +
"\"interval\": \""+interval+"\"," +
"\"interval_count\": "+intervalcount+"," +
"\"series_metadata\": [" +
"{" +
"\"color\": \"rgb(255,255,0)\"," +
"\"aggregation\": \"avg\"," +
"\"label\": \""+label+"\"" +
"}" +
"]," +
"\"values\": [" + data.ToString() +"]" +
"}";
//json.Dump();
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
// Define other methods and classes here
@erichexter
Copy link
Author

Make sure you replace the API Token for Telemetry, look for the APITOKEN___PUT_YOURS_HERE and replace that with your token.

In Telemetry you need to setup 9 TimeSeries Flows and 3 Gauges, using the following naming convention:
sql-cpu-month timeseries
sql-cpu-day timeseries
sql-cpu-hour timeseries
sql-cpu-now gauge

sql-data-month timeseries
sql-data-day timeseries
sql-data-hour timeseries
sql-data-now gauge

sql-memory-month timeseries
sql-memory-day timeseries
sql-memory-hour timeseries
sql-memory-now gauge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment