Skip to content

Commit

Permalink
🔧 add usage of ZnEventToLogRecordAdapter to StargateApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
mreyes authored and jvanecek committed Jun 27, 2024
1 parent 03d33fd commit 449ed5d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 105 deletions.
131 changes: 61 additions & 70 deletions source/Stargate-API-Skeleton-Tests/StargateApplicationTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Class {
#instVars : [
'port',
'baseUrl',
'logServerEvents',
'logClientEvents'
'logIncomingHTTPRequests',
'logOutgoingHTTPRequests'
],
#category : 'Stargate-API-Skeleton-Tests',
#package : 'Stargate-API-Skeleton-Tests'
Expand Down Expand Up @@ -90,8 +90,8 @@ StargateApplicationTest >> setUp [
port := self freeListeningTCPPort.
StargateApplication logsDirectory ensureCreateDirectory.

logServerEvents := false.
logClientEvents := false.
logIncomingHTTPRequests := false.
logOutgoingHTTPRequests := false.
]

{ #category : 'private' }
Expand All @@ -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
]

Expand Down Expand Up @@ -278,25 +281,21 @@ StargateApplicationTest >> testLogClientEventsDuringSucessfulGet [
| logRecord |

logger runDuring: [
logClientEvents := true.
logOutgoingHTTPRequests := true.
self testGetPets].

logRecord := NeoJSONObject fromString:
( String streamContents: [:stream | logger recordings last printOneLineJsonOn: stream ] ).

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

]

Expand All @@ -306,82 +305,74 @@ StargateApplicationTest >> testLogClientEventsDuringUnsucessfulPost [
| logRecord |

logger runDuring: [
logClientEvents := true.
logOutgoingHTTPRequests := true.
self testUnsupportedMediaType].

logRecord := NeoJSONObject fromString:
( String streamContents: [:stream | logger recordings last printOneLineJsonOn: stream ] ).

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

]

{ #category : #'tests - logs' }
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' }
Expand Down Expand Up @@ -440,29 +431,29 @@ StargateApplicationTest >> testPrintHelpOn [
self assert: help isLineEndingInsensitiveEqualsTo: ('NAME
pet-store [<1s>] - A RESTful API for Pet stores
SYNOPSYS
pet-store --pet-store.stargate.public-url=%<publicURL%> --pet-store.stargate.port=%<port%> --pet-store.stargate.operations-secret=%<operationsSecret%> [--pet-store.stargate.log-server-events=%<logServerEvents%>] [--pet-store.stargate.log-client-events=%<logClientEvents%>]
pet-store --pet-store.stargate.public-url=%<publicURL%> --pet-store.stargate.port=%<port%> --pet-store.stargate.operations-secret=%<operationsSecret%> [--pet-store.stargate.log-incoming-http-requests=%<logIncomingHTTPRequests%>] [--pet-store.stargate.log-outgoing-http-requests=%<logOutgoingHTTPRequests%>]
PARAMETERS
--pet-store.stargate.public-url=%<publicURL%>
Public URL where the API is deployed. Used to create hypermedia links.
--pet-store.stargate.port=%<port%>
Listening port.
--pet-store.stargate.operations-secret=%<operationsSecret%>
Secret key for checking JWT signatures.
--pet-store.stargate.log-server-events=%<logServerEvents%>
Boolean that indicates whether to log all the HTTP Server requests. Defaults to false.
--pet-store.stargate.log-client-events=%<logClientEvents%>
Boolean that indicates whether to log all the HTTP Clients requests. Defaults to false.
--pet-store.stargate.log-incoming-http-requests=%<logIncomingHTTPRequests%>
Boolean that indicates whether to log all the Incoming HTTP Requests. Defaults to false.
--pet-store.stargate.log-outgoing-http-requests=%<logOutgoingHTTPRequests%>
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.
PET_STORE__STARGATE__PORT
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)
]

Expand Down
80 changes: 45 additions & 35 deletions source/Stargate-API-Skeleton/StargateApplication.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ Class {
#name : 'StargateApplication',
#superclass : 'LaunchpadApplication',
#instVars : [
'apiOptional'
'apiOptional',
'znEventToLogRecordAdapter'
],
#classInstVars : [
'Version'
Expand Down Expand Up @@ -88,37 +89,43 @@ 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:
'Set the maximum number of concurrent connections that I will accept. When this threshold is reached, a 503 Service Unavailable response will be sent and the connection will be closed'
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
]

Expand Down Expand Up @@ -188,9 +195,7 @@ StargateApplication >> basicStartWithin: context [

| api |
self logAPIVersion.
self
configureHTTPClientLogging;
configureHTTPServerLogging.
self configureHTTPRequestsLogging.
api := self createAPI.
self
configureGlobalErrorHandlerIn: api;
Expand All @@ -203,6 +208,7 @@ StargateApplication >> basicStop [

apiOptional withContentDo: [ :api | api stop ].
ZnLogEvent announcer unsubscribe: self.
znEventToLogRecordAdapter stopListeners.
super basicStop
]

Expand Down Expand Up @@ -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 |
Expand All @@ -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' }
Expand All @@ -281,7 +290,8 @@ StargateApplication >> createAPI [
StargateApplication >> initialize [

super initialize.
apiOptional := Optional unused
apiOptional := Optional unused.
znEventToLogRecordAdapter := ZnEventToLogRecordAdapter new
]

{ #category : 'private - activation/deactivation' }
Expand Down

0 comments on commit 449ed5d

Please sign in to comment.