Home

Object Modeling in Azure Data Explorer

%3CLINGO-SUB%20id%3D%22lingo-sub-740546%22%20slang%3D%22en-US%22%3EObject%20Modeling%20in%20Azure%20Data%20Explorer%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-740546%22%20slang%3D%22en-US%22%3E%3CP%3EOver%20the%20past%20few%20years%2C%20Azure%20Data%20Explorer%20has%20become%20a%20critical%20part%20of%20running%20Live%20Site%20for%20most%20internal%20Azure%20services.%20This%20post%20describes%20a%20simple%20approach%20to%20model%20objects%20in%20your%20services%20using%20event%20streams%20ingested%20into%20Azure%20Data%20Explorer.%20The%20sample%20streams%20described%20below%20are%20based%20on%20real%20streams%20used%20by%20teams%20in%20Azure%20Compute%20(including%20my%20team)%20to%20monitor%20and%20manage%20all%20Virtual%20Machines%20(VMs)%20and%20host%20servers%20in%20Azure.%20We%E2%80%99ve%20built%20a%20lot%20of%20tooling%20in%20Azure%20Compute%20around%20the%20data%20modeling%20and%20query%20patterns%20shared%20below.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20don%E2%80%99t%20already%20have%20a%20pipeline%20setup%20for%20your%20service%20logs%20to%20be%20ingested%20into%20Azure%20Data%20Explorer%2C%20take%20a%20look%20at%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdata-explorer%2Fingest-data-event-hub%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%3C%2FA%3E%3C%2FSPAN%3E%20guide.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1791425926%22%20id%3D%22toc-hId-1791425926%22%3ESnapshot%20Events%3C%2FH2%3E%0A%3CP%3EObject%20properties%20are%20modeled%20as%20columns%20of%20a%20table%20and%20each%20row%20is%20a%20snapshot%20representation%20of%20that%20object.%3C%2FP%3E%0A%3CP%3ELet%E2%80%99s%20look%20at%20a%20sample%20event%20schema%20for%20a%20VM.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3E%5B%3CU%3E%3CA%20href%3D%22https%3A%2F%2Fdataexplorer.azure.com%2Fclusters%2Forbcluster.westus2%2Fdatabases%2Forbtestdb%3Fquery%3DH4sIAAAAAAAAAwvzDc5LLCjOyC%2Fh5apRKEnMTlUw5OXi5QIAfqaqehgAAAA%3D%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EExecute%20Query%3C%2FA%3E%3C%2FU%3E%5D%3C%2FP%3E%0A%3CP%3EVMSnapshot%3C%2FP%3E%0A%3CP%3E%7C%20take%201%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CSTRONG%3EVMId%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CSTRONG%3EHostId%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CSTRONG%3EOsImage%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CSTRONG%3EState%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CSTRONG%3ERegion%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CSTRONG%3EPreciseTimeStamp%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E3f576830-2d8f-481b-8448-bca561090831%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3Ea3d1a948-53ae-4175-8030-d7d174c427d1%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3ERServer-WS2016%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EStarted%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3Ewestus%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E2019-05-21T19%3A25%3A39Z%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOur%20services%20emit%20these%20snapshots%20periodically%20and%20on%20every%20column%20change%20%E2%80%93%20the%20latter%20is%20essential%20to%20look%20at%20when%20things%20changed%20without%20waiting%20on%20the%20snapshot%20period.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20do%20this%20for%20every%20key%20object%20modeled%20in%20the%20system%20%E2%80%93%20service%20endpoints%2C%20configuration%20values%2C%20VMs%2C%20host%20servers%2C%20etc.%20This%20snapshot%20pattern%20works%20well%20for%20long%20lived%20objects%20%E2%80%93%20short%20lived%20objects%20like%20requests%20typically%20have%20their%20properties%20emitted%20as%20part%20of%20the%20initial%20request%20processing.%20You%20can%20also%20consider%20pushing%20immutable%20properties%20of%20an%20object%20as%20a%20separate%20stream%20with%20lower%20snapshot%20frequencies.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--760731035%22%20id%3D%22toc-hId--760731035%22%3EQuery%20Pattern%201%20%E2%80%93%20What%20is%20the%20latest%20state%20of%20objects%3F%3C%2FH2%3E%0A%3CP%3EIn%20the%20example%20above%2C%20if%20you%20wanted%20to%20get%20the%20latest%20state%20of%20a%20single%20object%2C%20this%20is%20quite%20straightforward%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3E%5B%3CU%3E%3CA%20href%3D%22https%3A%2F%2Fdataexplorer.azure.com%2Fclusters%2Forbcluster.westus2%2Fdatabases%2Forbtestdb%3Fquery%3DH4sIAAAAAAAAAwvzDc5LLCjOyC%2Fh5apRKM9ILUpVCPP1TFGwtVVQMk4zNTezMDbQNUqxSNM1sTBM0rUwMbHQTUpONDUzNLA0sDA2VFKoUSjJL1AwVEiqVAgoSk3OLE4NycxNDS5JzC1QSEktTubl4uUCALB4YctnAAAA%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EExecute%20Query%3C%2FA%3E%3C%2FU%3E%5D%3C%2FP%3E%0A%3CP%20dir%3D%22ltr%22%3EVMSnapshot%3C%2FP%3E%0A%3CP%20dir%3D%22ltr%22%3E%7C%20where%20VMId%20%3D%3D%20%223f576830-2d8f-481b-8448-bca561090831%22%20%7C%20top%201%20by%20PreciseTimeStamp%20desc%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3EWhat%20if%20you%20wanted%20the%20latest%20snapshot%20for%20several%20objects%20at%20a%20time%3F%20This%20is%20where%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fkusto%2Fquery%2Farg-max-aggfunction%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Earg_max%3C%2FA%3E%3C%2FSPAN%3E%20is%20helpful.%3C%2FP%3E%0A%3CP%3E%3CEM%3Earg_max%20%3C%2FEM%3Efinds%20rows%20corresponding%20to%20the%20expression%20that%20needs%20to%20be%20maximized.%20If%20there%20are%20%3CEM%3En%20%3C%2FEM%3EVMs%20in%20your%20query%2C%20arg_max%20can%20be%20used%20to%20maximize%20PreciseTimeStamp%20for%20every%20unique%20VMId%20%E2%80%93%20the%20end%20result%20will%20be%20%3CEM%3En%20%3C%2FEM%3Erows%2C%20where%20each%20row%20corresponds%20to%20the%20latest%20snapshot%20for%20that%20VM.%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20the%20query%20below%20finds%20the%20latest%20VM%20state%20distribution%20in%20the%20region%20westus.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3E%5B%3CU%3E%3CA%20href%3D%22https%3A%2F%2Fdataexplorer.azure.com%2Fclusters%2Forbcluster.westus2%2Fdatabases%2Forbtestdb%3Fquery%3DH4sIAAAAAAAAA13LvQ6CMBSG4Z2Ee%2FjCVIyTO%2B4OJEYIqznCSdvhtKQ%2FQYgXL7rp%2FD7v0HaO5mh8KosXFsOBcWNtvUPToFo4phwrkJtwDTzayL0V7hLJjDNIe3Uy9WeNWYSC3RgU9F3oqf79EYcajxVDe5nwu4w%2Bu6S%2BdaeJsfeyeAOSaydHnAAAAA%3D%3D%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EExecute%20Query%3C%2FA%3E%3C%2FU%3E%5D%3C%2FP%3E%0A%3CP%3EVMSnapshot%3C%2FP%3E%0A%3CP%3E%7C%20where%20Region%20%3D%3D%20%22westus%22%20and%20PreciseTimeStamp%20%26gt%3B%20ago(2h)%3C%2FP%3E%0A%3CP%3E%7C%20summarize%20arg_max(PreciseTimeStamp%2C%20*)%20by%20VMId%3C%2FP%3E%0A%3CP%3E%7C%20summarize%20count()%20by%20State%26nbsp%3B%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3EThe%20other%20thing%20to%20note%20is%20that%20the%20query%20looks%20back%202%20hours%20here%20%E2%80%93%202x%20the%20snapshot%20interval%20which%20is%20hourly%20for%20this%20table.%20Since%20the%20full%20snapshot%20event%20is%20emitted%20on%20every%20property%20change%20for%20the%20VM%2C%20this%20query%20does%20give%20you%20the%20latest%20state%20of%20every%20object%20in%20Azure%20Data%20Explorer%2C%20not%20just%20what%20was%20visible%20on%20the%20hourly%20snapshot.%3C%2FP%3E%0A%3CP%3EAlso%2C%20the%20query%20result%20might%20include%20VMs%20that%20have%20already%20been%20deleted.%20To%20deal%20with%20this%2C%20we%20model%20the%20deletion%20itself%20as%20a%20state%20on%20the%20snapshot%2C%20so%20that%20such%20results%20can%20be%20filtered%20if%20required.%3C%2FP%3E%0A%3CP%3ESince%20we%20also%20model%20every%20service%20configuration%20value%20and%20the%20whole%20service%20itself%20as%20an%20object%2C%20we%20use%20the%20pattern%20above%20to%20track%20each%20config%20value%20and%20build%20version%20as%20rollouts%20occur%20in%20the%20fleet.%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20the%20query%20below%20gives%20us%20the%20version%20distribution%20of%20our%20services%20running%20across%20Azure%20host%20servers.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3E%5B%3CU%3E%3CA%20href%3D%22https%3A%2F%2Fdataexplorer.azure.com%2Fclusters%2Forbcluster.westus2%2Fdatabases%2Forbtestdb%3Fquery%3DH4sIAAAAAAAAA1WMOw7CMAxA90q9g8eCegpYwoYIOzKJ1VrCcRU7CBCHp4KJ%2BX2Cmkeqd04UCy42q%2FfdG6yJYOUXAdbpIvgYjpUSG51ZKDrKMsJ2A9cnhHVwyPAf5aSt%2BPBjX%2B1EE2sZYdf4lvcqwh7Q5rXruw%2FWpJfehAAAAA%3D%3D%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EExecute%20Query%3C%2FA%3E%3C%2FU%3E%5D%3C%2FP%3E%0A%3CP%3EHostServiceSnapshot%3C%2FP%3E%0A%3CP%3E%7C%20summarize%20arg_max(PreciseTimeStamp%2C%20*)%20by%20HostId%3C%2FP%3E%0A%3CP%3E%7C%20summarize%20dcount(HostId)%20by%20Region%2C%20BuildCommitHash%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3EThe%20BuildCommitHash%20in%20our%20snapshots%20enables%20us%20to%20track%20every%20code%20change%20committed%20as%20it%E2%80%99s%20deployed%20across%20the%20fleet.%20We%20have%20some%20tooling%20that%20takes%20in%20a%20commit%2C%20gets%20the%20latest%20snapshots%20of%20the%20service%2C%20runs%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgit-scm.com%2Fdocs%2Fgit-merge-base%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Egit%20merge-base%3C%2FA%3E%3C%2FSPAN%3E%20and%20provides%20the%20engineer%20with%20the%20exact%20spread%20of%20host%20servers%20that%20have%20that%20fix.%20We%20also%20use%20similar%20queries%20to%20setup%20reports%20using%20Microsoft%20Flow%20to%20track%20build%20versions%2C%20A%2FB%20testing%20of%20config%20and%20more.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-982079300%22%20id%3D%22toc-hId-982079300%22%3EQuery%20Pattern%202%20%E2%80%93%20When%20did%20a%20Property%20Value%20Change%3F%3C%2FH2%3E%0A%3CP%3ELet%E2%80%99s%20say%20you%20wanted%20to%20know%20when%20the%20version%20of%20a%20host%20service%20changed%20using%20snapshots%20%E2%80%93%20you%20could%20do%20a%20self-join%20with%20the%20same%20table%2C%20though%20this%20makes%20for%20a%20complicated%20query.%20This%20pattern%20is%20solved%20quite%20cleanly%20with%20the%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fkusto%2Fquery%2Fsortoperator%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esort%3C%2FA%3E%3C%2FSPAN%3E%20and%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fkusto%2Fquery%2Fprevfunction%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eprev%3C%2FA%3E%3C%2FSPAN%3E%20operators.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20prev%20operator%20allows%20you%20to%20access%20rows%20before%20the%20current%20row%20being%20processed%20(from%20a%20sorted%20dataset).%20To%20find%20the%20when%20a%20value%20changed%2C%20you%20can%20now%20compare%20row%20%3CEM%3En%20%3C%2FEM%3Eto%20row%20%3CEM%3En-1%3C%2FEM%3E%20quite%20easily.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3E%5B%3CU%3E%3CA%20href%3D%22https%3A%2F%2Fdataexplorer.azure.com%2Fclusters%2Forbcluster.westus2%2Fdatabases%2Forbtestdb%3Fquery%3DH4sIAAAAAAAAA12OvQ6CMBDHdxLe4WSShCYiGHBg0QU3E3yBsz1CDbWkLSoJD2%2BZDE53ud%2F9P2ptXUPmJTk1Txxsp10YzPDuyBDUHl4EVBVEmIkUj3nJDhkSy9PiwMpdtmOiEGmR83zvZ7QorTYO7hNcDXFp6SYVNQ7VAGg5LA%2F0cfQU4JPGXjTUE3dQgWzb7WmUvThrpaSr0XawqWAw9Pq%2Fxwk4M1ICLfaW4l%2FdleXstfrhN4ZvnNYsDL4465vk9wAAAA%3D%3D%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EExecute%20Query%3C%2FA%3E%3C%2FU%3E%5D%3C%2FP%3E%0A%3CP%3EHostServiceSnapshot%3C%2FP%3E%0A%3CP%3E%7C%20where%20HostId%20%3D%3D%20%22a3d1a948-53ae-4175-8030-d7d174c427d1%22%3C%2FP%3E%0A%3CP%3E%7C%20sort%20by%20PreciseTimeStamp%20asc%3C%2FP%3E%0A%3CP%3E%7C%20extend%20shouldSelect%20%3D%20iff(BuildCommitHash%20!%3D%20prev(BuildCommitHash)%2C%20true%2C%20false)%3C%2FP%3E%0A%3CP%3E%7C%20where%20shouldSelect%20%7C%20project-away%20shouldSelect%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3EThe%20query%20above%20gives%20you%20the%20service%20changes%20for%20one%20host%20server.%20It%20can%20be%20modified%20to%20look%20at%20all%20host%20servers%2C%20by%20doing%20a%20sort%20by%20HostId%20and%20then%20PreciseTimeStamp.%20The%20query%20below%20also%20creates%20a%20new%20column%20that%20contains%20the%20old%20value%20and%20the%20new%20value%20being%20transitioned%20to.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%20dir%3D%22ltr%22%3E%5B%3CU%3E%3CA%20href%3D%22https%3A%2F%2Fdataexplorer.azure.com%2Fclusters%2Forbcluster.westus2%2Fdatabases%2Forbtestdb%3Fquery%3DH4sIAAAAAAAAA3VQSw6CMBDdN%2Bkdxq4gqUfQhWxwZ4IXKGUiNUJJO6ImHN4KxGjF5bw375OXW08Fut5oLFrV%2BdoSZwN46wjKB%2BSB3lcSDg618Xg0DRakmg6U168%2FvBO2FXQO%2B93VXKrMNo2hXPkaNiOaRHD6oSq%2FqaxW7QmDTiuPCWdTNqxmo%2BlMJQghOYvT5q8YluDJaUXJAhmcYL0FISHuGPyFGJveanT4p2iIFAKGkGvPqOlnI%2Fleb1nPGWdPaTLWTH8BAAA%3D%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EExecute%20Query%3C%2FA%3E%3C%2FU%3E%5D%3C%2FP%3E%0A%3CP%20dir%3D%22ltr%22%3EHostServiceSnapshot%3C%2FP%3E%0A%3CP%20dir%3D%22ltr%22%3E%7C%20sort%20by%20HostId%2C%20PreciseTimeStamp%20asc%3C%2FP%3E%0A%3CP%20dir%3D%22ltr%22%3E%7C%20extend%20prevBuildCommitHash%20%3D%20prev(BuildCommitHash)%3C%2FP%3E%0A%3CP%20dir%3D%22ltr%22%3E%7C%20extend%20buildCommitHashChange%20%3D%20case(%3C%2FP%3E%0A%3CP%20dir%3D%22ltr%22%3EHostId%20!%3D%20prev(HostId)%2C%20%22%22%2C%3C%2FP%3E%0A%3CP%20dir%3D%22ltr%22%3EBuildCommitHash%20!%3D%20prevBuildCommitHash%20%2C%20strcat(prevBuildCommitHash%2C%20%22%20-%26gt%3B%20%22%2C%20BuildCommitHash)%2C%3C%2FP%3E%0A%3CP%20dir%3D%22ltr%22%3E%22%22)%3C%2FP%3E%0A%3CP%20dir%3D%22ltr%22%3E%7C%20where%20buildCommitHashChange%20!%3D%20%22%22%20%7C%20project%20PreciseTimeStamp%2C%20HostId%2C%20buildCommitHashChange%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CH2%20id%3D%22toc-hId--1570077661%22%20id%3D%22toc-hId--1570077661%22%3EQuery%20Pattern%203%20%E2%80%93%20Event%20Correlation%20with%20Object%20Properties%3C%2FH2%3E%0A%3CP%3EBesides%20these%20snapshot%20events%2C%20our%20services%20push%20a%20lot%20of%20other%20traditional%20log%20events%20to%20Azure%20Data%20Explorer.%20What%20if%20you%20wanted%20to%20get%20the%20last%20known%20property%20of%20an%20object%20as%20another%20log%20event%20occurs%3F%20For%20example%2C%20let%E2%80%99s%20say%20you%20wanted%20to%20get%20a%20property%20of%20the%20host%20server%20right%20before%20a%20windows%20event%20occurs.%20The%20simplest%20option%20might%20be%20to%20log%20this%20property%20on%20the%20windows%20event%20table%20itself%20%E2%80%93%20however%2C%20this%20approach%20doesn%E2%80%99t%20scale%20well%20when%20an%20object%20has%20hundreds%20of%20properties.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20of%20the%20biggest%20advantages%20to%20having%20your%20object%20snapshots%20and%20log%20events%20both%20be%20in%20Azure%20Data%20Explorer%20is%20that%20it%20enables%20you%20to%20easily%20combine%20this%20information%20%E2%80%93%20you%20can%20take%20any%20log%20event%2C%20find%20the%20object%20associated%20with%20that%20event%20and%20pull%20in%20properties%20for%20that%20object%20and%20add%20it%20to%20the%20original%20event%2C%20making%20it%20seem%20like%20that%20property%20was%20logged%20natively%20on%20the%20event.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20pattern%20is%20based%20on%20time%20window%20joins%20described%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fkusto%2Fquery%2Fjoin-timewindow%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%3C%2FSPAN%3E.%20The%20query%20below%20takes%20all%20host%20windows%20events%20and%20adds%20the%20last%20known%20BuildCommitHash%20of%20the%20host%20server%20to%20each%20event.%20Note%20that%20events%20without%20a%20matching%20snapshot%20prior%20to%20the%20event%20are%20dropped%20here%2C%20though%20this%20can%20be%20tweaked%20depending%20on%20your%20use%20case.%20The%20lookback%20window%20here%20is%202%20hours%2C%20twice%20the%20snapshot%20interval%20for%20this%20object.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3E%5B%3CU%3E%3CA%20href%3D%22https%3A%2F%2Fdataexplorer.azure.com%2Fclusters%2Forbcluster.westus2%2Fdatabases%2Forbtestdb%3Fquery%3DH4sIAAAAAAAAA41SS2%2FTQBC%2BR8p%2FGHqyi3FoEVxQOJSHWiEeUipxRBvvON7Eu2t213GD%2BuOZcd3YTo0glyS7s99rvhIDlNbu6uqHMtI2sITL4u18Vh7Pr5Shw9HMAi7TlzT08Nd%2F3KMJMJ%2FdA94FNBJulcbPeKBnTpgNRmtlou8OM%2BWRr1ZB6ApejDCTni1OCAv%2B%2BZkEfd7DjBD%2FB7AfZyt6j3eVGJgJFsKhQptHUgQMdBp3D3l8aymmHVmhqDAPtg7oIGppr60PK3R7leHKiMoXNgDcP3isnN1iFuDbmr8umItSO7WVtBg3EhK4qlUp31utVbgWvhhE1Yc%2BlUwysheDNT1m93KwQHb3QfkgTIYXhJgJj2TmhByWSzg7SygZnveUVvTqzWtJ%2B1ss4KuFo9vc1kamcLMx1iGEAj0C6bNO%2BnQ%2BG3p%2FN%2BF9Ev6IrTwYbCjsUAgDyFUcMNGt8NBgWRLRRAUH3DHDkrmKVsca2xBAdinAGkODaNqblgW4HL6Twdk1BTo8ie7Z8ol8puEfrHxb%2BwC1R8kqBexFWSMXLVcl94daBMrQqZKgRcgoOCbytdbCqd8IhSK3vqjzvMRdu%2FtuqcJtfmqqwUhNAucxrA%2FdTPIkalb2SfH6yWNWWo%2Bk7tFhCqueh6S3qkmqrYiDtfyq0R2gQpdbp5kvZa2PBf9bpSca%2FQW9FxukZv8BUQH%2BWZ0EAAA%3D%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EExecute%20Query%3C%2FA%3E%3C%2FU%3E%5D%3C%2FP%3E%0A%3CP%3Elet%20lookupWindow%20%3D%202h%3B%3C%2FP%3E%0A%3CP%3Elet%20lookupBin%20%3D%20lookupWindow%20%2F%202.0%3B%3C%2FP%3E%0A%3CP%3EWindowsEvent%3C%2FP%3E%0A%3CP%3E%7C%20extend%20TimeKey%20%3D%20range(bin(PreciseTimeStamp%20-%20lookupWindow%2C%20lookupBin)%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20bin(PreciseTimeStamp%20%2B%20lookupBin%2C%20lookupBin)%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20lookupBin)%3C%2FP%3E%0A%3CP%3E%7C%20mvexpand%20TimeKey%20to%20typeof(datetime)%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%7C%20join%20kind%3D%20leftouter%20(%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%20HostServiceSnapshot%26nbsp%3B%20%7C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%20project%20Object1Time%20%3D%20PreciseTimeStamp%2C%20HostId%20%2C%20BuildCommitHash%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20TimeKey%20%3D%20bin(PreciseTimeStamp%2C%20lookupBin)%3C%2FP%3E%0A%3CP%3E)%20on%20HostId%20%2C%20TimeKey%3C%2FP%3E%0A%3CP%3E%7C%20extend%20timeDistance1%20%3D%20case(%3C%2FP%3E%0A%3CP%3EBuildCommitHash%20%3D%3D%20%22%22%2C%20totimespan(365d)%2C%20%2F%2F%20No%20Snapshot%20found.%20Ignore%20these%20records.%3C%2FP%3E%0A%3CP%3EObject1Time%20%26gt%3B%20PreciseTimeStamp%2C%20totimespan(365d)%2C%20%2F%2F%20Snapshot%20is%20newer%20than%20event.%20Ignore%20this%20as%20well.%3C%2FP%3E%0A%3CP%3EPreciseTimeStamp%20-%20Object1Time)%20%2F%2F%20Compute%20the%20time%20distance%20between%20the%20event%20and%20snapshot%3C%2FP%3E%0A%3CP%3E%7C%20where%20timeDistance1%20!%3D%20totimespan(365d)%20%2F%2F%20365d%20is%20just%20used%20as%20a%20value%20to%20filter%20out%20invalid%20matches%3C%2FP%3E%0A%3CP%3E%7C%20summarize%20hint.shufflekey%20%3D%20HostId%20arg_min(timeDistance1%2C%20*)%20by%20HostId%2C%20PreciseTimeStamp%20%2F%2F%20Find%20the%20closest%20snapshot.%20Shufflekey%20is%20used%20to%20optimize%20query%20performance.%3C%2FP%3E%0A%3CP%3E%7C%20project%20PreciseTimeStamp%2C%20HostId%2C%20BuildCommitHash%2C%20Message%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3EThis%20pattern%20allows%20most%20log%20events%20to%20only%20carry%20object%20identifiers%20and%20pull%20in%20all%20properties%20about%20that%20object%20dynamically%20using%20snapshots.%20This%20is%20especially%20useful%20in%20cases%20where%20the%20service%20that%20emits%20log%20events%20doesn%E2%80%99t%20have%20all%20the%20relevant%20properties%20available%20about%20the%20object%20%E2%80%93%20this%20is%20becoming%20more%20prevalent%20with%20microservices.%20Internally%2C%20we%20use%20similar%20queries%20for%20A%2FB%20testing%20and%20automated%20anomaly%20detection.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20a%20follow-up%20post%2C%20I%E2%80%99ll%20be%20sharing%20more%20details%20about%20a%20Live%20Site%20tool%20built%20around%20these%20object%20snapshots%20that%20will%20be%20available%20on%20GitHub%20soon.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEdit%3A%20The%20follow%20up%20post%20on%20Orb%20is%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FAzure-Data-Explorer%2FOrb-A-tool-to-run-Live-Site-using-Azure-Data-Explorer%2Ftd-p%2F757581%22%20target%3D%22_self%22%3Ehere.%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-740546%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Data%20Explorer%20(Kusto)%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
gauravjag
Microsoft

Over the past few years, Azure Data Explorer has become a critical part of running Live Site for most internal Azure services. This post describes a simple approach to model objects in your services using event streams ingested into Azure Data Explorer. The sample streams described below are based on real streams used by teams in Azure Compute (including my team) to monitor and manage all Virtual Machines (VMs) and host servers in Azure. We’ve built a lot of tooling in Azure Compute around the data modeling and query patterns shared below.

 

If you don’t already have a pipeline setup for your service logs to be ingested into Azure Data Explorer, take a look at this guide.

 

Snapshot Events

Object properties are modeled as columns of a table and each row is a snapshot representation of that object.

Let’s look at a sample event schema for a VM.

 

[Execute Query]

VMSnapshot

| take 1

 

VMId

HostId

OsImage

State

Region

PreciseTimeStamp

3f576830-2d8f-481b-8448-bca561090831

a3d1a948-53ae-4175-8030-d7d174c427d1

RServer-WS2016

Started

westus

2019-05-21T19:25:39Z

 

Our services emit these snapshots periodically and on every column change – the latter is essential to look at when things changed without waiting on the snapshot period.

 

We do this for every key object modeled in the system – service endpoints, configuration values, VMs, host servers, etc. This snapshot pattern works well for long lived objects – short lived objects like requests typically have their properties emitted as part of the initial request processing. You can also consider pushing immutable properties of an object as a separate stream with lower snapshot frequencies.

 

Query Pattern 1 – What is the latest state of objects?

In the example above, if you wanted to get the latest state of a single object, this is quite straightforward:

 

[Execute Query]

VMSnapshot

| where VMId == "3f576830-2d8f-481b-8448-bca561090831" | top 1 by PreciseTimeStamp desc

What if you wanted the latest snapshot for several objects at a time? This is where arg_max is helpful.

arg_max finds rows corresponding to the expression that needs to be maximized. If there are n VMs in your query, arg_max can be used to maximize PreciseTimeStamp for every unique VMId – the end result will be n rows, where each row corresponds to the latest snapshot for that VM.

For example, the query below finds the latest VM state distribution in the region westus.

 

[Execute Query]

VMSnapshot

| where Region == "westus" and PreciseTimeStamp > ago(2h)

| summarize arg_max(PreciseTimeStamp, *) by VMId

| summarize count() by State 

The other thing to note is that the query looks back 2 hours here – 2x the snapshot interval which is hourly for this table. Since the full snapshot event is emitted on every property change for the VM, this query does give you the latest state of every object in Azure Data Explorer, not just what was visible on the hourly snapshot.

Also, the query result might include VMs that have already been deleted. To deal with this, we model the deletion itself as a state on the snapshot, so that such results can be filtered if required.

Since we also model every service configuration value and the whole service itself as an object, we use the pattern above to track each config value and build version as rollouts occur in the fleet.

For example, the query below gives us the version distribution of our services running across Azure host servers.

 

[Execute Query]

HostServiceSnapshot

| summarize arg_max(PreciseTimeStamp, *) by HostId

| summarize dcount(HostId) by Region, BuildCommitHash

The BuildCommitHash in our snapshots enables us to track every code change committed as it’s deployed across the fleet. We have some tooling that takes in a commit, gets the latest snapshots of the service, runs git merge-base and provides the engineer with the exact spread of host servers that have that fix. We also use similar queries to setup reports using Microsoft Flow to track build versions, A/B testing of config and more.

 

Query Pattern 2 – When did a Property Value Change?

Let’s say you wanted to know when the version of a host service changed using snapshots – you could do a self-join with the same table, though this makes for a complicated query. This pattern is solved quite cleanly with the sort and prev operators.

 

The prev operator allows you to access rows before the current row being processed (from a sorted dataset). To find the when a value changed, you can now compare row n to row n-1 quite easily.

 

[Execute Query]

HostServiceSnapshot

| where HostId == "a3d1a948-53ae-4175-8030-d7d174c427d1"

| sort by PreciseTimeStamp asc

| extend shouldSelect = iff(BuildCommitHash != prev(BuildCommitHash), true, false)

| where shouldSelect | project-away shouldSelect

The query above gives you the service changes for one host server. It can be modified to look at all host servers, by doing a sort by HostId and then PreciseTimeStamp. The query below also creates a new column that contains the old value and the new value being transitioned to.

 

[Execute Query]

HostServiceSnapshot

| sort by HostId, PreciseTimeStamp asc

| extend prevBuildCommitHash = prev(BuildCommitHash)

| extend buildCommitHashChange = case(

HostId != prev(HostId), "",

BuildCommitHash != prevBuildCommitHash , strcat(prevBuildCommitHash, " -> ", BuildCommitHash),

"")

| where buildCommitHashChange != "" | project PreciseTimeStamp, HostId, buildCommitHashChange

Query Pattern 3 – Event Correlation with Object Properties

Besides these snapshot events, our services push a lot of other traditional log events to Azure Data Explorer. What if you wanted to get the last known property of an object as another log event occurs? For example, let’s say you wanted to get a property of the host server right before a windows event occurs. The simplest option might be to log this property on the windows event table itself – however, this approach doesn’t scale well when an object has hundreds of properties.

 

One of the biggest advantages to having your object snapshots and log events both be in Azure Data Explorer is that it enables you to easily combine this information – you can take any log event, find the object associated with that event and pull in properties for that object and add it to the original event, making it seem like that property was logged natively on the event.

 

This pattern is based on time window joins described here. The query below takes all host windows events and adds the last known BuildCommitHash of the host server to each event. Note that events without a matching snapshot prior to the event are dropped here, though this can be tweaked depending on your use case. The lookback window here is 2 hours, twice the snapshot interval for this object.

 

[Execute Query]

let lookupWindow = 2h;

let lookupBin = lookupWindow / 2.0;

WindowsEvent

| extend TimeKey = range(bin(PreciseTimeStamp - lookupWindow, lookupBin),

                                  bin(PreciseTimeStamp + lookupBin, lookupBin),

                                  lookupBin)

| mvexpand TimeKey to typeof(datetime)      

| join kind= leftouter (

   HostServiceSnapshot  |

   project Object1Time = PreciseTimeStamp, HostId , BuildCommitHash,

       TimeKey = bin(PreciseTimeStamp, lookupBin)

) on HostId , TimeKey

| extend timeDistance1 = case(

BuildCommitHash == "", totimespan(365d), // No Snapshot found. Ignore these records.

Object1Time > PreciseTimeStamp, totimespan(365d), // Snapshot is newer than event. Ignore this as well.

PreciseTimeStamp - Object1Time) // Compute the time distance between the event and snapshot

| where timeDistance1 != totimespan(365d) // 365d is just used as a value to filter out invalid matches

| summarize hint.shufflekey = HostId arg_min(timeDistance1, *) by HostId, PreciseTimeStamp // Find the closest snapshot. Shufflekey is used to optimize query performance.

| project PreciseTimeStamp, HostId, BuildCommitHash, Message

This pattern allows most log events to only carry object identifiers and pull in all properties about that object dynamically using snapshots. This is especially useful in cases where the service that emits log events doesn’t have all the relevant properties available about the object – this is becoming more prevalent with microservices. Internally, we use similar queries for A/B testing and automated anomaly detection.

 

In a follow-up post, I’ll be sharing more details about a Live Site tool built around these object snapshots that will be available on GitHub soon.

 

Edit: The follow up post on Orb is here.

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
48 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
30 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies