public MotorMetricsFactory(
        IOptions <PrometheusOptions> options,
        IApplicationNameService nameService
        )
    {
        var prometheusConfig = options.Value ?? throw new ArgumentNullException(nameof(options), "Prometheus config doesn't exist.");

        _collectorRegistry = prometheusConfig.CollectorRegistryInstance ?? Prometheus.Client.Metrics.DefaultCollectorRegistry;

        _collectorRegistry.GetOrAdd(
            ThreadPoolGauge.ThreadPoolGaugeConfig,
            _ => new ThreadPoolGauge()
            );
        _metricFactory = new MetricFactory(_collectorRegistry);

        var counter = _metricFactory.CreateCounter(
            "motor_extensions_hosting_build_info",
            "A metric with a constant '1' value labeled by version, libversion, and framework from which the service was built.",
            "version", "libversion", "framework"
            );

        counter.WithLabels(
            nameService.GetVersion(),
            nameService.GetLibVersion(),
            RuntimeInformation.FrameworkDescription
            ).Inc();
    }
    public static MotorCloudEvent <byte[]> ToMotorCloudEvent(this Message <string?, byte[]> message,
                                                             IApplicationNameService applicationNameService, CloudEventFormatter cloudEventFormatter)
    {
        if (!message.IsCloudEvent())
        {
            return(new MotorCloudEvent <byte[]>(applicationNameService, message.Value, new Uri("kafka://notset")));
        }

        var cloudEvent = message.ToCloudEvent(cloudEventFormatter);

        if (cloudEvent.Data is null)
        {
            throw new ArgumentException("Data property of CloudEvent is null");
        }
        if (cloudEvent.Source is null)
        {
            throw new ArgumentException("Source property of CloudEvent is null");
        }
        var motorCloudEvent = new MotorCloudEvent <byte[]>(applicationNameService, (byte[])cloudEvent.Data,
                                                           cloudEvent.Type, cloudEvent.Source, cloudEvent.Id, cloudEvent.Time, cloudEvent.DataContentType);

        foreach (var(key, value) in cloudEvent.GetPopulatedAttributes())
        {
            if (motorCloudEvent.GetAttribute(key.Name) is null)
            {
                motorCloudEvent[key] = value;
            }
        }

        return(motorCloudEvent);
    }
Ejemplo n.º 3
0
    public MotorCloudEvent(
        IApplicationNameService applicationNameService,
        TData data,
        string?type,
        Uri source,
        string?id,
        DateTimeOffset?time,
        string?contentType,
        params KeyValuePair <CloudEventAttribute, object>[] extensions)
    {
        BaseEvent = new CloudEvent(CloudEventsSpecVersion.Default);
        foreach (var(key, value) in extensions)
        {
            BaseEvent[key] = value;
        }

        BaseEvent.Id              = id ?? Guid.NewGuid().ToString();
        BaseEvent.Type            = type ?? typeof(TData).Name;
        BaseEvent.Source          = source;
        BaseEvent.Time            = time ?? DateTimeOffset.UtcNow;
        BaseEvent.DataContentType = contentType ?? new ContentType().ToString();

        _applicationNameService = applicationNameService;
        TypedData = data;
    }
Ejemplo n.º 4
0
    public static MotorCloudEvent <byte[]> ToMotorCloudEvent(this string message,
                                                             IApplicationNameService applicationNameService)
    {
        var motorCloudEvent = new MotorCloudEvent <byte[]>(applicationNameService,
                                                           Encoding.UTF8.GetBytes(message), new Uri("sqs://notset"));

        return(motorCloudEvent);
    }
    public static MotorCloudEvent <byte[]> ToMotorCloudEvent(this byte[] message,
                                                             IApplicationNameService applicationNameService)
    {
        var motorCloudEvent = new MotorCloudEvent <byte[]>(applicationNameService,
                                                           message, new Uri("nats://notset"));

        return(motorCloudEvent);
    }
Ejemplo n.º 6
0
 public SQSConsumer(IOptions <SQSClientOptions> options, ILogger <SQSConsumer <TData> > logger,
                    IApplicationNameService applicationNameService, ISQSClientFactory sqsClientFactory)
 {
     _options = options.Value;
     _logger  = logger;
     _applicationNameService = applicationNameService;
     _amazonSqsClient        = sqsClientFactory.From(_options);
 }
Ejemplo n.º 7
0
 public MotorCloudEvent(
     IApplicationNameService applicationNameService,
     TData data,
     Uri source,
     params KeyValuePair <CloudEventAttribute, object>[] extensions) : this(applicationNameService, data, null,
                                                                            source, null, null, null, extensions)
 {
 }
Ejemplo n.º 8
0
 public Timer(IOptions <TimerOptions> config,
              IBackgroundTaskQueue <MotorCloudEvent <IJobExecutionContext> > queue,
              IApplicationNameService applicationNameService)
 {
     _queue = queue;
     _applicationNameService = applicationNameService;
     _options = config.Value ?? throw new ArgumentNullException(nameof(config));
 }
 public NATSMessageConsumer(IOptions <NATSConsumerOptions> options, ILogger <NATSMessageConsumer <TData> > logger,
                            IApplicationNameService applicationNameService, INATSClientFactory natsClientFactory)
 {
     _options = options.Value;
     _logger  = logger;
     _applicationNameService = applicationNameService;
     _client = natsClientFactory.From(_options);
 }
Ejemplo n.º 10
0
 public MotorCloudEvent(
     IApplicationNameService applicationNameService,
     TData data,
     IEnumerable <ICloudEventExtension> extensions)
     : base(CloudEventsSpecVersion.Default, extensions)
 {
     _applicationNameService = applicationNameService;
     TypedData = data;
 }
Ejemplo n.º 11
0
 private MotorCloudEvent(
     IApplicationNameService applicationNameService,
     TData data,
     Uri source,
     string?id     = null,
     DateTime?time = null,
     params ICloudEventExtension[] extensions)
     : this(applicationNameService, data, typeof(TData).Name, source, id, time, extensions)
 {
 }
Ejemplo n.º 12
0
    public static MotorCloudEvent <byte[]> ExtractCloudEvent(this BasicDeliverEventArgs self,
                                                             IApplicationNameService applicationNameService, ReadOnlyMemory <byte> body, bool extractBindingKey)
    {
        var cloudEvent = self.BasicProperties.ExtractCloudEvent(applicationNameService, body);

        if (extractBindingKey)
        {
            cloudEvent.SetRabbitMQBinding(self.Exchange, self.RoutingKey);
        }

        return(cloudEvent);
    }
Ejemplo n.º 13
0
 public RabbitMQMessageConsumer(ILogger <RabbitMQMessageConsumer <T> > logger,
                                IRabbitMQConnectionFactory connectionFactory, IOptions <RabbitMQConsumerConfig <T> > config,
                                IHostApplicationLifetime applicationLifetime, IApplicationNameService applicationNameService,
                                ICloudEventFormatter cloudEventFormatter)
 {
     _logger                 = logger;
     _connectionFactory      = connectionFactory;
     _config                 = config.Value;
     _applicationLifetime    = applicationLifetime;
     _applicationNameService = applicationNameService;
     _cloudEventFormatter    = cloudEventFormatter;
 }
 public RabbitMQMessageConsumer(ILogger <RabbitMQMessageConsumer <T> > logger,
                                IRabbitMQConnectionFactory <T> connectionFactory,
                                IOptions <RabbitMQConsumerOptions <T> > config,
                                IHostApplicationLifetime applicationLifetime,
                                IApplicationNameService applicationNameService)
 {
     _logger                 = logger;
     ConnectionFactory       = connectionFactory;
     _options                = config.Value;
     _applicationLifetime    = applicationLifetime;
     _applicationNameService = applicationNameService;
 }
Ejemplo n.º 15
0
 public MotorCloudEvent(
     IApplicationNameService applicationNameService,
     TData data,
     string type,
     Uri source,
     string?id     = null,
     DateTime?time = null,
     params ICloudEventExtension[] extensions)
     : base(CloudEventsSpecVersion.Default, type, source, id, time, extensions)
 {
     _applicationNameService = applicationNameService;
     TypedData       = data;
     DataContentType = new ContentType();
 }
Ejemplo n.º 16
0
 public KafkaMessageConsumer(ILogger <KafkaMessageConsumer <TData> > logger,
                             IOptions <KafkaConsumerConfig <TData> > config,
                             IMetricsFactory <KafkaMessageConsumer <TData> >?metricsFactory,
                             IApplicationNameService applicationNameService,
                             ICloudEventFormatter cloudEventFormatter)
 {
     _logger = logger ?? throw new ArgumentNullException(nameof(logger));
     _applicationNameService = applicationNameService ?? throw new ArgumentNullException(nameof(config));
     _cloudEventFormatter    = cloudEventFormatter;
     _config             = config?.Value ?? throw new ArgumentNullException(nameof(config));
     _consumerLagSummary = metricsFactory?.CreateSummary("consumer_lag_distribution",
                                                         "Contains a summary of current consumer lag of each partition", new [] { "topic", "partition" });
     _consumerLagGauge = metricsFactory?.CreateGauge("consumer_lag",
                                                     "Contains current number consumer lag of each partition", false, "topic", "partition");
 }
        public static MotorCloudEvent <byte[]> ExtractCloudEvent <T>(this IBasicProperties self,
                                                                     IApplicationNameService applicationNameService, ICloudEventFormatter cloudEventFormatter,
                                                                     ReadOnlyMemory <byte> body,
                                                                     IReadOnlyCollection <ICloudEventExtension> extensions)
        {
            var specVersion = CloudEventsSpecVersion.V1_0;
            var attributes  = new Dictionary <string, object>();
            IDictionary <string, object> headers = new Dictionary <string, object>();

            if (self.IsHeadersPresent() && self.Headers != null)
            {
                headers = self.Headers;
            }

            foreach (var header in headers
                     .Where(t => t.Key.StartsWith(CloudEventPrefix))
                     .Select(t =>
                             new KeyValuePair <string, object>(
                                 t.Key.Substring(CloudEventPrefix.Length),
                                 t.Value)))
            {
                if (string.Equals(header.Key, CloudEventAttributes.DataContentTypeAttributeName(specVersion),
                                  StringComparison.InvariantCultureIgnoreCase) ||
                    string.Equals(header.Key, CloudEventAttributes.SpecVersionAttributeName(specVersion),
                                  StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                attributes.Add(header.Key, header.Value);
            }

            if (attributes.Count == 0)
            {
                return(new MotorCloudEvent <byte[]>(applicationNameService, body.ToArray(), typeof(T).Name,
                                                    new Uri("rabbitmq://notset"), extensions: extensions.ToArray()));
            }

            var cloudEvent = new MotorCloudEvent <byte[]>(applicationNameService, body.ToArray(), extensions);

            foreach (var attribute in attributes)
            {
                cloudEvent.GetAttributes().Add(attribute.Key, cloudEventFormatter.DecodeAttribute(
                                                   cloudEvent.SpecVersion, attribute.Key, (byte[])attribute.Value, extensions));
            }

            return(cloudEvent);
        }
 private static void AddExporter(this TracerProviderBuilder builder, ILogger logger,
                                 JaegerExporterOptions options, IApplicationNameService applicationNameService,
                                 HostBuilderContext hostContext)
 {
     try
     {
         Dns.GetHostEntry(options.AgentHost);
         builder.AddJaegerExporter(internalOptions =>
         {
             var dictionary = options.ProcessTags.ToDictionary(pair => pair.Key, pair => pair.Value);
             dictionary.Add(AttributeMotorProduct, applicationNameService.GetProduct());
             dictionary.Add(AttributeMotorEnvironment, hostContext.HostingEnvironment.EnvironmentName.ToLower());
             internalOptions.ProcessTags = dictionary.ToList();
             internalOptions.AgentHost   = options.AgentHost;
             internalOptions.AgentPort   = options.AgentPort;
         });
     }
     catch (Exception ex)
     {
         logger.LogWarning(LogEvents.JaegerConfigurationFailed, ex, "Jaeger configuration failed, fallback to console.");
         builder.AddConsoleExporter();
     }
 }
        public static MotorCloudEvent <byte[]> ToMotorCloudEvent <T>(this ConsumeResult <string, byte[]> message,
                                                                     IApplicationNameService applicationNameService, ICloudEventFormatter cloudEventFormatter)
        {
            if (!message.Message.IsCloudEvent())
            {
                return(new MotorCloudEvent <byte[]>(applicationNameService, message.Message.Value, typeof(T).Name,
                                                    new Uri("kafka://notset")));
            }
            var cloudEvent      = message.Message.ToCloudEvent(cloudEventFormatter);
            var motorCloudEvent = new MotorCloudEvent <byte[]>(applicationNameService, (byte[])cloudEvent.Data,
                                                               cloudEvent.Type, cloudEvent.Source, cloudEvent.Id, cloudEvent.Time);
            var newAttributes = motorCloudEvent.GetAttributes();

            foreach (var(key, value) in cloudEvent.GetAttributes())
            {
                if (!newAttributes.ContainsKey(key))
                {
                    newAttributes.Add(key, value);
                }
            }

            return(motorCloudEvent);
        }