From 449ed5dd114ad93e5242874ea106f129ce4696e9 Mon Sep 17 00:00:00 2001 From: mreyes Date: Thu, 27 Jun 2024 13:30:46 -0300 Subject: [PATCH] :wrench: add usage of ZnEventToLogRecordAdapter to StargateApplication --- .../StargateApplicationTest.class.st | 131 ++++++++---------- .../StargateApplication.class.st | 80 ++++++----- 2 files changed, 106 insertions(+), 105 deletions(-) diff --git a/source/Stargate-API-Skeleton-Tests/StargateApplicationTest.class.st b/source/Stargate-API-Skeleton-Tests/StargateApplicationTest.class.st index 8d8dd30..6e7821c 100644 --- a/source/Stargate-API-Skeleton-Tests/StargateApplicationTest.class.st +++ b/source/Stargate-API-Skeleton-Tests/StargateApplicationTest.class.st @@ -7,8 +7,8 @@ Class { #instVars : [ 'port', 'baseUrl', - 'logServerEvents', - 'logClientEvents' + 'logIncomingHTTPRequests', + 'logOutgoingHTTPRequests' ], #category : 'Stargate-API-Skeleton-Tests', #package : 'Stargate-API-Skeleton-Tests' @@ -90,8 +90,8 @@ StargateApplicationTest >> setUp [ port := self freeListeningTCPPort. StargateApplication logsDirectory ensureCreateDirectory. - logServerEvents := false. - logClientEvents := false. + logIncomingHTTPRequests := false. + logOutgoingHTTPRequests := false. ] { #category : 'private' } @@ -108,13 +108,16 @@ StargateApplicationTest >> startConcurrentConnectionsApp [ { #category : 'private' } StargateApplicationTest >> startPetStore [ - self start: PetStoreApplication withAll: { - '--pet-store.stargate.public-url=http://localhost:<1p>' expandMacrosWith: port. - '--pet-store.stargate.port=<1p>' expandMacrosWith: port. - '--pet-store.stargate.operations-secret=<1s>' expandMacrosWith: self secret. - '--pet-store.stargate.log-server-events=<1p>' expandMacrosWith: logServerEvents . - '--pet-store.stargate.log-client-events=<1p>' expandMacrosWith: logClientEvents - }. + self start: PetStoreApplication withAll: { + ('--pet-store.stargate.public-url=http://localhost:<1p>' + expandMacrosWith: port). + ('--pet-store.stargate.port=<1p>' expandMacrosWith: port). + ('--pet-store.stargate.operations-secret=<1s>' expandMacrosWith: + self secret). + ('--pet-store.stargate.log-incoming-http-requests=<1p>' + expandMacrosWith: logIncomingHTTPRequests). + ('--pet-store.stargate.log-outgoing-http-requests=<1p>' + expandMacrosWith: logOutgoingHTTPRequests) }. baseUrl := application configuration petStore stargate publicURL ] @@ -278,7 +281,7 @@ StargateApplicationTest >> testLogClientEventsDuringSucessfulGet [ | logRecord | logger runDuring: [ - logClientEvents := true. + logOutgoingHTTPRequests := true. self testGetPets]. logRecord := NeoJSONObject fromString: @@ -286,17 +289,13 @@ StargateApplicationTest >> testLogClientEventsDuringSucessfulGet [ self assert: logRecord level equals: 'DEBUG'; - assert: logRecord message equals: 'HTTP Client event received'; + assert: logRecord message equals: 'Outgoing HTTP request responded'; assert: #( 'Morphic UI Process' 'CommandLine handler process' ) includes: logRecord process; assert: ( logRecord summary includesSubstring: 'GET /pets 200 59B' ); assert: logRecord request method equals: 'GET'; assert: logRecord request uri equals: ( 'http://localhost:<1p>/pets' expandMacrosWith: port ); - assert: logRecord request headers size equals: 4; assert: logRecord response code equals: 200; - assert: logRecord response totalSize equals: 59; - assert: logRecord response content - equals: ( '{"items":[],"links":{"self":"http://localhost:<1p>/pets"}}' expandMacrosWith: port ); - assert: logRecord response headers size equals: 8 + assert: logRecord response totalSize equals: 59 ] @@ -306,7 +305,7 @@ StargateApplicationTest >> testLogClientEventsDuringUnsucessfulPost [ | logRecord | logger runDuring: [ - logClientEvents := true. + logOutgoingHTTPRequests := true. self testUnsupportedMediaType]. logRecord := NeoJSONObject fromString: @@ -314,17 +313,13 @@ StargateApplicationTest >> testLogClientEventsDuringUnsucessfulPost [ self assert: logRecord level equals: 'DEBUG'; - assert: logRecord message equals: 'HTTP Client event received'; + assert: logRecord message equals: 'Outgoing HTTP request responded'; assert: #( 'Morphic UI Process' 'CommandLine handler process' ) includes: logRecord process; assert: (logRecord summary includesSubstring: 'POST /pets 415 72B'); assert: logRecord request method equals: 'POST'; assert: logRecord request uri equals: ( 'http://localhost:<1p>/pets' expandMacrosWith: port ); - assert: logRecord request headers size equals: 6; assert: logRecord response code equals: 415; - assert: logRecord response totalSize equals: 72; - assert: (NeoJSONObject fromString: logRecord response content) - equals: (NeoJSONObject fromString:'{ "message" : "Decoder not found for given media type", "code" : 415 }'); - assert: logRecord response headers size equals: 6 + assert: logRecord response totalSize equals: 72 ] @@ -332,56 +327,52 @@ StargateApplicationTest >> testLogClientEventsDuringUnsucessfulPost [ StargateApplicationTest >> testLogServerEventsDuringSucessfulGet [ | logRecord | - - logger runDuring: [ - logServerEvents := true. + logger runDuring: [ + logIncomingHTTPRequests := true. self testGetPets ]. - - logRecord := NeoJSONObject fromString: - ( String streamContents: [:stream | logger recordings last printOneLineJsonOn: stream ] ). - + + logRecord := NeoJSONObject fromString: + (String streamContents: [ :stream | + (logger recordings at: logger recordings size - 1) + printOneLineJsonOn: stream ]). + self - assert: logRecord level equals: 'DEBUG'; - assert: logRecord message equals: 'HTTP Server event received'; - assert: logRecord process equals: 'ZnManagingMultiThreadedServer HTTP worker'; - assert: (logRecord summary includesSubstring: 'GET /pets 200 59B'); + assert: logRecord level equals: 'DEBUG'; + assert: logRecord message equals: 'Incoming HTTP request responded'; + assert: logRecord process + equals: 'ZnManagingMultiThreadedServer HTTP worker'; + assert: (logRecord summary includesSubstring: 'GET /pets'); assert: logRecord request method equals: 'GET'; - assert: logRecord request uri equals: ( 'http://localhost:<1p>/pets' expandMacrosWith: port ); - assert: logRecord request headers size equals: 6; + assert: logRecord request uri + equals: ('http://localhost:<1p>/pets' expandMacrosWith: port); assert: logRecord response code equals: 200; - assert: logRecord response totalSize equals: 59; - assert: logRecord response content - equals: ( '{"items":[],"links":{"self":"http://localhost:<1p>/pets"}}' expandMacrosWith: port ); - assert: logRecord response headers size equals: 8 - + assert: logRecord response totalSize equals: 59 ] { #category : #'tests - logs' } StargateApplicationTest >> testLogServerEventsDuringUnsucessfulPost [ | logRecord | - - logger runDuring: [ - logServerEvents := true. + logger runDuring: [ + logIncomingHTTPRequests := true. self testUnsupportedMediaType ]. - - logRecord := NeoJSONObject fromString: - ( String streamContents: [:stream | logger recordings last printOneLineJsonOn: stream ] ). - + + logRecord := NeoJSONObject fromString: + (String streamContents: [ :stream | + (logger recordings at: logger recordings size - 1) + printOneLineJsonOn: stream ]). + self - assert: logRecord level equals: 'DEBUG'; - assert: logRecord message equals: 'HTTP Server event received'; - assert: logRecord process equals: 'ZnManagingMultiThreadedServer HTTP worker'; - assert: (logRecord summary includesSubstring: 'POST /pets 415 72B'); + assert: logRecord level equals: 'DEBUG'; + assert: logRecord message equals: 'Incoming HTTP request responded'; + assert: logRecord process + equals: 'ZnManagingMultiThreadedServer HTTP worker'; + assert: (logRecord summary includesSubstring: 'POST /pets'); assert: logRecord request method equals: 'POST'; - assert: logRecord request uri equals: ( 'http://localhost:<1p>/pets' expandMacrosWith: port ); - assert: logRecord request headers size equals: 8; + assert: logRecord request uri + equals: ('http://localhost:<1p>/pets' expandMacrosWith: port); assert: logRecord response code equals: 415; - assert: logRecord response totalSize equals: 72; - assert: (NeoJSONObject fromString: logRecord response content) - equals: (NeoJSONObject fromString:'{ "message" : "Decoder not found for given media type", "code" : 415 }'); - assert: logRecord response headers size equals: 6 - + assert: logRecord response totalSize equals: 72 ] { #category : #'tests - api' } @@ -440,7 +431,7 @@ StargateApplicationTest >> testPrintHelpOn [ self assert: help isLineEndingInsensitiveEqualsTo: ('NAME pet-store [<1s>] - A RESTful API for Pet stores SYNOPSYS - pet-store --pet-store.stargate.public-url=% --pet-store.stargate.port=% --pet-store.stargate.operations-secret=% [--pet-store.stargate.log-server-events=%] [--pet-store.stargate.log-client-events=%] + pet-store --pet-store.stargate.public-url=% --pet-store.stargate.port=% --pet-store.stargate.operations-secret=% [--pet-store.stargate.log-incoming-http-requests=%] [--pet-store.stargate.log-outgoing-http-requests=%] PARAMETERS --pet-store.stargate.public-url=% Public URL where the API is deployed. Used to create hypermedia links. @@ -448,10 +439,10 @@ PARAMETERS Listening port. --pet-store.stargate.operations-secret=% Secret key for checking JWT signatures. - --pet-store.stargate.log-server-events=% - Boolean that indicates whether to log all the HTTP Server requests. Defaults to false. - --pet-store.stargate.log-client-events=% - Boolean that indicates whether to log all the HTTP Clients requests. Defaults to false. + --pet-store.stargate.log-incoming-http-requests=% + Boolean that indicates whether to log all the Incoming HTTP Requests. Defaults to false. + --pet-store.stargate.log-outgoing-http-requests=% + Boolean that indicates whether to log all the Outgoing HTTP Requests. Defaults to false. ENVIRONMENT PET_STORE__STARGATE__PUBLIC_URL Public URL where the API is deployed. Used to create hypermedia links. @@ -459,10 +450,10 @@ ENVIRONMENT Listening port. PET_STORE__STARGATE__OPERATIONS_SECRET Secret key for checking JWT signatures. - PET_STORE__STARGATE__LOG_SERVER_EVENTS - Boolean that indicates whether to log all the HTTP Server requests. Defaults to false. - PET_STORE__STARGATE__LOG_CLIENT_EVENTS - Boolean that indicates whether to log all the HTTP Clients requests. Defaults to false. + PET_STORE__STARGATE__LOG_INCOMING_HTTP_REQUESTS + Boolean that indicates whether to log all the Incoming HTTP Requests. Defaults to false. + PET_STORE__STARGATE__LOG_OUTGOING_HTTP_REQUESTS + Boolean that indicates whether to log all the Outgoing HTTP Requests. Defaults to false. ' expandMacrosWith: PetStoreApplication version) ] diff --git a/source/Stargate-API-Skeleton/StargateApplication.class.st b/source/Stargate-API-Skeleton/StargateApplication.class.st index bc92522..062e7e3 100644 --- a/source/Stargate-API-Skeleton/StargateApplication.class.st +++ b/source/Stargate-API-Skeleton/StargateApplication.class.st @@ -2,7 +2,8 @@ Class { #name : 'StargateApplication', #superclass : 'LaunchpadApplication', #instVars : [ - 'apiOptional' + 'apiOptional', + 'znEventToLogRecordAdapter' ], #classInstVars : [ 'Version' @@ -88,18 +89,36 @@ StargateApplication class >> stackTraceDumpExtension [ StargateApplication class >> stargateConfigurationParameters [ ^ OrderedCollection new - add: ( MandatoryConfigurationParameter named: 'Public URL' - describedBy: 'Public URL where the API is deployed. Used to create hypermedia links' - inside: self sectionsForStargateConfiguration - convertingWith: #asUrl ); - add: ( MandatoryConfigurationParameter named: 'Port' - describedBy: 'Listening port' - inside: self sectionsForStargateConfiguration - convertingWith: #asNumber ); - add: ( MandatoryConfigurationParameter named: 'Operations Secret' - describedBy: 'Secret key for checking JWT signatures' - inside: self sectionsForStargateConfiguration - convertingWith: #asByteArray ) asSensitive + add: (MandatoryConfigurationParameter + named: 'Public URL' + describedBy: + 'Public URL where the API is deployed. Used to create hypermedia links' + inside: self sectionsForStargateConfiguration + convertingWith: #asUrl); + add: (MandatoryConfigurationParameter + named: 'Port' + describedBy: 'Listening port' + inside: self sectionsForStargateConfiguration + convertingWith: #asNumber); + add: (MandatoryConfigurationParameter + named: 'Operations Secret' + describedBy: 'Secret key for checking JWT signatures' + inside: self sectionsForStargateConfiguration + convertingWith: #asByteArray) asSensitive; + add: (OptionalConfigurationParameter + named: 'Log Incoming HTTP Requests' + describedBy: + 'Boolean that indicates whether to log all the Incoming HTTP Requests' + inside: self sectionsForStargateConfiguration + defaultingTo: false + convertingWith: [ :value | value = 'true' ]); + add: (OptionalConfigurationParameter + named: 'Log Outgoing HTTP Requests' + describedBy: + 'Boolean that indicates whether to log all the Outgoing HTTP Requests' + inside: self sectionsForStargateConfiguration + defaultingTo: false + convertingWith: [ :value | value = 'true' ]); add: ( OptionalConfigurationParameter named: 'Concurrent Connections Threshold' describedBy: @@ -107,18 +126,6 @@ StargateApplication class >> stargateConfigurationParameters [ inside: self sectionsForStargateConfiguration defaultingTo: 32 convertingWith: #asNumber ); - add: ( OptionalConfigurationParameter - named: 'Log server events' - describedBy: 'Boolean that indicates whether to log all the HTTP Server requests' - inside: self sectionsForStargateConfiguration - defaultingTo: false - convertingWith: [ :value | value = 'true' ] ); - add: ( OptionalConfigurationParameter - named: 'Log client events' - describedBy: 'Boolean that indicates whether to log all the HTTP Clients requests' - inside: self sectionsForStargateConfiguration - defaultingTo: false - convertingWith: [ :value | value = 'true' ] ); yourself ] @@ -188,9 +195,7 @@ StargateApplication >> basicStartWithin: context [ | api | self logAPIVersion. - self - configureHTTPClientLogging; - configureHTTPServerLogging. + self configureHTTPRequestsLogging. api := self createAPI. self configureGlobalErrorHandlerIn: api; @@ -203,6 +208,7 @@ StargateApplication >> basicStop [ apiOptional withContentDo: [ :api | api stop ]. ZnLogEvent announcer unsubscribe: self. + znEventToLogRecordAdapter stopListeners. super basicStop ] @@ -235,19 +241,19 @@ StargateApplication >> configureGlobalErrorHandlerIn: api [ ] { #category : #'private - activation/deactivation' } -StargateApplication >> configureHTTPClientLogging [ +StargateApplication >> configureHTTPRequestsLogging [ - self stargateConfiguration enableHTTPClientLogging then: [ + self stargateConfiguration logClientEvents then: [ ZnLogEvent announcer when: ZnClientTransactionEvent do: [ :event | LaunchpadLogRecord emitStructuredDebuggingInfo: 'HTTP Client event received' with: [ :data | self dumpDataOf: event on: data ] ] ] ] -{ #category : #'private - activation/deactivation' } -StargateApplication >> configureHTTPServerLogging [ + znEventToLogRecordAdapter logIncomingRequests: + self stargateConfiguration logIncomingHTTPRequests. - self stargateConfiguration enableHTTPServerLogging then: [ + self stargateConfiguration logServerEvents then: [ ZnLogEvent announcer when: ZnServerTransactionEvent , ZnSimplifiedServerTransactionEvent do: [ :event | @@ -257,7 +263,10 @@ StargateApplication >> configureHTTPServerLogging [ ZnLogEvent announcer when: ZnServerHandlerErrorEvent - do: [ :event | LaunchpadLogRecord emitError: event exception messageText ] + do: [ :event | + LaunchpadLogRecord emitError: event exception messageText ]. + + znEventToLogRecordAdapter startUpListeners ] { #category : #'private - accessing' } @@ -281,7 +290,8 @@ StargateApplication >> createAPI [ StargateApplication >> initialize [ super initialize. - apiOptional := Optional unused + apiOptional := Optional unused. + znEventToLogRecordAdapter := ZnEventToLogRecordAdapter new ] { #category : 'private - activation/deactivation' }