0

我在 kubernetes 中部署了两项服务(裸机k0s。要对 Jaeger 服务 A 进行一些跟踪,请调用服务 B 以获取 JWT 身份验证令牌 - 当我在 Visual Studio 中进行测试时,这非常有效(服务 A 能够检索令牌来自服务 B)。

但是,当我已经部署到 k0S 后在 Postman 中执行调用时,我既看不到服务也看不到任何痕迹 Jaeger UI: 杰格UI

使用此处的指南,我已将服务配置如下:

服务 A (Startup.cs)

public class Startup
{
    private readonly IWebHostEnvironment _environment;
    public Startup(IConfiguration configuration, IWebHostEnvironment environment)
    {
        Configuration = configuration;
        _environment = environment;
    }
    public IConfiguration Configuration { get; }
   
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddDbContext<ModelContext>(o => o.UseOracle(Configuration.GetConnectionString("OracleConn")));
        services.AddCors(c =>
        {
            c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());
        });
        services.AddTransient<IAsyncAccountService<SttmCustAccount>, AccountService>();
        services.AddTransient<IAsyncLookupResponse, LookupService>();
        services.AddOpenTracing();
        services.AddSingleton<ITracer>(cli =>
        {
            var serviceName = cli.GetRequiredService<IWebHostEnvironment>().ApplicationName;
            Environment.SetEnvironmentVariable("JAEGER_SERVICE_NAME", serviceName);

            if (_environment.IsDevelopment())
            {
                Environment.SetEnvironmentVariable("JAEGER_AGENT_HOST", "localhost");
                Environment.SetEnvironmentVariable("JAEGER_AGENT_PORT", "6831");
                Environment.SetEnvironmentVariable("JAEGER_SAMPLER_TYPE", "const");
            }        
            ILoggerFactory loggerFactory = cli.GetRequiredService<ILoggerFactory>();

            ISampler sampler = new ConstSampler(sample: true);

            ITracer tracer = new Tracer.Builder(serviceName)
                .WithLoggerFactory(loggerFactory)
                .WithSampler(sampler)
                .Build();

            GlobalTracer.Register(tracer);
            return tracer;
        });           
      
    }

 
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

服务 B 具有完全相同的配置。

Jaeger 操作员是使用带有配置的 NodePort 设置的,如下文件所示:

[文件][https://drive.google.com/file/d/1nAjQ2jHrLSZLWoV9vGb3l4b0M1_yTMTw/view?usp=sharing]

我必须尝试将操作员公开为 Traefik Ingress,但失败了,不得不更改为 NodePort(如果有的话,这会影响服务的注册吗?):

这是服务 A 中从服务 B 检索令牌的调用:

    [HttpGet]
        public async Task<IActionResult> GetAccountAsync([FromQuery] AccountLookupRequest request)
        {         
             string authenticationResponse = await GetTokenAsync();           
            if (authenticationResponse != null)
            {
                _logger.LogInformation("Called AccountLookup API");               
                List<AccountLookupResponse> res = new List<AccountLookupResponse>();
                var tranRef = String.Format("{1}{2}{0}", Helpers.aa, Helpers.bb, DateTime.Now.ToString("yyMMddHHmmss"));

...
}
}



 static  async Task<string> GetTokenAsync()
    {           
        var payload = "{\"Username\": \"golide\",\"Password\": \"It@XXXXX\"}";
        Uri uri = new Uri("http://10.XXX.XXX.XXX:31306/users/authenticate");
        HttpContent c = new StringContent(payload, Encoding.UTF8, "application/json");
        AuthenticationResponse responseEntity = new AuthenticationResponse();        
        var response = string.Empty;
        using (var client = new HttpClient())
        {
            HttpResponseMessage result = await client.PostAsync(uri, c);
            if (result.IsSuccessStatusCode)
            {
                response = result.StatusCode.ToString();
               // responseEntity = JsonConvert.DeserializeObject<AuthenticationResponse>(response);
            }
        }
        return response;
    }

在 Postman 上下文中,对http://10.170.XXX.XXX:30488/account?field4=3025202645050&field7=GENERIC01&field10=ABC076的调用将隐式调用http://10.170.xxx.xxx:31306/users/authenticate处的 AuthenticationAPI

更新 容器日志显示正在生成 trace-id 和 span-id :

[02:54:03 INF] Executed action method 

FlexToEcocash.Controllers.AccountController.GetAccountAsync (FlexToEcocash), returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 416.2876ms.
[02:54:03 WRN] Event-Exception: Microsoft.AspNetCore.Mvc.BeforeActionResult
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Object.GetType()
   at OpenTracing.Contrib.NetCore.AspNetCore.MvcEventProcessor.ProcessEvent(String eventName, Object arg)
   at OpenTracing.Contrib.NetCore.AspNetCore.AspNetCoreDiagnostics.OnNext(String eventName, Object untypedArg)
   at OpenTracing.Contrib.NetCore.Internal.DiagnosticListenerObserver.System.IObserver<System.Collections.Generic.KeyValuePair<System.String,System.Object>>.OnNext(KeyValuePair`2 value)
[02:54:03 INF] Executing ObjectResult, writing value of type 'FlexToEcocash.Models.ResponseModels.AccountLookupResponse'.
[02:54:03 INF] Executed action FlexToEcocash.Controllers.AccountController.GetAccountAsync (FlexToEcocash) in 421.8383ms
[02:54:03 WRN] Span has already been finished; will not be reported again. Operation: HTTP GET Trace Id: c976e25bf21bfae5 Span Id: c976e25bf21bfae5
[02:54:03 INF] Executed endpoint 'FlexToEcocash.Controllers.AccountController.GetAccountAsync (FlexToEcocash)'
[02:54:03 INF] Request finished in 426.0567ms 200 application/json; charset=utf-8
[02:54:03 WRN] Span has already been finished; will not be reported again. Operation: HTTP GET Trace Id: c976e25bf21bfae5 Span Id: c976e25bf21bfae5

我错过了什么?

4

0 回答 0