|
3 | 3 | using System.Collections.Generic; |
4 | 4 | using System.Linq; |
5 | 5 | using System.Reflection; |
| 6 | +#if NETFRAMEWORK |
| 7 | +using System.Runtime.Remoting.Messaging; |
| 8 | +#endif |
6 | 9 | using System.Text; |
7 | 10 | using Newtonsoft.Json.Linq; |
8 | 11 |
|
@@ -312,6 +315,265 @@ public static string CleanPartialUrl(string url) |
312 | 315 |
|
313 | 316 | return sbNewUrl.ToString(); |
314 | 317 | } |
| 318 | + public static string GetRequestId() |
| 319 | + { |
| 320 | + string reqId = null; |
| 321 | + |
| 322 | +#if NETFULL |
| 323 | + try |
| 324 | + { |
| 325 | + if (string.IsNullOrEmpty(reqId)) |
| 326 | + { |
| 327 | + var stackifyRequestID = CallContext.LogicalGetData("Stackify-RequestID"); |
| 328 | + |
| 329 | + if (stackifyRequestID != null) |
| 330 | + { |
| 331 | + reqId = stackifyRequestID.ToString(); |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + if (string.IsNullOrEmpty(reqId)) |
| 336 | + { |
| 337 | + //gets from Trace.CorrelationManager.ActivityId but doesnt assume it is guid since it technically doesn't have to be |
| 338 | + //not calling the CorrelationManager method because it blows up if it isn't a guid |
| 339 | + var correltionManagerId = CallContext.LogicalGetData("E2ETrace.ActivityID"); |
| 340 | + |
| 341 | + if (correltionManagerId != null && correltionManagerId is Guid && |
| 342 | + ((Guid) correltionManagerId) != Guid.Empty) |
| 343 | + { |
| 344 | + reqId = correltionManagerId.ToString(); |
| 345 | + } |
| 346 | + } |
| 347 | + } |
| 348 | + catch (System.Web.HttpException ex) |
| 349 | + { |
| 350 | + StackifyAPILogger.Log("Request not available \r\n" + ex.ToString()); |
| 351 | + } |
| 352 | + catch (Exception ex) |
| 353 | + { |
| 354 | + StackifyAPILogger.Log("Error figuring out TransID \r\n" + ex.ToString()); |
| 355 | + } |
| 356 | +#endif |
| 357 | + |
| 358 | + try |
| 359 | + { |
| 360 | + if (string.IsNullOrEmpty(reqId)) |
| 361 | + { |
| 362 | + reqId = getContextProperty("RequestId") as string; |
| 363 | + } |
| 364 | + } |
| 365 | + catch (Exception ex) |
| 366 | + { |
| 367 | + StackifyAPILogger.Log("Error figuring out TransID \r\n" + ex.ToString()); |
| 368 | + } |
| 369 | + |
| 370 | + return reqId; |
| 371 | + } |
| 372 | + |
| 373 | + public static string GetReportingUrl() |
| 374 | + { |
| 375 | + return GetStackifyProperty("REPORTING_URL"); |
| 376 | + } |
| 377 | + |
| 378 | + public static string GetAppName() |
| 379 | + { |
| 380 | + if (!IsBeingProfiled) |
| 381 | + { |
| 382 | + return Config.AppName; |
| 383 | + } |
| 384 | + |
| 385 | + // Getting profiler app name and environment are a side effect of checking the profiler wrapper |
| 386 | + GetWrapperAssembly(); |
| 387 | + |
| 388 | + return _profilerAppName ?? Config.AppName; |
| 389 | + } |
| 390 | + |
| 391 | + public static string GetAppEnvironment() |
| 392 | + { |
| 393 | + if (!IsBeingProfiled) |
| 394 | + { |
| 395 | + return Config.AppName; |
| 396 | + } |
| 397 | + |
| 398 | + // Getting profiler app name and environment are a side effect of checking the profiler wrapper |
| 399 | + GetWrapperAssembly(); |
| 400 | + |
| 401 | + return _profilerEnvironment ?? Config.Environment; |
| 402 | + } |
| 403 | + |
| 404 | + private static Assembly _wrapperAssembly = null; |
| 405 | + private static Type _stackifyCallContextType = null; |
| 406 | + |
| 407 | + protected static Assembly GetWrapperAssembly() |
| 408 | + { |
| 409 | + if (!IsBeingProfiled) |
| 410 | + { |
| 411 | + return null; |
| 412 | + } |
| 413 | + |
| 414 | + if (_wrapperAssembly != null) |
| 415 | + { |
| 416 | + return _wrapperAssembly; |
| 417 | + } |
| 418 | + |
| 419 | + try |
| 420 | + { |
| 421 | + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); |
| 422 | + |
| 423 | + var agentAssemblyQry = assemblies.Where(assembly => assembly.FullName.StartsWith("Stackify.Agent,")); |
| 424 | + |
| 425 | + foreach (var middleware in agentAssemblyQry) |
| 426 | + { |
| 427 | + var stackifyCallContextType = middleware?.GetType("Stackify.Agent.Threading.StackifyCallContext"); |
| 428 | + |
| 429 | + if (stackifyCallContextType != null) |
| 430 | + { |
| 431 | + GetProfilerAppNameAndEnvironment(middleware); |
| 432 | + |
| 433 | + _stackifyCallContextType = stackifyCallContextType; |
| 434 | + _wrapperAssembly = middleware; |
| 435 | + } |
| 436 | + } |
| 437 | + } |
| 438 | + catch |
| 439 | + { |
| 440 | + // Ignore errors |
| 441 | + } |
| 442 | + |
| 443 | + return _wrapperAssembly; |
| 444 | + } |
| 445 | + |
| 446 | + private static string _profilerAppName = null; |
| 447 | + private static string _profilerEnvironment = null; |
| 448 | + |
| 449 | + private static void GetProfilerAppNameAndEnvironment(Assembly middleware) |
| 450 | + { |
| 451 | + try |
| 452 | + { |
| 453 | + // Get AppName and Environment and cache them, so we won't query over and over. |
| 454 | + var agentConfigType = middleware?.GetType("Stackify.Agent.Configuration.AgentConfig"); |
| 455 | + |
| 456 | + if (agentConfigType != null) |
| 457 | + { |
| 458 | + var profilerSettings = agentConfigType.GetProperty("Settings")?.GetValue(null, null); |
| 459 | + |
| 460 | + if (profilerSettings != null) |
| 461 | + { |
| 462 | + var settingsType = profilerSettings.GetType(); |
| 463 | + |
| 464 | + _profilerAppName = settingsType?.GetProperty("AppName")?.GetValue(profilerSettings, null) as string; |
| 465 | + _profilerEnvironment = settingsType?.GetProperty("Environment")?.GetValue(profilerSettings, null) as string; |
| 466 | + } |
| 467 | + } |
| 468 | + } |
| 469 | + catch |
| 470 | + { |
| 471 | + // Ignore errors here |
| 472 | + } |
| 473 | + } |
| 474 | + |
| 475 | + private static object getContextProperty(string propName) |
| 476 | + { |
| 477 | + if (!IsBeingProfiled || string.IsNullOrWhiteSpace(propName)) |
| 478 | + { |
| 479 | + return null; |
| 480 | + } |
| 481 | + |
| 482 | + if (_wrapperAssembly == null) |
| 483 | + { |
| 484 | + GetWrapperAssembly(); |
| 485 | + } |
| 486 | + |
| 487 | + try |
| 488 | + { |
| 489 | + if (_stackifyCallContextType != null) |
| 490 | + { |
| 491 | + var traceContextProp = _stackifyCallContextType.GetProperty("TraceContext")?.GetValue(null, null); |
| 492 | + if (traceContextProp != null) |
| 493 | + { |
| 494 | + var traceContextType = traceContextProp.GetType(); |
| 495 | + var contextProp = traceContextType.GetProperty(propName); |
| 496 | + var propValue = contextProp?.GetValue(traceContextProp, null); |
| 497 | + return propValue; |
| 498 | + } |
| 499 | + } |
| 500 | + } |
| 501 | + catch |
| 502 | + { |
| 503 | + // Ignore errors |
| 504 | + } |
| 505 | + |
| 506 | + return null; |
| 507 | + } |
| 508 | + |
| 509 | + private static string GetStackifyProperty(string propName) |
| 510 | + { |
| 511 | + if (!IsBeingProfiled || string.IsNullOrWhiteSpace(propName)) |
| 512 | + { |
| 513 | + return null; |
| 514 | + } |
| 515 | + |
| 516 | + try |
| 517 | + { |
| 518 | + var stackifyProps = getContextProperty("props"); |
| 519 | + |
| 520 | + if (stackifyProps != null) |
| 521 | + { |
| 522 | + var propsType = stackifyProps.GetType(); |
| 523 | + |
| 524 | + var getItemMethod = propsType.GetMethod("get_Item", new[] {typeof(string)}); |
| 525 | + |
| 526 | + if (getItemMethod != null) |
| 527 | + { |
| 528 | + return getItemMethod.Invoke(stackifyProps, new[] {propName}) as string; |
| 529 | + } |
| 530 | + } |
| 531 | + } |
| 532 | + catch |
| 533 | + { |
| 534 | + // Ignore Errors |
| 535 | + } |
| 536 | + |
| 537 | + return null; |
| 538 | + } |
| 539 | + |
| 540 | + private static Boolean? _isBeingProfiled = null; |
| 541 | + |
| 542 | + protected static bool IsBeingProfiled |
| 543 | + { |
| 544 | + get |
| 545 | + { |
| 546 | + if (_isBeingProfiled.HasValue) |
| 547 | + { |
| 548 | + return _isBeingProfiled.Value; |
| 549 | + } |
| 550 | + |
| 551 | +#if NETFRAMEWORK |
| 552 | + var profilerEnv = "COR_ENABLE_PROFILING"; |
| 553 | + var profilerUuidEnv = "COR_PROFILER"; |
| 554 | +#else |
| 555 | + var profilerEnv = "CORECLR_ENABLE_PROFILING"; |
| 556 | + var profilerUuidEnv = "CORECLR_PROFILER"; |
| 557 | +#endif |
| 558 | + |
| 559 | + var enableString = Environment.GetEnvironmentVariable(profilerEnv); |
| 560 | + var uuidString = Environment.GetEnvironmentVariable(profilerUuidEnv); |
| 561 | + |
| 562 | + if (!string.IsNullOrWhiteSpace(enableString) && |
| 563 | + !string.IsNullOrWhiteSpace(uuidString)) |
| 564 | + { |
| 565 | + _isBeingProfiled = string.Equals("1", enableString?.Trim()) |
| 566 | + && string.Equals("{cf0d821e-299b-5307-a3d8-b283c03916da}", uuidString.Trim(), StringComparison.OrdinalIgnoreCase); |
| 567 | + } |
| 568 | + else |
| 569 | + { |
| 570 | + _isBeingProfiled = false; |
| 571 | + } |
| 572 | + |
| 573 | + return _isBeingProfiled.Value; |
| 574 | + } |
| 575 | + } |
| 576 | + |
315 | 577 | } |
316 | 578 |
|
317 | 579 | public class ToStringConverter : JsonConverter |
|
0 commit comments