Skip to content

Commit 3f54db6

Browse files
committed
add events
1 parent 23d8a96 commit 3f54db6

File tree

4 files changed

+139
-10
lines changed

4 files changed

+139
-10
lines changed

create_function_toddog.sql

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ show err
55

66
prompt create function f_metrictoddog
77
CREATE OR REPLACE FUNCTION f_metrictoddog (
8-
name IN VARCHAR2,
8+
name IN VARCHAR2,
99
metric IN BINARY_INTEGER,
10-
kind IN VARCHAR2,
11-
tag IN VARCHAR2)
10+
kind IN VARCHAR2,
11+
tag IN VARCHAR2)
1212
RETURN VARCHAR2 AS LANGUAGE C
1313
NAME "metrictoddog"
1414
LIBRARY libtoddog
@@ -29,6 +29,29 @@ PARAMETERS (
2929
/
3030
show err
3131

32+
prompt create function f_eventtoddog
33+
CREATE OR REPLACE FUNCTION f_eventtoddog (
34+
title IN VARCHAR2,
35+
text IN VARCHAR2,
36+
tag IN VARCHAR2)
37+
RETURN VARCHAR2 AS LANGUAGE C
38+
NAME "eventtoddog"
39+
LIBRARY libtoddog
40+
WITH CONTEXT
41+
PARAMETERS (
42+
CONTEXT,
43+
title STRING,
44+
title INDICATOR short,
45+
text STRING,
46+
text INDICATOR short,
47+
tag STRING,
48+
tag INDICATOR short,
49+
RETURN INDICATOR short,
50+
RETURN LENGTH short,
51+
RETURN STRING);
52+
/
53+
show err
54+
3255
CREATE OR REPLACE FUNCTION f_gaugetoddog(
3356
name IN VARCHAR2,
3457
metric IN number,
@@ -89,6 +112,28 @@ begin
89112
tag := 'source:plsql';
90113
result := f_counttoddog(name,metric,tag);
91114
dbms_output.put_line(result);
115+
EXCEPTION
116+
WHEN OTHERS THEN
117+
err_num := SQLCODE;
118+
err_msg := SUBSTR(SQLERRM, 1, 100);
119+
raise_application_error(err_num, err_msg);
120+
END;
121+
/
122+
show err
123+
124+
declare
125+
title varchar2(50);
126+
text varchar2(50);
127+
tag varchar2(50);
128+
result varchar2(10);
129+
err_num NUMBER;
130+
err_msg VARCHAR2(100);
131+
begin
132+
title := 'title of event';
133+
text := 'text for event';
134+
tag := 'source:plsql';
135+
result := f_eventtoddog(title,text,tag);
136+
dbms_output.put_line(result);
92137
EXCEPTION
93138
WHEN OTHERS THEN
94139
err_num := SQLCODE;
@@ -98,4 +143,4 @@ END;
98143
/
99144
show err
100145

101-
exit
146+
exit

samples/errtoevent.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
set serveroutput on
2+
3+
declare
4+
name varchar2(50);
5+
tag varchar2(50);
6+
result varchar2(10);
7+
cursor c_table is
8+
select count(sysdate) as c from dual;
9+
errorsysdate EXCEPTION;
10+
err_num NUMBER;
11+
err_msg VARCHAR2(100);
12+
event_rmsg VARCHAR2(100);
13+
begin
14+
for t in c_table loop
15+
result := t.c;
16+
if result<2 then
17+
event_rmsg := f_eventtoddog('sample event','sample: error counting sysdate, expected 2 or more','sample');
18+
RAISE errorsysdate;
19+
end if;
20+
end loop;
21+
EXCEPTION
22+
WHEN errorsysdate THEN
23+
raise_application_error(-20101, 'error counting from sysdate, event to ddog:' || event_rmsg);
24+
WHEN OTHERS THEN
25+
err_num := SQLCODE;
26+
err_msg := SUBSTR(SQLERRM, 1, 100);
27+
raise_application_error(err_num, err_msg);
28+
END;
29+
/
30+
show err
31+
32+
exit

samples/rowcount.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ declare
1313
begin
1414
name := 'sample.rowcount';
1515
for t in c_table loop
16-
tag := 'table_name:' || t.table_name || ',schema:' || t.owner;
16+
tag := 'table_name:' || t.table_name || ',schema:' || t.owner ;
1717
dbms_output.put_line(name || ':' || t.num_rows || ',' || tag);
1818
result := f_gaugetoddog(name,t.num_rows,tag);
1919
IF result != 'done' THEN

src/toddog.c

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ char *metrictoddog(
1818
{
1919
char *tmp;
2020
short len;
21-
char* metricname;
2221
char* themetric; // to store the metric we will send to datadog
2322
char* thekind; // to store the kind we will send to datadog
2423

@@ -35,7 +34,6 @@ char *metrictoddog(
3534
}
3635

3736
// the main part
38-
strcat(metricname, name);
3937

4038
if (kind_i == OCI_IND_NOTNULL)
4139
{
@@ -46,9 +44,9 @@ char *metrictoddog(
4644

4745
if (tag_i == OCI_IND_NOTNULL)
4846
{
49-
asprintf(&themetric,"%s:%d|%s|#%s",metricname, metric, thekind,tag);
47+
asprintf(&themetric,"%s:%d|%s|#%s",name, metric, thekind,tag);
5048
} else {
51-
asprintf(&themetric,"%s:%d|%s|",metricname, metric, thekind);
49+
asprintf(&themetric,"%s:%d|%s|",name, metric, thekind);
5250
}
5351

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

7169
// Return pointer, which PL/SQL frees later.
7270
return tmp;
73-
}
71+
}
72+
73+
74+
char *eventtoddog(
75+
OCIExtProcContext *ctx,
76+
char *title, // pointer to name -- name of the metric
77+
short title_i, // null status of name
78+
char *text, // pointer to metric -- number to be sent to the metric
79+
short text_i, // null status of metric
80+
char *tag, // pointer to string1
81+
short tag_i, // null status of string1
82+
short *ret_i, // pointer to indicator null or not null
83+
short *ret_l // len of return
84+
)
85+
{
86+
char *tmp;
87+
short len;
88+
char* theevent;
89+
90+
// Check for null inputs
91+
if ( (title_i == OCI_IND_NULL) || (text_i == OCI_IND_NULL) )
92+
{
93+
*ret_i = (short)OCI_IND_NULL;
94+
95+
// PL/SQL has no notion of a NULL ptr, so return a zero-byte string.
96+
tmp = OCIExtProcAllocCallMemory(ctx, 1);
97+
tmp[0] = '\0';
98+
99+
return(tmp);
100+
}
101+
102+
// the main part
103+
104+
asprintf(&theevent,"_e{%d,%d}:%s|%s|#%s",strlen(title),strlen(text),title,text,tag);
105+
106+
udp(8125, theevent);
107+
108+
// the reply part
109+
char *reply = "done"; // as udp is stateless, we assume all went fine
110+
111+
// Allocate memory for result string, including NULL terminator.
112+
len = strlen(reply);
113+
tmp = OCIExtProcAllocCallMemory(ctx, len + 1);
114+
tmp[0] = '\0';
115+
116+
// set the reply
117+
strcat(tmp, reply);
118+
119+
// Set ret indicator and length.
120+
*ret_i = (short)OCI_IND_NOTNULL;
121+
*ret_l = len;
122+
123+
// Return pointer, which PL/SQL frees later.
124+
return tmp;
125+
}

0 commit comments

Comments
 (0)