Skip to content
Merged
Show file tree
Hide file tree
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
53 changes: 49 additions & 4 deletions create_function_toddog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ show err

prompt create function f_metrictoddog
CREATE OR REPLACE FUNCTION f_metrictoddog (
name IN VARCHAR2,
name IN VARCHAR2,
metric IN BINARY_INTEGER,
kind IN VARCHAR2,
tag IN VARCHAR2)
kind IN VARCHAR2,
tag IN VARCHAR2)
RETURN VARCHAR2 AS LANGUAGE C
NAME "metrictoddog"
LIBRARY libtoddog
Expand All @@ -29,6 +29,29 @@ PARAMETERS (
/
show err

prompt create function f_eventtoddog
CREATE OR REPLACE FUNCTION f_eventtoddog (
title IN VARCHAR2,
text IN VARCHAR2,
tag IN VARCHAR2)
RETURN VARCHAR2 AS LANGUAGE C
NAME "eventtoddog"
LIBRARY libtoddog
WITH CONTEXT
PARAMETERS (
CONTEXT,
title STRING,
title INDICATOR short,
text STRING,
text INDICATOR short,
tag STRING,
tag INDICATOR short,
RETURN INDICATOR short,
RETURN LENGTH short,
RETURN STRING);
/
show err

CREATE OR REPLACE FUNCTION f_gaugetoddog(
name IN VARCHAR2,
metric IN number,
Expand Down Expand Up @@ -89,6 +112,28 @@ begin
tag := 'source:plsql';
result := f_counttoddog(name,metric,tag);
dbms_output.put_line(result);
EXCEPTION
WHEN OTHERS THEN
err_num := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 100);
raise_application_error(err_num, err_msg);
END;
/
show err

declare
title varchar2(50);
text varchar2(50);
tag varchar2(50);
result varchar2(10);
err_num NUMBER;
err_msg VARCHAR2(100);
begin
title := 'title of event';
text := 'text for event';
tag := 'source:plsql';
result := f_eventtoddog(title,text,tag);
dbms_output.put_line(result);
EXCEPTION
WHEN OTHERS THEN
err_num := SQLCODE;
Expand All @@ -98,4 +143,4 @@ END;
/
show err

exit
exit
32 changes: 32 additions & 0 deletions samples/errtoevent.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
set serveroutput on

declare
name varchar2(50);
tag varchar2(50);
result varchar2(10);
cursor c_table is
select count(sysdate) as c from dual;
errorsysdate EXCEPTION;
err_num NUMBER;
err_msg VARCHAR2(100);
event_rmsg VARCHAR2(100);
begin
for t in c_table loop
result := t.c;
if result<2 then
event_rmsg := f_eventtoddog('sample event','sample: error counting sysdate, expected 2 or more','sample');
RAISE errorsysdate;
end if;
end loop;
EXCEPTION
WHEN errorsysdate THEN
raise_application_error(-20101, 'error counting from sysdate, event to ddog:' || event_rmsg);
WHEN OTHERS THEN
err_num := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 100);
raise_application_error(err_num, err_msg);
END;
/
show err

exit
2 changes: 1 addition & 1 deletion samples/rowcount.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ declare
begin
name := 'sample.rowcount';
for t in c_table loop
tag := 'table_name:' || t.table_name || ',schema:' || t.owner;
tag := 'table_name:' || t.table_name || ',schema:' || t.owner ;
dbms_output.put_line(name || ':' || t.num_rows || ',' || tag);
result := f_gaugetoddog(name,t.num_rows,tag);
IF result != 'done' THEN
Expand Down
62 changes: 57 additions & 5 deletions src/toddog.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ char *metrictoddog(
{
char *tmp;
short len;
char* metricname;
char* themetric; // to store the metric we will send to datadog
char* thekind; // to store the kind we will send to datadog

Expand All @@ -35,7 +34,6 @@ char *metrictoddog(
}

// the main part
strcat(metricname, name);

if (kind_i == OCI_IND_NOTNULL)
{
Expand All @@ -46,9 +44,9 @@ char *metrictoddog(

if (tag_i == OCI_IND_NOTNULL)
{
asprintf(&themetric,"%s:%d|%s|#%s",metricname, metric, thekind,tag);
asprintf(&themetric,"%s:%d|%s|#%s",name, metric, thekind,tag);
} else {
asprintf(&themetric,"%s:%d|%s|",metricname, metric, thekind);
asprintf(&themetric,"%s:%d|%s|",name, metric, thekind);
}

udp(8125, themetric); // we fire the metric to datadog
Expand All @@ -70,4 +68,58 @@ char *metrictoddog(

// Return pointer, which PL/SQL frees later.
return tmp;
}
}


char *eventtoddog(
OCIExtProcContext *ctx,
char *title, // pointer to name -- name of the metric
short title_i, // null status of name
char *text, // pointer to metric -- number to be sent to the metric
short text_i, // null status of metric
char *tag, // pointer to string1
short tag_i, // null status of string1
short *ret_i, // pointer to indicator null or not null
short *ret_l // len of return
)
{
char *tmp;
short len;
char* theevent;

// Check for null inputs
if ( (title_i == OCI_IND_NULL) || (text_i == OCI_IND_NULL) )
{
*ret_i = (short)OCI_IND_NULL;

// PL/SQL has no notion of a NULL ptr, so return a zero-byte string.
tmp = OCIExtProcAllocCallMemory(ctx, 1);
tmp[0] = '\0';

return(tmp);
}

// the main part

asprintf(&theevent,"_e{%d,%d}:%s|%s|#%s",strlen(title),strlen(text),title,text,tag);

udp(8125, theevent);

// the reply part
char *reply = "done"; // as udp is stateless, we assume all went fine

// Allocate memory for result string, including NULL terminator.
len = strlen(reply);
tmp = OCIExtProcAllocCallMemory(ctx, len + 1);
tmp[0] = '\0';

// set the reply
strcat(tmp, reply);

// Set ret indicator and length.
*ret_i = (short)OCI_IND_NOTNULL;
*ret_l = len;

// Return pointer, which PL/SQL frees later.
return tmp;
}