/// <summary> /// This method writes the incoming event data to the data collectors. /// </summary> /// <param name="eventData">The event data.</param> /// <param name="support">The event data type.</param> /// <param name="sync">Specifies whether the data should be written out immediately.</param> /// <param name="claims">The optional claims of the calling party. If not set explicitly, then this /// will be populated from the current thread. If you don't want this then pass an empty claims object.</param> public void Write(EventBase eventData, DataCollectionSupport support, bool sync = false, ClaimsPrincipal claims = null) { if (eventData == null) { throw new ArgumentNullException("eventData", "eventData cannot be null for Write"); } if (!Active) { throw new ServiceNotStartedException(); } //Create the event holder and set the identity based on the claims passed or if null, //picked up from the current thread. var item = new EventHolder(support, claims ?? Thread.CurrentPrincipal as ClaimsPrincipal) { Data = eventData , Sync = sync , Timestamp = StatisticsInternal.ActiveIncrement() }; //Do we have to write this straight away, or can we push it on to an async thread. if (item.Sync) { //Process the item immediately. ProcessItem(item); } else { mQueue.Enqueue(item); //Signal to the logging thread that there is more data waiting. mReset.Set(); } }
/// <summary> /// This method writes the events to the queue. /// </summary> /// <param name="eventData">The event data.</param> /// <param name="support">The collection support type.</param> /// <param name="sync">The sync flag. Ignored for this usage.</param> /// <param name="claims">The current claims.</param> public void Write(EventBase eventData, DataCollectionSupport support, bool sync = false, ClaimsPrincipal claims = null) { Events.Enqueue(new EventHolder(support, claims) { Data = eventData, Sync = sync, Timestamp = Environment.TickCount }); }
/// <summary> /// This is the default constructor. /// </summary> /// <param name="support">The collection support type.</param> /// <param name="serializerBinary">Serializer used to serialize the event object</param> /// <param name="entityPath">Function to determine the entity path that will be used when sending messages to event hub</param> /// <param name="isSupported">Function to determine whether the event is supported</param> public EventHubDataCollectorOptions(DataCollectionSupport support , Func <EventHolder, MicroserviceId, BinaryContainer> serializerBinary = null , Func <EventHolder, MicroserviceId, string> entityPath = null , Func <EventHolder, bool> isSupported = null) { Support = support; SerializerBinary = serializerBinary ?? BinaryContainerHelper.DefaultJsonBinarySerializer; EntityPath = entityPath ?? ((ev, ms) => $"{ms.Name}_{support.ToString()}"); IsSupported = isSupported ?? (ev => true); }
/// <summary> /// Write event source /// </summary> /// <param name="support"></param> /// <param name="eventHolder"></param> protected virtual void WriteEvent(DataCollectionSupport support, EventHolder eventHolder) { var options = mPolicy.Options.FirstOrDefault(o => o.Support == support); if (options == null || !options.IsSupported(eventHolder)) { return; } int start = StatisticsInternal.ActiveIncrement(options.Support); Guid?traceId = options.ShouldProfile ? (ProfileStart($"AzureEventHub{options.Support}_{eventHolder.Data.TraceId}")) : default(Guid?); // Check we have en event hub client mEventHubClient = mEventHubClient ?? CreateEventHubClient(); var result = ResourceRequestResult.Unknown; try { //Serialize the blob. var blob = options.SerializerBinary(eventHolder, OriginatorId).Blob; //Encrypt the blob is that is the policy. if (options.EncryptionPolicy != AzureStorageEncryption.None && mEncryption != null) { blob = ServiceHandlers.Encryption[mEncryption].Encrypt(blob); } mEventHubClient.SendAsync(new EventData(blob)).Wait(); result = ResourceRequestResult.Success; } catch (Exception) { result = ResourceRequestResult.Exception; StatisticsInternal.ErrorIncrement(options.Support); throw; } finally { StatisticsInternal.ActiveDecrement(options.Support, start); if (traceId.HasValue) { ProfileEnd(traceId.Value, start, result); } } }
/// <summary> /// This is the default constructor. /// </summary> /// <param name="support">The collection support type.</param> /// <param name="behavior">The default storage behaviour.</param> public AzureStorageDataCollectorOptions(DataCollectionSupport support , AzureStorageBehaviour behavior = AzureStorageBehaviour.None , Func <EventHolder, MicroserviceId, ITableEntity> serializerTable = null , Func <EventHolder, MicroserviceId, AzureStorageBinary> serializerBinary = null , Func <EventHolder, MicroserviceId, string> makeId = null , Func <EventHolder, MicroserviceId, string> binaryMakeId = null , Func <EventHolder, MicroserviceId, string> binaryMakeFolder = null , Func <AzureStorageBehaviour, EventHolder, bool> isSupported = null ) { Support = support; Behaviour = behavior; SerializerTable = serializerTable; SerializerBinary = serializerBinary ?? AzureStorageHelper.DefaultJsonBinarySerializer; MakeId = makeId ?? ((EventHolder e, MicroserviceId i) => e.Data.TraceId); BinaryMakeId = binaryMakeId ?? MakeId; BinaryMakeFolder = binaryMakeFolder; IsSupported = isSupported ?? ((b, e) => true); }
/// <summary> /// Output the data for the three option types. /// </summary> /// <param name="support">The storage options</param> /// <param name="e">The event object.</param> protected void WriteConnectors(DataCollectionSupport support, EventHolder e) { //Blob if (mHoldersBlob.ContainsKey(support) && mHoldersBlob[support].ShouldWrite(e)) { WriteConnector(mHoldersBlob[support], e).Wait(); } //Table if (mHoldersTable.ContainsKey(support) && mHoldersTable[support].ShouldWrite(e)) { WriteConnector(mHoldersTable[support], e).Wait(); } //Queue if (mHoldersQueue.ContainsKey(support) && mHoldersQueue[support].ShouldWrite(e)) { WriteConnector(mHoldersQueue[support], e).Wait(); } //File if (mHoldersFile.ContainsKey(support) && mHoldersFile[support].ShouldWrite(e)) { WriteConnector(mHoldersFile[support], e).Wait(); } }
/// <summary> /// This method configures the mapping. /// </summary> protected override void StartInternal() { var resourceTracker = SharedServices?.GetService <IResourceTracker>(); if (resourceTracker != null && mResourceProfile != null) { mResourceConsumer = resourceTracker.RegisterConsumer(GetType().Name, mResourceProfile); } mSupported = new Dictionary <DataCollectionSupport, Action <EventHolder> >(); SupportLoadDefault(); var support = mSupported.Select((k) => k.Key).Aggregate((a, b) => a | b); if (mSupportMapSubmitted.HasValue) { mSupportMapActual = support & mSupportMapSubmitted.Value; } else { mSupportMapActual = support; } }
/// <summary> /// This method is used to decrement the active count and submits the processing time. /// </summary> /// <param name="support">The specific collection type.</param> /// <param name="extent">The processing time in milliseconds.</param> public virtual int ActiveDecrement(DataCollectionSupport support, TimeSpan extent) { return(mStats[support].ActiveDecrement(extent)); }
/// <summary> /// This method adds support for the log event. /// </summary> /// <param name="eventType">The event type.</param> /// <param name="eventData">The event data.</param> protected virtual void SupportAdd(DataCollectionSupport eventType, Action <EventHolder> eventData) { mSupported[eventType] = eventData; }
/// <summary> /// Returns true if the requested type is supported. /// </summary> /// <param name="support">The data collection type</param> /// <returns></returns> public virtual bool IsSupported(DataCollectionSupport support) { return((mSupportMapActual & support) > 0); }
/// <summary> /// This method is used to increment the active and total record count. /// </summary> /// <param name="support">The specific collection type.</param> public virtual void ErrorIncrement(DataCollectionSupport support) { mStats[support].ErrorIncrement(); }
/// <summary> /// This constructor sets the data type for the event data holder. /// </summary> /// <param name="dataType">The data type.</param> /// <param name="claims">This </param> public EventHolder(DataCollectionSupport dataType, ClaimsPrincipal claims) { DataType = dataType; Claims = claims; }
/// <summary> /// This method is used to increment the active and total record count. /// </summary> /// <param name="support">The specific collection type.</param> public virtual int ActiveIncrement(DataCollectionSupport support) { return(mStats[support].ActiveIncrement()); }
/// <summary> /// This method is used by the extension method to write data to the collector. /// </summary> /// <param name="eventData">The event data.</param> /// <param name="support">The support type.</param> /// <param name="sync">The sync identifier. </param> /// <param name="claims">Any specific claims.</param> public void Write(EventBase eventData, DataCollectionSupport support, bool sync = false, ClaimsPrincipal claims = null) { ValidateServiceStarted(); mDataCollection.Write(eventData, support, sync, claims); }
/// <summary> /// Output the data for the three option types. /// </summary> /// <param name="support">The storage options</param> /// <param name="e">The event object.</param> protected void WriteConnectors(DataCollectionSupport support, EventHolder e) { List <Exception> exs = null; Action <Action> wrapper = (a) => { try { a(); } catch (Exception ex) { if (exs == null) { exs = new List <Exception>(); } exs.Add(ex); } }; //Blob wrapper(() => { if (mHoldersBlob.ContainsKey(support) && mHoldersBlob[support].ShouldWrite(e)) { WriteConnector(mHoldersBlob[support], e).Wait(); } }); //Table wrapper(() => { if (mHoldersTable.ContainsKey(support) && mHoldersTable[support].ShouldWrite(e)) { WriteConnector(mHoldersTable[support], e).Wait(); } }); //Queue wrapper(() => { if (mHoldersQueue.ContainsKey(support) && mHoldersQueue[support].ShouldWrite(e)) { WriteConnector(mHoldersQueue[support], e).Wait(); } }); //File wrapper(() => { if (mHoldersFile.ContainsKey(support) && mHoldersFile[support].ShouldWrite(e)) { WriteConnector(mHoldersFile[support], e).Wait(); } }); //If there were errors during execution, then throw an aggregrate exception. if (exs != null && exs.Count > 0) { throw new AzureDataCollectionAggregrateException("WriteConnectors failure.", exs); } }
/// <summary> /// This is the default constructor. /// </summary> /// <param name="support">The support options.</param> public AzureStorageDataCollectorEncryptionPolicyException(DataCollectionSupport support) { Support = support; }
/// <summary> /// This method is used to decrement the active count and submits the processing time. /// </summary> /// <param name="support">The specific collection type.</param> /// <param name="start">The processing time in milliseconds.</param> public virtual int ActiveDecrement(DataCollectionSupport support, int start) { return(mStats[support].ActiveDecrement(start)); }