11import * as http from "http" ;
2- import { sanitizeDsn } from "../node/helpers" ;
32import { EventResponse , Incident } from "../transport/events" ;
4- import { TraceoIncomingMessage , RequestOptions } from "../transport/http" ;
3+ import { TraceoIncomingMessage , RequestOptions , HTTP_ENDPOINT , RequestStatus } from "../transport/http" ;
4+ import { TraceoLog } from "../transport/logger" ;
5+ import { RequestType } from "../transport/types" ;
56import { getGlobalClientData } from "./global" ;
6- import { isClientConnected } from "./is" ;
77import { TRACEO_SDK_VERSION } from "./version" ;
88
9- enum RequestStatus {
10- SUCCESS = "success" ,
11- ERROR = "error" ,
12- }
13-
14- const createHttpOptions = ( path : string ) : http . RequestOptions => {
9+ const createHttpOptions = (
10+ path : string ,
11+ method : RequestType = "POST"
12+ ) : http . RequestOptions => {
1513 const client = getGlobalClientData ( ) ;
1614
17- const { dsn } = client ;
18- const { host, secretKey } = sanitizeDsn ( dsn ) ;
15+ const { host, port } = client ;
1916
2017 const baseOptions : RequestOptions = {
2118 hostname : host ,
22- method : "POST" ,
19+ port,
20+ method,
2321 headers : {
2422 "Content-Type" : "application/json" ,
25- "Traceo-Secret-Key" : secretKey ,
2623 } ,
2724 } ;
2825
@@ -32,84 +29,44 @@ const createHttpOptions = (path: string): http.RequestOptions => {
3229 } ;
3330} ;
3431
35- const statusFromCode = ( code : number ) =>
36- code >= 200 && code <= 299 ? RequestStatus . SUCCESS : RequestStatus . ERROR ;
37-
38- export const sendRuntimeMetrics = async ( data : { } ) => {
39- const { appId, environment } = getGlobalClientData ( ) ;
40-
41- if ( ! appId ) {
42- return ;
43- }
32+ const sendLog = async ( log : TraceoLog ) => {
33+ const httpOptions = createHttpOptions ( HTTP_ENDPOINT . LOG ) ;
34+ await sendEvent ( log , httpOptions ) ;
35+ } ;
4436
45- const httpOptions = createHttpOptions (
46- `/${ appId } /${ environment } /metrics/runtime`
47- ) ;
37+ const sendRuntimeMetrics = async ( data : { } ) => {
38+ const httpOptions = createHttpOptions ( HTTP_ENDPOINT . RUNTIME ) ;
4839 await sendEvent ( data , httpOptions ) ;
4940} ;
5041
51- export const sendIncidentEvent = async ( incident : Incident ) => {
52- const { environment, appId } = getGlobalClientData ( ) ;
53-
54- if ( ! environment || ! appId ) {
55- return ;
56- }
57-
42+ const sendIncident = async ( incident : Incident ) => {
5843 const version = TRACEO_SDK_VERSION ;
59- const baseData = { version, env : environment } ;
44+ const baseData = { version } ;
6045
6146 const payload = Object . assign ( incident , baseData ) ;
6247
63- const httpOptions = createHttpOptions ( `/ ${ appId } ` ) ;
48+ const httpOptions = createHttpOptions ( HTTP_ENDPOINT . INCIDENT ) ;
6449 await sendEvent ( payload , httpOptions ) ;
6550} ;
6651
67- export const sendEvent = async (
52+ const sendEvent = async (
6853 payload : any ,
6954 httpOptions : http . RequestOptions
7055) : Promise < EventResponse | void > => {
71- const { environment , dsn } = getGlobalClientData ( ) ;
56+ const { host , appId } = getGlobalClientData ( ) ;
7257
73- if ( ! environment || ! dsn ) {
58+ if ( ! host || ! appId ) {
7459 return ;
7560 }
7661
77- return new Promise < EventResponse > ( ( resolve , reject ) => {
78- // const httpOptions = createHttpOptions({ payload });
79- if ( ! httpOptions ) {
80- reject ( {
81- statusCode : 400 ,
82- statusMessage :
83- "[Traceo] Error during sending event to Traceo. No HTTP options." ,
84- } ) ;
62+ return new Promise < EventResponse > ( ( _ , reject ) => {
63+ const options = {
64+ ...httpOptions ,
65+ path : httpOptions . path + `/${ appId } `
8566 }
8667
87- if ( ! isClientConnected ( ) ) {
88- reject ( {
89- statusCode : 400 ,
90- statusMessage :
91- "[Traceo] Error during sending event to Traceo. No client global data in NodeJS scope." ,
92- } ) ;
93- }
94-
95- const request = http . request ( httpOptions , ( res : TraceoIncomingMessage ) => {
68+ const request = http . request ( options , ( res : TraceoIncomingMessage ) => {
9669 res . setEncoding ( "utf8" ) ;
97-
98- const status = statusFromCode ( res ?. statusCode as number ) ;
99- const isSuccess = status === RequestStatus . SUCCESS ;
100-
101- if ( ! isSuccess ) {
102- reject ( {
103- statusCode : res ?. statusCode as number ,
104- statusMessage : "[Traceo] Error during sending event to Traceo." ,
105- } ) ;
106- } else {
107- resolve ( {
108- statusCode : res ?. statusCode as number ,
109- statusMessage : "[Traceo] Event successfully sended to Traceo." ,
110- } ) ;
111- }
112-
11370 res . on ( "error" , reject ) ;
11471 } ) ;
11572
@@ -128,3 +85,7 @@ export const sendEvent = async (
12885 request . end ( ) ;
12986 } ) ;
13087} ;
88+
89+ export const httpService = {
90+ sendLog, sendIncident, sendRuntimeMetrics
91+ }
0 commit comments