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}].");
                }
            }
        }
Example #2
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}].");
                }
            }
        }
Example #3
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}].");
            }
        }
Example #4
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);
        }