@@ -8,6 +8,7 @@ namespace VirtualClient.Actions
88 using System . Linq ;
99 using System . Net ;
1010 using System . Net . Http ;
11+ using System . Text . RegularExpressions ;
1112 using System . Threading ;
1213 using System . Threading . Tasks ;
1314 using Microsoft . Extensions . DependencyInjection ;
@@ -19,6 +20,7 @@ namespace VirtualClient.Actions
1920 using VirtualClient . Common . Extensions ;
2021 using VirtualClient . Common . Telemetry ;
2122 using VirtualClient . Contracts ;
23+ using VirtualClient . Contracts . Metadata ;
2224 using VirtualClient . Logging ;
2325
2426 /// <summary>
@@ -195,7 +197,7 @@ protected override Task ExecuteAsync(EventContext telemetryContext, Cancellation
195197
196198 await this . SaveStateAsync ( telemetryContext , cancellationToken ) ;
197199 this . SetServerOnline ( true ) ;
198-
200+ this . CaptureVersion ( telemetryContext , cancellationToken ) ;
199201 if ( this . IsMultiRoleLayout ( ) )
200202 {
201203 using ( BackgroundOperations profiling = BackgroundOperations . BeginProfiling ( this , cancellationToken ) )
@@ -247,6 +249,54 @@ protected override async Task InitializeAsync(EventContext telemetryContext, Can
247249 this . InitializeApiClients ( ) ;
248250 }
249251
252+ /// <summary>
253+ /// Initializes the environment and dependencies for server of redis workload.
254+ /// </summary>
255+ /// <param name="telemetryContext">Provides context information that will be captured with telemetry events.</param>
256+ /// <param name="cancellationToken">A token that can be used to cancel the operation.</param>
257+ /// <returns>redisVersion</returns>
258+ protected string GetRedisVersion ( EventContext telemetryContext , CancellationToken cancellationToken )
259+ {
260+ string command = "redis-cli INFO SERVER" ;
261+ string versionPattern = @"redis_version:(\d+\.\d+\.\d+)" ;
262+ Regex versionRegex = new Regex ( versionPattern , RegexOptions . Compiled ) ;
263+ string redisVersion = null ;
264+ try
265+ {
266+ using ( IProcessProxy process = this . ExecuteCommandAsync (
267+ command ,
268+ commandArguments : null ,
269+ workingDirectory : this . RedisPackagePath ,
270+ telemetryContext : telemetryContext ,
271+ cancellationToken : cancellationToken ,
272+ runElevated : true ) . Result )
273+ {
274+ if ( ! cancellationToken . IsCancellationRequested )
275+ {
276+ string output = process . StandardOutput . ToString ( ) ;
277+ Match match = versionRegex . Match ( output ) ;
278+ if ( match . Success )
279+ {
280+ redisVersion = match . Groups [ 1 ] . Value ;
281+ telemetryContext . AddContext ( "redisVersion" , redisVersion ) ;
282+ this . Logger . LogMessage ( $ "{ this . TypeName } .RedisVersionCaptured", LogLevel . Information , telemetryContext ) ;
283+ }
284+ else
285+ {
286+ throw new WorkloadException ( "Failed to parse Redis version from output." , ErrorReason . CriticalWorkloadFailure ) ;
287+ }
288+ }
289+ }
290+ }
291+ catch ( Exception ex )
292+ {
293+ this . Logger . LogMessage ( $ "{ this . TypeName } .RedisVersionCaptureError", LogLevel . Error , telemetryContext . Clone ( ) . AddError ( ex ) ) ;
294+ throw ;
295+ }
296+
297+ return redisVersion ;
298+ }
299+
250300 /// <summary>
251301 /// Validates the component definition for requirements.
252302 /// </summary>
@@ -442,5 +492,18 @@ private Task StartServerInstanceAsync(int port, string command, string commandAr
442492 }
443493 } ) ;
444494 }
495+
496+ private void CaptureVersion ( EventContext telemetryContext , CancellationToken cancellationToken )
497+ {
498+ if ( ! cancellationToken . IsCancellationRequested )
499+ {
500+ string redisversion = this . GetRedisVersion ( telemetryContext , cancellationToken ) ;
501+ this . MetadataContract . AddForScenario (
502+ "Redis-Benchmark" ,
503+ null ,
504+ toolVersion : redisversion ) ;
505+ this . MetadataContract . Apply ( telemetryContext ) ;
506+ }
507+ }
445508 }
446509}
0 commit comments