返回顶部
首页 > 资讯 > 后端开发 > GO >skywalking 链路追踪监控并接入golang,php环境
  • 864
分享到

skywalking 链路追踪监控并接入golang,php环境

skywalkinggolangphp 2023-09-09 11:09:07 864人浏览 泡泡鱼
摘要

k8s安装skywalking [root@master skping]# cat apm-oap-server.yamlapiVersion: apps/v1kind: Deploymentmeta

  1. k8s安装skywalking
[root@master skping]# cat apm-oap-server.yamlapiVersion: apps/v1kind: Deploymentmetadata:  labels:    app: skywalking-oap-server  name: skywalking-oap-server  namespace: ops-sharespec:  replicas: 1  revisionHistoryLimit: 10  selector:    matchLabels:      app: skywalking-oap-server  template:    metadata:      creationTimestamp: null      labels:        app: skywalking-oap-server    spec:      containers:        - env:            - name: SW_STORAGE              value: elasticsearch7            - name: SW_STORAGE_ES_CLUSTER_nodeS              value: '192.168.14.27:9200'  #es地址          image: 'apache/skywalking-oap-server:8.5.0-es7'          imagePullPolicy: IfNotPresent          securityContext:            privileged: true          name: skywalking-oap-server          ports:            - containerPort: 1234              protocol: tcp            - containerPort: 11800              protocol: TCP            - containerPort: 12800              protocol: TCP          resources: {}          terminationMessagePath: /dev/termination-log          terminationMessagePolicy: File      dnsPolicy: ClusterFirst      restartPolicy: Always      schedulerName: default-scheduler      securityContext: {}      terminationGracePeriodSeconds: 30---apiVersion: v1kind: Servicemetadata:  name: skywalking-oap-server  namespace: ops-share  labels:    app: skywalking-oap-serverspec:  type: ClusterIP  ports:    - name: Http1      protocol: TCP      port: 1234      targetPort: 1234    - name: http2      protocol: TCP      port: 11800      targetPort: 11800    - name: http3      protocol: TCP      port: 12800      targetPort: 12800  selector:    app: skywalking-oap-server
[root@master skping]# cat skywalking-ui.yamlapiVersion: apps/v1kind: Deploymentmetadata:  labels:    app: skywalking-ui  name: skywalking-ui  namespace: ops-sharespec:  replicas: 1  selector:    matchLabels:      app: skywalking-ui  template:    metadata:      creationTimestamp: null      labels:        app: skywalking-ui    spec:      containers:        - env:            - name: SW_OAP_ADDRESS              value: 'skywalking-oap-server.ops-share:12800'          image: 'apache/skywalking-ui:8.5.0'          imagePullPolicy: IfNotPresent          name: skywalking-ui          ports:            - containerPort: 8080              name: http              protocol: TCP          resources: {}          terminationMessagePath: /dev/termination-log          terminationMessagePolicy: File      dnsPolicy: ClusterFirst      restartPolicy: Always      schedulerName: default-scheduler      securityContext: {}      terminationGracePeriodSeconds: 30---apiVersion: v1kind: Servicemetadata:  name: skywalking-ui  namespace: ops-share  labels:    app: skywalking-uispec:  #type: ClusterIP  type: NodePort  ports:    - name: http      protocol: TCP      port: 8080      targetPort: http  selector:    app: skywalking-ui
  1. 启动
kubectl ns create ops-sharekubectl apply -f ./kubectl get pod,svc -n ops-share
  1. 访问ip+端口
    在这里插入图片描述
    在这里插入图片描述

  2. golang代码接入skywalking

#cat test.Gopackage mainimport (    "fmt"    "GitHub.com/SkyAPM/go2sky"    v3 "github.com/SkyAPM/go2sky-plugins/gin/v3"    "github.com/SkyAPM/go2sky/reporter"    "github.com/gin-gonic/gin"    "net/http"    "time")func main() {    r := gin.Default()    //skywalking 的端口需要nodeport映射出来,如果你使用的go容器,那么直接使用svc的地址也行。    rp, err := reporter.NewgrpcReporter("192.168.14.27:11800", reporter.WithCheckInterval(time.Second))    if err != nil{        fmt.Println("NewGrpcReporter failed!")        return    }    tracer, err := go2sky.NewTracer("demo2", go2sky.WithReporter(rp))    if err != nil{        fmt.Println("NewTracer failed!")        return    }    //使用go2sky-plugins的middleware,就不用我们自己写span了,插件帮我们完成    go2sky.SetGlobalTracer(tracer)    r.Use(v3.Middleware(r, tracer))    r.GET("/",test)    r.Run(":7070")}func test(c *gin.Context)  {    span,ctx, _ :=go2sky.GetGlobalTracer().CreateLocalSpan(c.Request.Context(),go2sky.WithOperationName("demo2-test"))    if span!=nil{        span.Log(time.Now(),"start demo2-test")        defer span.End()    }    fmt.Println(fmt.Sprintf("traceid:%s  spaneId:%d, test 7070",go2sky.TraceID(ctx),go2sky.SpanID(ctx)))    c.String(http.StatusOK, "test 7070")}
#cat test2.gopackage mainimport (    "context"    "crypto/tls"    "fmt"    "github.com/SkyAPM/go2sky"    v3 "github.com/SkyAPM/go2sky-plugins/gin/v3"    "github.com/SkyAPM/go2sky/reporter"    "github.com/gin-gonic/gin"    "io/ioutil"    "net/http"    "time")func main() {    r := gin.Default()    //skywalking 的端口需要nodeport映射出来,如果你使用的go容器,那么直接使用svc的地址也行。    rp, err := reporter.NewGRPCReporter("192.168.14.27:11800", reporter.WithCheckInterval(time.Second))    if err != nil {        fmt.Println("NewGRPCReporter failed!")        return    }    tracer, err := go2sky.NewTracer("demo1", go2sky.WithReporter(rp))    if err != nil {        fmt.Println("NewTracer failed!")        return    }    //使用go2sky-plugins的middleware,就不用我们自己写span了,插件帮我们完成    go2sky.SetGlobalTracer(tracer)    r.Use(v3.Middleware(r, tracer))    r.GET("/", test)    r.Run(":8085")}func test(c *gin.Context) {    span, ctx, _ := go2sky.GetGlobalTracer().CreateLocalSpan(c.Request.Context(), go2sky.WithOperationName("demo1-test"))    if span != nil {        defer span.End()    }    fmt.Println(fmt.Sprintf("traceid:%s  spaneId:%d, test 8085",go2sky.TraceID(ctx),go2sky.SpanID(ctx)))    HttpRequest(ctx)    c.String(http.StatusOK, "test 8085")}func HttpRequest(ctx context.Context) {    url := "http://localhost:7070/"    request, _ := http.NewRequest(http.MethodGet, url, nil)    span, _ := go2sky.GetGlobalTracer().CreateExitSpan(ctx, "httpRequest", url, func(headerKey, headerValue string) error {        request.Header.Set(headerKey, headerValue)        return nil    })    defer span.End()    span.Log(time.Now(), "start "+url)    request = request.WithContext(ctx)    client := http.Client{Transport: &http.Transport{DisableKeepAlives: true, TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}    response, err := client.Do(request)    if err != nil {        span.Error(time.Now(), fmt.Sprintf("%v", err))    } else {        defer response.Body.Close()        responseByte, _ := ioutil.ReadAll(response.Body)        span.Log(time.Now(), "response:"+string(responseByte))        fmt.Println(fmt.Sprintf("traceid:%s  spaneId:%d, response%s", go2sky.TraceID(ctx), go2sky.SpanID(ctx), string(responseByte)))    }}
  1. 启动go服务
go run test.go go run test2.go 
  1. 访问ip+8085地址
    在这里插入图片描述

  2. 多请求一点数据让skywalking数据展示出来

for i in {1..100};do sleep 2; hey -n 100 -m get  http://192.168.10.100:8085 ;done

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  1. PHP-fpm容器接入skywalking
Docker pull  skyapm/skywalking-php:v4.2.0-7.2-fpm-alpinedocker run -itd  -p 8080:8080 skyapm/skywalking-php:v4.2.0-7.2-fpm-alpine bash docker exec -it id bashcd /usr/local/etc/php/conf.d/cat ext-skywalking.ini[skywalking]extension=skywalking.soskywalking.app_code = hello_skywalkingskywalking.enable = 1skywalking.version = 8skywalking.grpc = 192.168.14.27:11800#skywalking.error_handler_enable = 0#skywalking.sample_n_per_3_secs = -1#skywalking.instance_name = ""cd /var/www/htmlcat 2.php<?php$url = 'http://192.168.10.100:8085/';echo file_get_contents($url);?>
  1. 重启docker php-fpm容器并访问8080/2.php
    页面显示test 8085 因为访问的8085的接口

  2. 查看skywalking是否监控到了php
    在这里插入图片描述

在这里插入图片描述

来源地址:https://blog.csdn.net/weixin_43606975/article/details/128575800

您可能感兴趣的文档:

--结束END--

本文标题: skywalking 链路追踪监控并接入golang,php环境

本文链接: https://lsjlt.com/news/401466.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作