Ejemplo n.º 1
0
        private async Task RegisterApplicationInstance(CancellationToken cancellationToken)
        {
            if (RuntimeEnvironment.ServiceId.HasValue && !RuntimeEnvironment.ServiceInstanceId.HasValue)
            {
                var osInfoRequest = new AgentOsInfoRequest
                {
                    HostName  = DnsHelpers.GetHostName(),
                    IpAddress = DnsHelpers.GetIpV4s(),
                    OsName    = PlatformInformation.GetOSName(),
                    ProcessNo = Process.GetCurrentProcess().Id
                };
                var value = await Polling(3,
                                          () => skyApmClient.RegisterApplicationInstanceAsync(RuntimeEnvironment.ServiceId.Value,
                                                                                              RuntimeEnvironment.InstanceId,
                                                                                              DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), osInfoRequest, cancellationToken),
                                          cancellationToken);

                if (value.HasValue && RuntimeEnvironment is RuntimeEnvironment environment)
                {
                    environment.ServiceInstanceId = value;
                    Logger.LogInformation(
                        $"Registered Application Instance[Id={environment.ServiceInstanceId.Value}].");
                }
            }
        }
Ejemplo n.º 2
0
        private async Task ReportServiceInstancePropertiesAsync(CancellationToken cancellationToken)
        {
            var properties = new AgentOsInfoRequest
            {
                HostName  = DnsHelpers.GetHostName(),
                IpAddress = DnsHelpers.GetIpV4s(),
                OsName    = PlatformInformation.GetOSName(),
                ProcessNo = Process.GetCurrentProcess().Id,
                Language  = "dotnet"
            };
            var request = new ServiceInstancePropertiesRequest
            {
                ServiceId         = _config.ServiceName,
                ServiceInstanceId = _config.ServiceInstanceName,
                Properties        = properties
            };
            var result = await Polling(3,
                                       () => _serviceRegister.ReportInstancePropertiesAsync(request, cancellationToken),
                                       cancellationToken);

            if (result && RuntimeEnvironment is RuntimeEnvironment environment)
            {
                environment.Initialized = true;
                Logger.Information($"Reported Service Instance Properties[Service={request.ServiceId},InstanceId={request.ServiceInstanceId}].");
            }
        }
Ejemplo n.º 3
0
        private async Task RegisterServiceInstanceAsync(CancellationToken cancellationToken)
        {
            if (RuntimeEnvironment.ServiceId.HasValue && !RuntimeEnvironment.ServiceInstanceId.HasValue)
            {
                var properties = new AgentOsInfoRequest
                {
                    HostName = DnsHelpers.GetHostName(),
                    IpAddress = DnsHelpers.GetIpV4s(),
                    OsName = PlatformInformation.GetOSName(),
                    ProcessNo = Process.GetCurrentProcess().Id,
                    Language = "dotnet"
                };
                var request = new ServiceInstanceRequest
                {
                    ServiceId = RuntimeEnvironment.ServiceId.Value,
                    InstanceUUID = RuntimeEnvironment.InstanceId.ToString("N"),
                    Properties = properties
                };
                var value = await Polling(3,
                    () => _serviceRegister.RegisterServiceInstanceAsync(request, cancellationToken),
                    cancellationToken);

                if (value.HasValue && RuntimeEnvironment is RuntimeEnvironment)
                {
                    var environment = (RuntimeEnvironment)RuntimeEnvironment;

                    environment.ServiceInstanceId = value;
                    Logger.Information($"Registered ServiceInstance[Id={environment.ServiceInstanceId.Value}].");
                }
            }
        }
Ejemplo n.º 4
0
        public async Task <NullableValue> RegisterApplicationInstanceAsync(int applicationId, Guid agentUUID,
                                                                           long registerTime, AgentOsInfoRequest osInfoRequest,
                                                                           CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!_connectionManager.Ready)
            {
                return(NullableValue.Null);
            }

            var connection = _connectionManager.GetConnection();

            var client = new InstanceDiscoveryService.InstanceDiscoveryServiceClient(connection);

            var applicationInstance = new ApplicationInstance
            {
                ApplicationId = applicationId,
                AgentUUID     = agentUUID.ToString("N"),
                RegisterTime  = registerTime,
                Osinfo        = new OSInfo
                {
                    OsName    = osInfoRequest.OsName,
                    Hostname  = osInfoRequest.HostName,
                    ProcessNo = osInfoRequest.ProcessNo
                }
            };

            applicationInstance.Osinfo.Ipv4S.AddRange(osInfoRequest.IpAddress);

            return(await new Call(_logger, _connectionManager).Execute(async() =>
            {
                var applicationInstanceMapping = await client.registerInstanceAsync(applicationInstance, null,
                                                                                    _config.GetTimeout(), cancellationToken);
                return new NullableValue(applicationInstanceMapping?.ApplicationInstanceId ?? 0);
            },
                                                                       () => NullableValue.Null,
                                                                       () => ExceptionHelpers.RegisterApplicationInstanceError));
        }
Ejemplo n.º 5
0
        private int instance(Channel connection, string instanceUUID, int serviceId)
        {
            var serviceInstanceId = 0;

            var properties = new AgentOsInfoRequest
            {
                HostName  = DnsHelpers.GetHostName(),
                IpAddress = DnsHelpers.GetIpV4s(),
                OsName    = PlatformInformation.GetOSName(),
                ProcessNo = Process.GetCurrentProcess().Id,
                Language  = "dotnet"
            };
            var request = new ServiceInstanceRequest
            {
                ServiceId    = serviceId,
                InstanceUUID = instanceUUID,
                Properties   = properties
            };

            var client   = new Register.RegisterClient(connection);
            var instance = new ServiceInstance
            {
                ServiceId    = request.ServiceId,
                InstanceUUID = request.InstanceUUID,
                Time         = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            };

            instance.Properties.Add(new KeyStringValuePair
            {
                Key = OS_NAME, Value = request.Properties.OsName
            });
            instance.Properties.Add(new KeyStringValuePair
            {
                Key = HOST_NAME, Value = request.Properties.HostName
            });
            instance.Properties.Add(new KeyStringValuePair
            {
                Key = PROCESS_NO, Value = request.Properties.ProcessNo.ToString()
            });
            instance.Properties.Add(new KeyStringValuePair
            {
                Key = LANGUAGE, Value = request.Properties.Language
            });
            foreach (var ip in request.Properties.IpAddress)
            {
                instance.Properties.Add(new KeyStringValuePair {
                    Key = IPV4, Value = ip
                });
            }

            var serviceInstances = new ServiceInstances();

            serviceInstances.Instances.Add(instance);

            try
            {
                var value = Task.Run(async() =>
                {
                    return(await Polly.Polling(3,
                                               () => client.doServiceInstanceRegisterAsync(serviceInstances)));
                }).Result;

                //var reply = Task.Run(async () =>
                //{
                //    return await client.doServiceInstanceRegisterAsync(serviceInstances);
                //}).Result;
                //foreach (var serviceInstance in reply.ServiceInstances)
                //{
                //    serviceInstanceId = serviceInstance.Value;
                //}

                serviceInstanceId = value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(serviceInstanceId);
        }