private static bool MakeSamplingDecision( SpanContext parent, string name, ISampler sampler, IEnumerable <ISpan> parentLinks, ITraceId traceId, ISpanId spanId, ITraceParams activeTraceParams) { // If users set a specific sampler in the SpanBuilder, use it. if (sampler != null) { return(sampler.ShouldSample(parent, traceId, spanId, name, parentLinks)); } // Use the default sampler if this is a root Span or this is an entry point Span (has remote // parent). if (parent == null || !parent.IsValid) { return(activeTraceParams .Sampler .ShouldSample(parent, traceId, spanId, name, parentLinks)); } // Parent is always different than null because otherwise we use the default sampler. return(parent.TraceOptions.IsSampled || IsAnyParentLinkSampled(parentLinks)); }
//public virtual void AddMessageEvent(MessageEventBase messageEvent) //{ // Default implementation by invoking addNetworkEvent() so that any existing derived classes, // including implementation and the mocked ones, do not need to override this method explicitly. //addNetworkEvent(BaseMessageEventUtil.asNetworkEvent(messageEvent)); //} //public abstract void AddLink(LinkBase link); private Span( ISpanContext context, SpanOptions options, string name, ISpanId parentSpanId, bool?hasRemoteParent, ITraceParams traceParams, IStartEndHandler startEndHandler, ITimestampConverter timestampConverter, IClock clock) : base(context, options) { this.parentSpanId = parentSpanId; this.hasRemoteParent = hasRemoteParent; this.name = name; this.traceParams = traceParams; this.startEndHandler = startEndHandler; this.clock = clock; this.hasBeenEnded = false; this.sampleToLocalSpanStore = false; if (options.HasFlag(SpanOptions.RECORD_EVENTS)) { this.timestampConverter = timestampConverter != null ? timestampConverter : Census.Internal.TimestampConverter.Now(clock); startNanoTime = clock.NowNanos; } else { this.startNanoTime = 0; this.timestampConverter = timestampConverter; } }
internal static ISpan StartSpan( Activity activity, Tracestate tracestate, SpanKind spanKind, ITraceParams traceParams, IStartEndHandler startEndHandler, bool ownsActivity = true) { var span = new Span( activity, tracestate, spanKind, traceParams, startEndHandler, ownsActivity); // Call onStart here instead of calling in the constructor to make sure the span is completely // initialized. if (span.IsRecordingEvents) { startEndHandler.OnStart(span); } return(span); }
private Span( ISpanContext context, SpanOptions options, string name, ISpanId parentSpanId, bool?hasRemoteParent, ITraceParams traceParams, IStartEndHandler startEndHandler, ITimestampConverter timestampConverter, IClock clock) : base(context, options) { this.parentSpanId = parentSpanId; this.hasRemoteParent = hasRemoteParent; this.Name = name; this.traceParams = traceParams ?? throw new ArgumentNullException(nameof(traceParams)); this.startEndHandler = startEndHandler; this.clock = clock; this.hasBeenEnded = false; this.sampleToLocalSpanStore = false; if (options.HasFlag(SpanOptions.RecordEvents)) { this.timestampConverter = timestampConverter ?? OpenCensus.Internal.TimestampConverter.Now(clock); this.startNanoTime = clock.NowNanos; } else { this.startNanoTime = 0; this.timestampConverter = timestampConverter; } }
internal static ISpan StartSpan( SpanContext context, SpanOptions options, string name, SpanKind spanKind, ISpanId parentSpanId, ITraceParams traceParams, IStartEndHandler startEndHandler, Timer timestampConverter) { var span = new Span( context, options, name, spanKind, parentSpanId, traceParams, startEndHandler, timestampConverter); // Call onStart here instead of calling in the constructor to make sure the span is completely // initialized. if (span.IsRecordingEvents) { startEndHandler.OnStart(span); } return(span); }
internal static ISpan StartSpan( ISpanContext context, SpanOptions options, string name, ISpanId parentSpanId, bool?hasRemoteParent, ITraceParams traceParams, IStartEndHandler startEndHandler, ITimestampConverter timestampConverter, IClock clock ) { var span = new Span( context, options, name, parentSpanId, hasRemoteParent, traceParams, startEndHandler, timestampConverter, clock); // Call onStart here instead of calling in the constructor to make sure the span is completely // initialized. if (span.Options.HasFlag(SpanOptions.RECORD_EVENTS)) { startEndHandler.OnStart(span); } return(span); }
private ISpan StartSpanInternal( SpanContext parent, string name, ISampler sampler, IEnumerable <ISpan> parentLinks, bool recordEvents, Timer timestampConverter) { ITraceParams activeTraceParams = this.Options.TraceConfig.ActiveTraceParams; IRandomGenerator random = this.Options.RandomHandler; ITraceId traceId; ISpanId spanId = SpanId.GenerateRandomId(random); ISpanId parentSpanId = null; TraceOptionsBuilder traceOptionsBuilder; if (parent == null || !parent.IsValid) { // New root span. traceId = TraceId.GenerateRandomId(random); traceOptionsBuilder = TraceOptions.Builder(); } else { // New child span. traceId = parent.TraceId; parentSpanId = parent.SpanId; traceOptionsBuilder = TraceOptions.Builder(parent.TraceOptions); } traceOptionsBuilder.SetIsSampled( MakeSamplingDecision( parent, name, sampler, parentLinks, traceId, spanId, activeTraceParams)); TraceOptions traceOptions = traceOptionsBuilder.Build(); SpanOptions spanOptions = SpanOptions.None; if (traceOptions.IsSampled || recordEvents) { spanOptions = SpanOptions.RecordEvents; } ISpan span = Span.StartSpan( SpanContext.Create(traceId, spanId, traceOptions, parent?.Tracestate ?? Tracestate.Empty), spanOptions, name, this.Kind, parentSpanId, activeTraceParams, this.Options.StartEndHandler, timestampConverter); LinkSpans(span, parentLinks); return(span); }
internal static object Run(string zipkinUri) { // 1. Configure exporter to export traces to Zipkin var exporter = new ZipkinTraceExporter( new ZipkinTraceExporterOptions() { Endpoint = new Uri(zipkinUri), ServiceName = "tracing-to-zipkin-service", }, Tracing.ExportComponent); exporter.Start(); // 2. Configure 100% sample rate for the purposes of the demo ITraceConfig traceConfig = Tracing.TraceConfig; ITraceParams currentConfig = traceConfig.ActiveTraceParams; var newConfig = currentConfig.ToBuilder() .SetSampler(Samplers.AlwaysSample) .Build(); traceConfig.UpdateActiveTraceParams(newConfig); // 3. Tracer is global singleton. You can register it via dependency injection if it exists // but if not - you can use it as follows: var tracer = Tracing.Tracer; var collector = new StackExchangeRedisCallsCollector(null, tracer, null, Tracing.ExportComponent); // connect to the server ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("localhost:6379"); connection.RegisterProfiler(collector.GetProfilerSessionsFactory()); // select a database (by default, DB = 0) IDatabase db = connection.GetDatabase(); // 4. Create a scoped span. It will end automatically when using statement ends using (var scope = tracer.SpanBuilder("Main").StartScopedSpan()) { Console.WriteLine("About to do a busy work"); for (int i = 0; i < 10; i++) { DoWork(db, i); } } // 5. Gracefully shutdown the exporter so it'll flush queued traces to Zipkin. Tracing.ExportComponent.SpanExporter.Dispose(); return(null); }
private static void ConfigureOpencensus(string zipkinUri, string appInsightsKey) { //if we want to have multiple tracers //we should use agent mode and send first to agent var ocExp = new OpenCensus.Exporter.Ocagent.OcagentExporter( Tracing.ExportComponent , "http://localhost:55678" , Environment.MachineName , "distributedtracingdemo" ); ocExp.Start(); if (!string.IsNullOrEmpty(zipkinUri)) { var zipK = new ZipkinTraceExporter( new ZipkinTraceExporterOptions() { Endpoint = new Uri(zipkinUri), ServiceName = "tracing-to-zipkin-service", }, Tracing.ExportComponent ); zipK.Start(); } if (!string.IsNullOrEmpty(appInsightsKey)) { var config = new TelemetryConfiguration(appInsightsKey); var appI = new ApplicationInsightsExporter( Tracing.ExportComponent, Stats.ViewManager, config); appI.Start(); } //Metrics //define the measure to be used // context fields defined, that will be attached var invokeView = View.Create(ViewName.Create("executioncounts"), "Number of execs", numberofInvocations, Count.Create(), new[] { tagEnv, tagMachine }); //register Stats.ViewManager.RegisterView(invokeView); // 2. Configure 100% sample rate for the purposes of the demo ITraceConfig traceConfig = Tracing.TraceConfig; ITraceParams currentConfig = traceConfig.ActiveTraceParams; var newConfig = currentConfig.ToBuilder() .SetSampler(Samplers.AlwaysSample) .Build(); traceConfig.UpdateActiveTraceParams(newConfig); }
internal static object Run(string host, int port) { // 1. Configure exporter to export traces to Jaeger var exporter = new JaegerExporter( new JaegerExporterOptions { ServiceName = "tracing-to-jaeger-service", AgentHost = host, AgentPort = port, }, Tracing.SpanExporter); exporter.Start(); // 2. Configure 100% sample rate for the purposes of the demo ITraceConfig traceConfig = Tracing.TraceConfig; ITraceParams currentConfig = traceConfig.ActiveTraceParams; var newConfig = currentConfig.ToBuilder() .SetSampler(Samplers.AlwaysSample) .Build(); traceConfig.UpdateActiveTraceParams(newConfig); // 3. Tracer is global singleton. You can register it via dependency injection if it exists // but if not - you can use it as follows: var tracer = Tracing.Tracer; // 4. Create a scoped span. It will end automatically when using statement ends using (tracer.WithSpan(tracer.SpanBuilder("Main").StartSpan())) { tracer.CurrentSpan.SetAttribute("custom-attribute", 55); Console.WriteLine("About to do a busy work"); for (int i = 0; i < 10; i++) { DoWork(i); } } // 5. Gracefully shutdown the exporter so it'll flush queued traces to Zipkin. Tracing.SpanExporter.Dispose(); return(null); }
internal static object Run(string zipkinUri) { // 1. Configure exporter to export traces to Zipkin var exporter = new ZipkinTraceExporter( new ZipkinTraceExporterOptions() { Endpoint = new Uri(zipkinUri), ServiceName = "tracing-to-zipkin-service", }, Tracing.ExportComponent); exporter.Start(); // 2. Configure 100% sample rate for the purposes of the demo ITraceConfig traceConfig = Tracing.TraceConfig; ITraceParams currentConfig = traceConfig.ActiveTraceParams; var newConfig = currentConfig.ToBuilder() .SetSampler(Samplers.AlwaysSample) .Build(); traceConfig.UpdateActiveTraceParams(newConfig); // 3. Tracer is global singleton. You can register it via dependency injection if it exists // but if not - you can use it as follows: var tracer = Tracing.Tracer; // 4. Create a scoped span. It will end automatically when using statement ends using (var scope = tracer.SpanBuilder("Main").StartScopedSpan()) { Console.WriteLine("About to do a busy work"); for (int i = 0; i < 10; i++) { DoWork(i); } } // 5. Gracefully shutdown the exporter so it'll flush queued traces to Zipkin. Tracing.ExportComponent.SpanExporter.Dispose(); return(null); }
private Span( Activity activity, Tracestate tracestate, SpanKind spanKind, ITraceParams traceParams, IStartEndHandler startEndHandler, bool ownsActivity) { this.Activity = activity; this.spanContext = new Lazy <SpanContext>(() => SpanContext.Create( this.Activity.TraceId, this.Activity.SpanId, this.Activity.ActivityTraceFlags, tracestate)); this.Name = this.Activity.OperationName; this.traceParams = traceParams ?? throw new ArgumentNullException(nameof(traceParams)); this.startEndHandler = startEndHandler; this.Kind = spanKind; this.OwnsActivity = ownsActivity; this.IsRecordingEvents = this.Activity.Recorded; }
private Span( SpanContext context, SpanOptions options, string name, SpanKind spanKind, SpanId parentSpanId, ITraceParams traceParams, IStartEndHandler startEndHandler, Timer timestampConverter) { this.Context = context; this.Options = options; this.parentSpanId = parentSpanId; this.Name = name; this.traceParams = traceParams ?? throw new ArgumentNullException(nameof(traceParams)); this.startEndHandler = startEndHandler; this.hasBeenEnded = false; this.sampleToLocalSpanStore = false; this.Kind = spanKind; if (this.IsRecordingEvents) { if (timestampConverter == null) { this.TimestampConverter = Timer.StartNew(); this.startTime = this.TimestampConverter.StartTime; } else { this.TimestampConverter = timestampConverter; this.startTime = this.TimestampConverter.Now; } } else { this.startTime = DateTimeOffset.MinValue; this.TimestampConverter = timestampConverter; } }
private static void ConsfigExporter() { // 1. Configure exporter to export traces to Jaeger var exporter = new JaegerExporter( new JaegerExporterOptions { ServiceName = "OpenTelemetrySample", AgentHost = "localhost", AgentPort = 5775, }, Tracing.SpanExporter); exporter.Start(); // 2. Configure 100% sample rate for the purposes of the demo ITraceConfig traceConfig = Tracing.TraceConfig; ITraceParams currentConfig = traceConfig.ActiveTraceParams; var newConfig = currentConfig.ToBuilder() .SetSampler(Samplers.AlwaysSample) .Build(); traceConfig.UpdateActiveTraceParams(newConfig); }
private Span( ISpanContext context, SpanOptions options, string name, ISpanId parentSpanId, bool?hasRemoteParent, ITraceParams traceParams, IStartEndHandler startEndHandler, Timer timestampConverter) : base(context, options) { this.parentSpanId = parentSpanId; this.hasRemoteParent = hasRemoteParent; this.Name = name; this.traceParams = traceParams ?? throw new ArgumentNullException(nameof(traceParams)); this.startEndHandler = startEndHandler; this.hasBeenEnded = false; this.sampleToLocalSpanStore = false; if (options.HasFlag(SpanOptions.RecordEvents)) { if (timestampConverter == null) { this.timestampConverter = Timer.StartNew(); this.startTime = this.timestampConverter.StartTime; } else { this.timestampConverter = timestampConverter; this.startTime = this.timestampConverter.Now; } } else { this.startTime = DateTimeOffset.MinValue; this.timestampConverter = timestampConverter; } }
public abstract void UpdateActiveTraceParams(ITraceParams traceParams);
public override void UpdateActiveTraceParams(ITraceParams traceParams) { _activeTraceParams = traceParams; }
public static object Run(string zipkinUri) { // 1. Configure exporter to export traces to Zipkin var exporter = new ZipkinTraceExporter( new ZipkinTraceExporterOptions() { Endpoint = new Uri(zipkinUri), ServiceName = "trace-A", }, Tracing.ExportComponent); exporter.Start(); // 2. Configure 100% sample rate for the purposes of the demo ITraceConfig traceConfig = Tracing.TraceConfig; ITraceParams currentConfig = traceConfig.ActiveTraceParams; var newConfig = currentConfig.ToBuilder() .SetSampler(Samplers.AlwaysSample) .Build(); traceConfig.UpdateActiveTraceParams(newConfig); //Build application // Microsoft.Identity.Client var confidentialClientApplication = PublicClientApplicationBuilder .Create("efe9af1d-0c2a-499e-bc4c-1d23c22a8a87") .Build(); // Microsoft.Graph.auth DeviceCodeProvider authenticationProvider = new DeviceCodeProvider(confidentialClientApplication); var config = new GraphTelemetryConfiguration(); config.LatencyConfig = new LatencyConfig { authenticationLatencyIsEnabled = true, compressionHandlerLatencyIsEnabled = true, }; var client = new GraphServiceClient(authenticationProvider: authenticationProvider, config: config); using (var scope = Tracing.Tracer.SpanBuilder("'v1.0/me' full request latency ").StartScopedSpan()) { var user = client.Me.Request().GetAsync().GetAwaiter().GetResult(); Console.WriteLine("Succesfully captured full latency of /me"); } //using (var scope = Tracing.Tracer.SpanBuilder("v1.0/me/Drive/Root/Children ").StartScopedSpan()) //{ // var driveItemsStream = client.Me.Drive.Items["01ZJ3LUMVJPVXOFF43LVA23PKLT3KRFL54"].Content.Request().GetAsync().GetAwaiter().GetResult(); //} Tracing.ExportComponent.SpanExporter.Dispose(); return(null); }
private ISpan StartSpanInternal( ISpanContext parent, bool hasRemoteParent, string name, ISampler sampler, IList <ISpan> parentLinks, bool recordEvents, ITimestampConverter timestampConverter) { ITraceParams activeTraceParams = Options.TraceConfig.ActiveTraceParams; IRandomGenerator random = Options.RandomHandler; ITraceId traceId; ISpanId spanId = SpanId.GenerateRandomId(random); ISpanId parentSpanId = null; TraceOptionsBuilder traceOptionsBuilder; if (parent == null || !parent.IsValid) { // New root span. traceId = TraceId.GenerateRandomId(random); traceOptionsBuilder = TraceOptions.Builder(); // This is a root span so no remote or local parent. //hasRemoteParent = null; hasRemoteParent = false; } else { // New child span. traceId = parent.TraceId; parentSpanId = parent.SpanId; traceOptionsBuilder = TraceOptions.Builder(parent.TraceOptions); } traceOptionsBuilder.SetIsSampled( MakeSamplingDecision( parent, hasRemoteParent, name, sampler, parentLinks, traceId, spanId, activeTraceParams)); TraceOptions traceOptions = traceOptionsBuilder.Build(); SpanOptions spanOptions = SpanOptions.NONE; if (traceOptions.IsSampled || recordEvents) { spanOptions = SpanOptions.RECORD_EVENTS; } ISpan span = Span.StartSpan( SpanContext.Create(traceId, spanId, traceOptions), spanOptions, name, parentSpanId, hasRemoteParent, activeTraceParams, Options.StartEndHandler, timestampConverter, Options.Clock); LinkSpans(span, parentLinks); return(span); }
public TraceConfig() { this.activeTraceParams = TraceParams.Default; }