public void TextMapPropagationRegistry_Inject_ThrowsWhenFormatIsNotSupported() { var carrier = Substitute.For <ITextMap>(); var ex = Assert.Throws <ArgumentException>(() => _propagationRegistry.Inject(_spanContext, _format, carrier)); Assert.Contains($"{_format.GetType().FullName} is not a supported injection format", ex.Message); }
public SpanContext Extract <TCarrier>(IFormat <TCarrier> format, TCarrier carrier, StringComparison comparison) { _logger.Trace($"Extracting {format.GetType()} from {carrier.GetType()}"); if (carrier is ITextMap text) { ulong?traceId = null; ulong?spanId = null; var baggage = new Baggage(); foreach (var entry in text) { if (Keys.TraceId.Equals(entry.Key, comparison)) { traceId = Convert.ToUInt64(entry.Value, 16); } else if (Keys.SpanId.Equals(entry.Key, comparison)) { spanId = Convert.ToUInt64(entry.Value, 16); } else if (entry.Key.StartsWith(Keys.BaggagePrefix, comparison)) { var key = entry.Key.Substring(Keys.BaggagePrefix.Length); baggage.Set(key, entry.Value); } } if (traceId.HasValue && spanId.HasValue) { _logger.Trace($"Existing trace/spanID found, returning SpanContext."); return(new SpanContext(traceId.Value, spanId.Value)); } } return(null); }
/// <inheritdoc /> public SpanContext Extract <TCarrier>(IFormat <TCarrier> format, TCarrier carrier) { _logger.Trace($"Extracting {format.GetType()} from {carrier.GetType()}"); string traceId = null; string spanId = null; var baggage = new Baggage(); if (carrier is ITextMap text) { foreach (var entry in text) { if (Keys.TraceId.Equals(entry.Key)) { traceId = Convert.ToUInt64(entry.Value, 16).ToString(); } else if (Keys.SpanId.Equals(entry.Key)) { spanId = Convert.ToUInt64(entry.Value, 16).ToString(); } else if (entry.Key.StartsWith(Keys.BaggagePrefix)) { var key = entry.Key.Substring(Keys.BaggagePrefix.Length); baggage.Set(key, entry.Value); } } } if (!string.IsNullOrEmpty(traceId) && !string.IsNullOrEmpty(spanId)) { _logger.Trace($"Existing trace/spanID found, returning SpanContext."); return(new SpanContext(traceId, spanId, baggage)); } return(null); }
public static string Format(this IFormat obj, string delimeter = ",") { var result = string.Empty; var objType = obj.GetType(); var properties = objType.GetProperties(); var props = new SortedDictionary <int, PropertyInfo>(); foreach (var property in properties) { var formatAttributes = property.GetCustomAttributes(typeof(FormatAttribute), false); if (formatAttributes.Length > 0) { var formatAttribute = (FormatAttribute)formatAttributes[0]; if (formatAttribute.IsVisible) { props.Add(formatAttribute.Order, property); } } else { props.Add(props.Count, property); } } foreach (var prop in props) { result += prop.Value.GetValue(obj, null) + delimeter; } return(result); }
/// <summary> /// Get the default (automatically discovered) configuration for a certain format. /// </summary> /// <param name="format">The format.</param> /// <returns>The default (auto discovered) configuration.</returns> public Configuration GetDefaultConfig(IFormat format) { Dictionary <Type, IPrimitiveSerializer> primitiveConfig = new Dictionary <Type, IPrimitiveSerializer>(); if (PrimitiveSerializers.ContainsKey(format.SerialDataType)) { foreach (IPrimitiveSerializer f in PrimitiveSerializers[format.SerialDataType]) { if (!primitiveConfig.ContainsKey(f.SourceType)) { primitiveConfig.Add(f.SourceType, (IPrimitiveSerializer)Activator.CreateInstance(f.GetType())); } } } else { Logger.Warn(String.Format( "No primitive serializers found for format {0} with serial data type {1}", format.GetType().AssemblyQualifiedName, format.SerialDataType.AssemblyQualifiedName)); } return(new Configuration( format, primitiveConfig.Values, CompositeSerializers.Where((d) => d.Priority > 0).Select(d => (ICompositeSerializer)Activator.CreateInstance(d.GetType())))); }
/// <inheritdoc /> public void Inject <TCarrier>(ISpanContext spanContext, IFormat <TCarrier> format, TCarrier carrier) { _propagator.Inject((SpanContext)spanContext, format, carrier); if (_options.EnableMetaEventLogging) { this.BuildSpan(LightStepConstants.MetaEvent.InjectOperation) .IgnoreActiveSpan() .WithTag(LightStepConstants.MetaEvent.MetaEventKey, true) .WithTag(LightStepConstants.MetaEvent.SpanIdKey, spanContext.SpanId) .WithTag(LightStepConstants.MetaEvent.TraceIdKey, spanContext.TraceId) .WithTag(LightStepConstants.MetaEvent.PropagationFormatKey, format.GetType().ToString()) .Start() .Finish(); } }
/// <inheritdoc /> public ISpanContext Extract <TCarrier>(IFormat <TCarrier> format, TCarrier carrier) { var ctx = _propagator.Extract(format, carrier); if (_options.EnableMetaEventLogging) { this.BuildSpan(LightStepConstants.MetaEvent.ExtractOperation) .IgnoreActiveSpan() .WithTag(LightStepConstants.MetaEvent.MetaEventKey, true) .WithTag(LightStepConstants.MetaEvent.SpanIdKey, ctx.SpanId) .WithTag(LightStepConstants.MetaEvent.TraceIdKey, ctx.TraceId) .WithTag(LightStepConstants.MetaEvent.PropagationFormatKey, format.GetType().ToString()) .Start() .Finish(); } return(ctx); }
public DataTable ExtractTable( IFormat format ) { PathSeriesFormat pathSeriesFormat = format as PathSeriesFormat; PathTableFormat pathTableFormat = format as PathTableFormat; if ( pathSeriesFormat != null ) { var htmlSettings = new HtmlExtractionSettings(); htmlSettings.ExtractLinkUrl = pathSeriesFormat.ExtractLinkUrl; var result = Content.ExtractTable( HtmlPath.Parse( pathSeriesFormat.Path ), pathSeriesFormat.ToExtractionSettings(), htmlSettings ); if ( !result.Success ) { throw new Exception( "Failed to extract table from document: " + result.FailureReason ); } return pathSeriesFormat.ToFormattedTable( result.Value ); } else if ( pathTableFormat != null ) { var result = Content.ExtractTable( HtmlPath.Parse( pathTableFormat.Path ), true ); if ( !result.Success ) { throw new Exception( "Failed to extract table from document: " + result.FailureReason ); } return pathTableFormat.ToFormattedTable( result.Value ); } else if ( format is PathSingleValueFormat ) { var f = (PathSingleValueFormat)format; var str = Content.GetTextByPath( HtmlPath.Parse( f.Path ) ); var value = f.ValueFormat.Convert( str ); // XXX: this is really ugly - i have to create a table just to satisfy the interface :( return CreateTableForScalar( f.ValueFormat.Type, value ); } else { throw new NotSupportedException( "Format not supported for Html documents: " + format.GetType() ); } }
/// <inheritdoc /> public void Inject <TCarrier>(SpanContext context, IFormat <TCarrier> format, TCarrier carrier) { _logger.Trace($"Injecting {context} of {format.GetType()} to {carrier.GetType()}"); if (carrier is ITextMap text) { foreach (var entry in context.GetBaggageItems()) { text.Set(Keys.BaggagePrefix + entry.Key, entry.Value); } text.Set(Keys.SpanId, context.SpanId); text.Set(Keys.TraceId, context.TraceId); text.Set(Keys.Sampled, "true"); } else { _logger.Warn($"Unknown carrier during inject."); throw new InvalidOperationException($"Unknown carrier {carrier.GetType()}"); } }
public DataTable ExtractTable( IFormat format ) { SeparatorSeriesFormat separatorSeriesFormat = format as SeparatorSeriesFormat; CsvFormat csvFormat = format as CsvFormat; if ( csvFormat != null ) { DataTable result = CsvReader.Read( Location, csvFormat.Separator ); return csvFormat.ToFormattedTable( result ); } else if ( separatorSeriesFormat != null ) { DataTable result = CsvReader.Read( Location, separatorSeriesFormat.Separator ); return separatorSeriesFormat.ToFormattedTable( result ); } else { throw new NotSupportedException( "Format not supported for text files: " + format.GetType() ); } }
public async Task PostProcess(string filename, IFormat format, CancellationToken token = default) { foreach (var pFunc in PostProcessors) { var pp = pFunc(format); Type generic = pp.GetType() .GetInterfaces() .Where( x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IPostProcessor <>)) .First().GenericTypeArguments[0]; if (generic.IsAssignableFrom(format.GetType())) { await(Task) pp.GetType().GetMethod("ProcessAsync").Invoke(pp, new object[] { format, filename, token }); } else { LogWarning($"PostProcesssor is incompatible with format {format.Id} because the format is not a {generic.Name}"); } } }
/// <inheritdoc /> public void Inject <TCarrier>(SpanContext context, IFormat <TCarrier> format, TCarrier carrier) { _logger.Trace($"Injecting {context} of {format.GetType()} to {carrier.GetType()}"); if (carrier is IBinary s) { var ctx = new BinaryCarrier { BasicCtx = new BasicTracerCarrier { SpanId = Convert.ToUInt64(context.SpanId), TraceId = Convert.ToUInt64(context.TraceId), Sampled = true } }; foreach (var item in context.GetBaggageItems()) { ctx.BasicCtx.BaggageItems.Add(item.Key, item.Value); } var ctxArray = ctx.ToByteArray(); var ctxStream = new MemoryStream(ctxArray); s.Set(ctxStream); } }
public void WriteOutputContent(MediaType mediaType, IFormat format) { if (mediaType == null) { throw new ArgumentNullException("mediaType", "mediaType cannot be null."); } if (format == null) { throw new ArgumentNullException("format", "format cannot be null."); } string contentType = format.ContentType(mediaType); if (contentType.Contains("*")) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, "Format {0} returned a Content-Type of {1} for the chosen media type of {2}. Writing a wildcard Content-Type to the response is not supported.", format.GetType(), contentType, mediaType)); } object resp = this.ResponseObject; this.httpResponse.ContentType = contentType; if (resp != null) { format.Serialize(mediaType, resp, this.httpResponse.OutputStream); } }
public void ApplyFormatToCell(IWorkbook wb, ICell cell, IFormat format) { if (format != null) { if (format is T concreteFormat) { ApplyFormatToCell(wb, cell, concreteFormat); } else { throw new ArgumentException($"Given Format is not of Type {typeof(T)}, actual {format.GetType()}"); } } }
public DataTable ExtractTable(IFormat format) { PathSeriesFormat pathSeriesFormat = format as PathSeriesFormat; PathTableFormat pathTableFormat = format as PathTableFormat; if (pathSeriesFormat != null) { var htmlSettings = new HtmlExtractionSettings(); htmlSettings.ExtractLinkUrl = pathSeriesFormat.ExtractLinkUrl; var result = Content.ExtractTable(HtmlPath.Parse(pathSeriesFormat.Path), pathSeriesFormat.ToExtractionSettings(), htmlSettings); if (!result.Success) { throw new Exception("Failed to extract table from document: " + result.FailureReason); } return(pathSeriesFormat.ToFormattedTable(result.Value)); } else if (pathTableFormat != null) { var result = Content.ExtractTable(HtmlPath.Parse(pathTableFormat.Path), true); if (!result.Success) { throw new Exception("Failed to extract table from document: " + result.FailureReason); } return(pathTableFormat.ToFormattedTable(result.Value)); } else if (format is PathSingleValueFormat) { var f = (PathSingleValueFormat)format; var str = Content.GetTextByPath(HtmlPath.Parse(f.Path)); var value = f.ValueFormat.Convert(str); // XXX: this is really ugly - i have to create a table just to satisfy the interface :( return(CreateTableForScalar(f.ValueFormat.Type, value)); } else { throw new NotSupportedException("Format not supported for Html documents: " + format.GetType()); } }
public DataTable ExtractTable(IFormat format) { SeparatorSeriesFormat separatorSeriesFormat = format as SeparatorSeriesFormat; CsvFormat csvFormat = format as CsvFormat; if (csvFormat != null) { DataTable result = CsvReader.Read(Location, csvFormat.Separator); return(csvFormat.ToFormattedTable(result)); } else if (separatorSeriesFormat != null) { DataTable result = CsvReader.Read(Location, separatorSeriesFormat.Separator); return(separatorSeriesFormat.ToFormattedTable(result)); } else { throw new NotSupportedException("Format not supported for text files: " + format.GetType()); } }
/// <summary> /// Indicates whether the current object is equal to another object of the same type. /// </summary> /// <param name="other">An object to compare with this object.</param> /// <returns>True if the current object is equal to the other parameter, otherwise false.</returns> public bool Equals(IFormat other) { if ((object)other != null) { return this.GetType().Equals(other.GetType()); } return false; }