Skip to content

SkyWalking 6.1 & SkyApm.Agent.AspNetCore

🏷️ SkyWalking

.NET Core 项目使用的是 SkyWalking.Agent.AspNetCore -Version 0.6.0 包来加载 SkyWalking 探针,但是在多线程中跟踪时会发生如下异常:

txt
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
    at SkyWalking.Diagnostics.SqlClient.SqlClientTracingDiagnosticProcessor.BeforeExecuteCommand(SqlCommand sqlCommand)

怀疑是跟官方的 issue #72 同样的问题。

问题应该是已经修复了,但是在准备更新包的时候发现 SkyWalking.Agent.AspNetCore 这个包已经找不到了。
看官方库应该是去年年底今年年初的时候更改了项目的名字,从 SkyWalking.Agent.AspNetCore 改成了 SkyApm.Agent.AspNetCore

换成 SkyApm.Agent.AspNetCore 包之后遇到两个问题:

  1. 之前 SkyWalking 的 TraceId 作为日志的一个字段记录的,但在新包中已经找不到这个属性了(SkyWalking.Context.ContextManager.GlobalTraceId)。

    这个暂时没法解决,只能不使用这个字段了。

  2. 使用 SkyApm.Agent.AspNetCore 启动时会报 Method not found: Register/doServiceRegister 的错误。

    这个问题应该是由于 SkyWalking 的版本太低导致的,升级到 6.1.0 就不会再报这个错了。

SkyWalking 6.1 & SkyApm.Agent.AspNetCore

记录一下 SkyWalking 6.1 + SkyApm.Agent.AspNetCore 的使用方法(windows 环境)。

  1. 安装 SkyAPM.Agent.AspNetCore 包(当前只有一个版本 0.8.0

    powershell
    Install-Package SkyAPM.Agent.AspNetCore -Version 0.8.0
  2. 下载 SkyWalking 6.1.0 => 下载页面

  3. 运行 apache-skywalking-apm-bin\bin 目录下的 startup.bat 文件。

    会启动两个窗口 Skywalking-CollectorSkywalking-Webapp

    Skywalking-Webapp 如果启动不起来,可以查看 logs 目录下的日志文件 webapp.log
    配置文件在 webapp 目录下的 webapp.yml 文件。其默认值如下:

    yaml
    # Licensed to the Apache Software Foundation (ASF) under one
    # or more contributor license agreements.  See the NOTICE file
    # distributed with this work for additional information
    # regarding copyright ownership.  The ASF licenses this file
    # to you under the Apache License, Version 2.0 (the
    # "License"); you may not use this file except in compliance
    # with the License.  You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    server:
      port: 8080
    
    collector:
      path: /graphql
      ribbon:
        ReadTimeout: 10000
        # Point to all backend's restHost:restPort, split by ,
        listOfServers: 127.0.0.1:12800
    
    security:
      user:
        # username
        admin:
        # password
        password: admin

    如果报 Failed to start connector [Connector[HTTP/1.1-8080]] 错误,说明端口被占用,修改 server.port 的值再启动就可以了。

  4. 创建 skyapm.json 配置文件

    安装 SkyAPM.DotNet.CLI

    bash
    dotnet tool install -g SkyAPM.DotNet.CLI

    使用 dotnet skyapm config [your_service_name] [your_servers] 创建配置文件.

    bash
    dotnet skyapm config sample_app 127.0.0.1:11800

    正确执行后会生成 skyapm.json 文件,其内容如下:

    json
    {
        "SkyWalking": {
            "ServiceName": "sample_app",
            "Namespace": "",
            "HeaderVersions": [
                "sw6"
            ],
            "Sampling": {
                "SamplePer3Secs": -1,
                "Percentage": -1.0
            },
            "Logging": {
                "Level": "Information",
                "FilePath": "logs\\skyapm-{Date}.log"
            },
            "Transport": {
                "Interval": 3000,
                "ProtocolVersion": "v6",
                "QueueSize": 30000,
                "BatchSize": 3000,
                "gRPC": {
                    "Servers": "127.0.0.1:11800",
                    "Timeout": 10000,
                    "ConnectTimeout": 10000,
                    "ReportTimeout": 600000
                }
            }
        }
    }

    需要注意的是项目中该文件的 复制到输出目录 属性需要设置为 如果较新则复制

  5. 工程的 launchSettings.json 文件中增加环境变量 SKYWALKING__SERVICENAMEASPNETCORE_HOSTINGSTARTUPASSEMBLIES

    json
    {
        "profiles": {
            "IIS Express": {
                "commandName": "IISExpress",
                "launchBrowser": true,
                "launchUrl": "api/values",
                    "environmentVariables": {
                        "SKYWALKING__SERVICENAME": "skyapm_sample_app",
                        "ASPNETCORE_ENVIRONMENT": "Development"
                        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore"
                    }
                },
            "SkyApmSample": {
                "commandName": "Project",
                "launchBrowser": true,
                "launchUrl": "api/values",
                "applicationUrl": "http://localhost:5000",
                "environmentVariables": {
                    "SKYWALKING__SERVICENAME": "skyapm_sample_app",
                    "ASPNETCORE_ENVIRONMENT": "Development",
                    "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore"
                }
            }
        }
    }

    也可以通过 bat 文件启动,其内容如下(其它 OS 参考 github):

    bash
    set ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=SkyAPM.Agent.AspNetCore
    set SKYWALKING__SERVICENAME=sample_app
    dotnet run
  6. 打开 http://localhost:8080/ 查看(默认账户和密码都是 admin)。