From 3f2dff4dfcca83d2efe044a93a95b9b807f9da4b Mon Sep 17 00:00:00 2001 From: smx_Moragn <1048492656@qq.com> Date: Fri, 11 Oct 2024 18:12:22 +0800 Subject: [PATCH 1/9] Connect to cwgo-pkg --- example/main.go | 33 ++++++++++++++++ go.mod | 42 +++++++++++++------- go.sum | 96 +++++++++++++++++++++++++++++---------------- trace.go | 101 ++++++------------------------------------------ 4 files changed, 133 insertions(+), 139 deletions(-) create mode 100644 example/main.go diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..a435f9b --- /dev/null +++ b/example/main.go @@ -0,0 +1,33 @@ +/* + * Copyright 2024 CloudWeGo Authors + * + * Licensed 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. + */ + +package main + +import ( + "context" + "github.com/cloudwego/hertz/pkg/app" + "github.com/cloudwego/hertz/pkg/app/server" + "github.com/cloudwego/hertz/pkg/common/utils" + prometheus "github.com/hertz-contrib/monitor-prometheus" +) + +func main() { + h := server.Default(server.WithTracer(prometheus.NewServerTracer(":9091", "/hertz"))) + h.GET("/ping", func(ctx context.Context, c *app.RequestContext) { + c.JSON(200, utils.H{"ping": "pong"}) + }) + h.Spin() +} diff --git a/go.mod b/go.mod index b757d08..aa6c57e 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,13 @@ module github.com/hertz-contrib/monitor-prometheus -go 1.19 +go 1.21 + require ( - github.com/cloudwego/hertz v0.7.3 - github.com/prometheus/client_golang v1.17.0 - github.com/stretchr/testify v1.8.4 + github.com/cloudwego-contrib/cwgo-pkg/telemetry v0.0.0 + github.com/cloudwego/hertz v0.9.2 + github.com/prometheus/client_golang v1.19.1 + github.com/stretchr/testify v1.9.0 ) require ( @@ -14,28 +16,38 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/bytedance/go-tagexpr/v2 v2.9.11 // indirect github.com/bytedance/gopkg v0.0.0-20231219111115-a5eedbe96960 // indirect - github.com/bytedance/sonic v1.10.2 // indirect - github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/bytedance/sonic v1.11.2 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect - github.com/cloudwego/netpoll v0.5.1 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/cloudwego/netpoll v0.6.0 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/klauspost/cpuid/v2 v2.2.6 // indirect github.com/kr/text v0.2.0 // indirect - github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/nyaruka/phonenumbers v1.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_model v0.5.0 // indirect - github.com/prometheus/common v0.45.0 // indirect - github.com/prometheus/procfs v0.12.0 // indirect + github.com/prometheus/client_model v0.6.1 // indirect + github.com/prometheus/common v0.55.0 // indirect + github.com/prometheus/procfs v0.15.1 // indirect github.com/tidwall/gjson v1.17.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + go.opentelemetry.io/otel v1.28.0 // indirect + go.opentelemetry.io/otel/metric v1.28.0 // indirect + go.opentelemetry.io/otel/sdk v1.28.0 // indirect + go.opentelemetry.io/otel/trace v1.28.0 // indirect golang.org/x/arch v0.6.0 // indirect - golang.org/x/sys v0.15.0 // indirect - golang.org/x/text v0.14.0 // indirect - google.golang.org/protobuf v1.31.0 // indirect + golang.org/x/sys v0.21.0 // indirect + golang.org/x/text v0.16.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +//replace github.com/cloudwego-contrib/cwgo-pkg => D:\Projects\Go\cwgo-pkg +replace github.com/cloudwego-contrib/cwgo-pkg/telemetry => D:\Projects\Go\cwgo-pkg\telemetry diff --git a/go.sum b/go.sum index 2ba3a2c..a1eb6ff 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,6 @@ github.com/bytedance/go-tagexpr/v2 v2.9.2/go.mod h1:5qsx05dYOiUXOUgnQ7w3Oz8BYs2q github.com/bytedance/go-tagexpr/v2 v2.9.11 h1:jJgmoDKPKacGl0llPYbYL/+/2N+Ng0vV0ipbnVssXHY= github.com/bytedance/go-tagexpr/v2 v2.9.11/go.mod h1:UAyKh4ZRLBPGsyTRFZoPqTni1TlojMdOJXQnEIPCX84= github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7/go.mod h1:2ZlV9BaUH4+NXIBF0aMdKKAnHTzqH+iMU4KUjAbL23Q= -github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b h1:R6PWoQtxEMpWJPHnpci+9LgFxCS7iJCfOGBvCgZeTKI= -github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ= github.com/bytedance/gopkg v0.0.0-20231219111115-a5eedbe96960 h1:t2xAuIlnhWJDIpcHZEbpoVsQH1hOk9eGGaKU2dXl1PE= github.com/bytedance/gopkg v0.0.0-20231219111115-a5eedbe96960/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ= github.com/bytedance/mockey v1.2.1 h1:g84ngI88hz1DR4wZTL3yOuqlEcq67MretBfQUdXwrmw= @@ -17,10 +15,10 @@ github.com/bytedance/mockey v1.2.1/go.mod h1:+Jm/fzWZAuhEDrPXVjDf/jLM2BlLXJkwk94 github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.8.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= -github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE= -github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= -github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= -github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/bytedance/sonic v1.11.2 h1:ywfwo0a/3j9HR8wsYGWsIWl2mvRsI950HyoxiBERw5A= +github.com/bytedance/sonic v1.11.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= @@ -28,22 +26,30 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpV github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= -github.com/cloudwego/hertz v0.7.3 h1:VM1DxditA6vxI97rG5SBu4hHB24xdzDbKBQfUy7sfVE= -github.com/cloudwego/hertz v0.7.3/go.mod h1:WliNtVbwihWHHgAaIQEbVXl0O3aWj0ks1eoPrcEAnjs= -github.com/cloudwego/netpoll v0.5.0/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ= -github.com/cloudwego/netpoll v0.5.1 h1:zDUF7xF0C97I10fGlQFJ4jg65khZZMUvSu/TWX44Ohc= -github.com/cloudwego/netpoll v0.5.1/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ= +github.com/cloudwego/hertz v0.9.2 h1:VbqddZ5RuvcgxzfxvXcmTiRisGYoo0+WnHGeDJKhjqI= +github.com/cloudwego/hertz v0.9.2/go.mod h1:cs8dH6unM4oaJ5k9m6pqbgLBPqakGWMG0+cthsxitsg= +github.com/cloudwego/netpoll v0.6.0 h1:JRMkrA1o8k/4quxzg6Q1XM+zIhwZsyoWlq6ef+ht31U= +github.com/cloudwego/netpoll v0.6.0/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/henrylee2cn/ameda v1.4.8/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4= @@ -56,27 +62,26 @@ github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/4 github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= -github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= -github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/nyaruka/phonenumbers v1.0.55/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJrupC6LuhshJq1U= -github.com/nyaruka/phonenumbers v1.2.2 h1:OwVjf7Y4uHoK9VJUrA8ebR0ha2yc6sEYbfrwkq0asCY= -github.com/nyaruka/phonenumbers v1.2.2/go.mod h1:wzk2qq7qwsaBKrfbkWKdgHYOOH+QFTesSpIq53ELw8M= github.com/nyaruka/phonenumbers v1.3.0 h1:IFyyJfF2Elg8xGKFghWrRXzb6qAHk+Q3uPqmIgS20JQ= github.com/nyaruka/phonenumbers v1.3.0/go.mod h1:4jyKp/BFUokLbCHyoZag+T3S1KezFVoEKtgnbpzItC4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q= -github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY= -github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= -github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= -github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM= -github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= -github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= -github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= +github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE= +github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho= +github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= +github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= +github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc= +github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8= +github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= +github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= @@ -91,8 +96,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM= @@ -104,6 +109,26 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +go.opentelemetry.io/contrib/propagators/b3 v1.20.0 h1:Yty9Vs4F3D6/liF1o6FNt0PvN85h/BJJ6DQKJ3nrcM0= +go.opentelemetry.io/contrib/propagators/b3 v1.20.0/go.mod h1:On4VgbkqYL18kbJlWsa18+cMNe6rYpBnPi1ARI/BrsU= +go.opentelemetry.io/contrib/propagators/ot v1.20.0 h1:duH7mgL6VGQH7e7QEAVOFkCQXWpCb4PjTtrhdrYrJRQ= +go.opentelemetry.io/contrib/propagators/ot v1.20.0/go.mod h1:gijQzxOq0JLj9lyZhTvqjDddGV/zaNagpPIn+2r8CEI= +go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo= +go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4= +go.opentelemetry.io/otel/exporters/prometheus v0.50.0 h1:2Ewsda6hejmbhGFyUvWZjUThC98Cf8Zy6g0zkIimOng= +go.opentelemetry.io/otel/exporters/prometheus v0.50.0/go.mod h1:pMm5PkUo5YwbLiuEf7t2xg4wbP0/eSJrMxIMxKosynY= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.28.0 h1:EVSnY9JbEEW92bEkIYOVMw4q1WJxIAGoFTrtYOzWuRQ= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.28.0/go.mod h1:Ea1N1QQryNXpCD0I1fdLibBAIpQuBkznMmkdKrapk1Y= +go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q= +go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s= +go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE= +go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg= +go.opentelemetry.io/otel/sdk/metric v1.28.0 h1:OkuaKgKrgAbYrrY0t92c+cC+2F6hsFNnCQArXCKlg08= +go.opentelemetry.io/otel/sdk/metric v1.28.0/go.mod h1:cWPjykihLAPvXKi4iZc1dpER3Jdq2Z0YLse3moQUCpg= +go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g= +go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= @@ -111,6 +136,8 @@ golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= +golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -118,22 +145,23 @@ golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/trace.go b/trace.go index ed6ba64..c4eb515 100644 --- a/trace.go +++ b/trace.go @@ -17,64 +17,15 @@ package prometheus import ( - "context" - "net/http" - "strconv" - - "github.com/cloudwego/hertz/pkg/common/hlog" - "github.com/prometheus/client_golang/prometheus/promhttp" - - "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/common/tracer" - "github.com/cloudwego/hertz/pkg/common/tracer/stats" - prom "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/collectors" -) -const ( - labelMethod = "method" - labelStatusCode = "statusCode" - labelPath = "path" + "github.com/cloudwego-contrib/cwgo-pkg/telemetry/provider/promprovider" - unknownLabelValue = "unknown" + "github.com/cloudwego-contrib/cwgo-pkg/telemetry/instrumentation/otelhertz" ) -// genLabels make labels values. -func genLabels(ctx *app.RequestContext) prom.Labels { - labels := make(prom.Labels) - labels[labelMethod] = defaultValIfEmpty(string(ctx.Request.Method()), unknownLabelValue) - labels[labelStatusCode] = defaultValIfEmpty(strconv.Itoa(ctx.Response.Header.StatusCode()), unknownLabelValue) - labels[labelPath] = defaultValIfEmpty(ctx.FullPath(), unknownLabelValue) - - return labels -} - -type serverTracer struct { - serverHandledCounter *prom.CounterVec - serverHandledHistogram *prom.HistogramVec -} - -// Start record the beginning of server handling request from client. -func (s *serverTracer) Start(ctx context.Context, c *app.RequestContext) context.Context { - return ctx -} - -// Finish record the ending of server handling request from client. -func (s *serverTracer) Finish(ctx context.Context, c *app.RequestContext) { - if c.GetTraceInfo().Stats().Level() == stats.LevelDisabled { - return - } - - httpStart := c.GetTraceInfo().Stats().GetEvent(stats.HTTPStart) - httpFinish := c.GetTraceInfo().Stats().GetEvent(stats.HTTPFinish) - if httpFinish == nil || httpStart == nil { - return - } - cost := httpFinish.Time().Sub(httpStart.Time()) - _ = counterAdd(s.serverHandledCounter, 1, genLabels(c)) - _ = histogramObserve(s.serverHandledHistogram, cost, genLabels(c)) -} - // NewServerTracer provides tracer for server access, addr and path is the scrape_configs for prometheus server. func NewServerTracer(addr, path string, opts ...Option) tracer.Tracer { cfg := defaultConfig() @@ -82,48 +33,18 @@ func NewServerTracer(addr, path string, opts ...Option) tracer.Tracer { for _, opts := range opts { opts.apply(cfg) } - - if !cfg.disableServer { - http.Handle(path, promhttp.HandlerFor(cfg.registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError})) - go func() { - if err := http.ListenAndServe(addr, nil); err != nil { - hlog.Fatal("HERTZ: Unable to start a promhttp server, err: " + err.Error()) - } - }() - } - - serverHandledCounter := prom.NewCounterVec( - prom.CounterOpts{ - Name: "hertz_server_throughput", - Help: "Total number of HTTPs completed by the server, regardless of success or failure.", - }, - []string{labelMethod, labelStatusCode, labelPath}, - ) - cfg.registry.MustRegister(serverHandledCounter) - - serverHandledHistogram := prom.NewHistogramVec( - prom.HistogramOpts{ - Name: "hertz_server_latency_us", - Help: "Latency (microseconds) of HTTP that had been application-level handled by the server.", - Buckets: cfg.buckets, - }, - []string{labelMethod, labelStatusCode, labelPath}, - ) - cfg.registry.MustRegister(serverHandledHistogram) - if cfg.enableGoCollector { cfg.registry.MustRegister(collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(cfg.runtimeMetricRules...))) } + p := promprovider.NewPromProvider( + promprovider.WithRegistry(cfg.registry), + promprovider.WithHistogramBuckets(cfg.buckets), + promprovider.WithHttpServer(), + ) - return &serverTracer{ - serverHandledCounter: serverHandledCounter, - serverHandledHistogram: serverHandledHistogram, + if !cfg.disableServer { + p.Serve(addr, path) } -} -func defaultValIfEmpty(val, def string) string { - if val == "" { - return def - } - return val + return otelhertz.NewServerTracer() } From ce754f0159f93b61e0ba2691ebf24e4b608ffe26 Mon Sep 17 00:00:00 2001 From: smx_Moragn <1048492656@qq.com> Date: Tue, 15 Oct 2024 10:56:22 +0800 Subject: [PATCH 2/9] softly Connect to cwgo-pkg --- go.mod | 6 +----- go.sum | 2 ++ 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index aa6c57e..888c67c 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,8 @@ module github.com/hertz-contrib/monitor-prometheus go 1.21 - require ( - github.com/cloudwego-contrib/cwgo-pkg/telemetry v0.0.0 + github.com/cloudwego-contrib/cwgo-pkg/telemetry v0.0.0-20241014044734-80a98dbe0b6a github.com/cloudwego/hertz v0.9.2 github.com/prometheus/client_golang v1.19.1 github.com/stretchr/testify v1.9.0 @@ -48,6 +47,3 @@ require ( google.golang.org/protobuf v1.34.2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) - -//replace github.com/cloudwego-contrib/cwgo-pkg => D:\Projects\Go\cwgo-pkg -replace github.com/cloudwego-contrib/cwgo-pkg/telemetry => D:\Projects\Go\cwgo-pkg\telemetry diff --git a/go.sum b/go.sum index a1eb6ff..6d38392 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpV github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/cloudwego-contrib/cwgo-pkg/telemetry v0.0.0-20241014044734-80a98dbe0b6a h1:jB0AG6R9hB/UB7m13a8PJyINrO/Q/CvujvGbUNks48I= +github.com/cloudwego-contrib/cwgo-pkg/telemetry v0.0.0-20241014044734-80a98dbe0b6a/go.mod h1:xnZ0Px0hRRTDNO5KaicZtGIkza32DBpFglJty79yLzI= github.com/cloudwego/hertz v0.9.2 h1:VbqddZ5RuvcgxzfxvXcmTiRisGYoo0+WnHGeDJKhjqI= github.com/cloudwego/hertz v0.9.2/go.mod h1:cs8dH6unM4oaJ5k9m6pqbgLBPqakGWMG0+cthsxitsg= github.com/cloudwego/netpoll v0.6.0 h1:JRMkrA1o8k/4quxzg6Q1XM+zIhwZsyoWlq6ef+ht31U= From 9bed3274d6fa44b40104fa74a37c039b13fde799 Mon Sep 17 00:00:00 2001 From: smx_Moragn <1048492656@qq.com> Date: Tue, 15 Oct 2024 11:35:32 +0800 Subject: [PATCH 3/9] softly Connect to cwgo-pkg --- tracer_test.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tracer_test.go b/tracer_test.go index 9d30e35..16ba237 100644 --- a/tracer_test.go +++ b/tracer_test.go @@ -70,16 +70,17 @@ func TestServerTracerWorkWithHertz(t *testing.T) { metricsResStr := string(metricsResBytes) - assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="GET",path="/metricGet",statusCode="200",le="+Inf"} 10`)) - assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_count{method="GET",path="/metricGet",statusCode="200"} 10`)) - assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="250000"} 10`)) - // response time is always greater than 50000 microseconds - assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="50000"} 0`)) - assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="5000"} 0`)) - assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_count{method="POST",path="/metricPost",statusCode="200"} 10`)) - - assert.True(t, strings.Contains(metricsResStr, `hertz_server_throughput{method="GET",path="/metricGet",statusCode="200"} 10`)) - assert.True(t, strings.Contains(metricsResStr, `hertz_server_throughput{method="POST",path="/metricPost",statusCode="200"} 10`)) + assert.True(t, strings.Contains(metricsResStr, `http_latency_bucket{http_method="GET",http_status_code="200",path="/metricGet",le="+Inf"} 10`)) + assert.True(t, strings.Contains(metricsResStr, `http_latency_count{http_method="GET",http_status_code="200",path="/metricGet"} 10`)) + + assert.True(t, strings.Contains(metricsResStr, `http_latency_bucket{http_method="POST",http_status_code="200",path="/metricPost",le="250000"} 10`)) + + assert.True(t, strings.Contains(metricsResStr, `http_latency_bucket{http_method="POST",http_status_code="200",path="/metricPost",le="50000"} 10`)) + assert.True(t, strings.Contains(metricsResStr, `http_latency_bucket{http_method="POST",http_status_code="200",path="/metricPost",le="5000"} 10`)) + assert.True(t, strings.Contains(metricsResStr, `http_latency_count{http_method="POST",http_status_code="200",path="/metricPost"} 10`)) + + assert.True(t, strings.Contains(metricsResStr, `http_counter{http_method="GET",http_status_code="200",path="/metricGet"} 10`)) + assert.True(t, strings.Contains(metricsResStr, `http_counter{http_method="POST",http_status_code="200",path="/metricPost"} 10`)) } // TestWithOption test server tracer with options @@ -169,8 +170,8 @@ func TestWithBucketsOption(t *testing.T) { metricsResStr := string(metricsResBytes) - assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="500"} 0`)) - assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="250000"} 10`)) + assert.True(t, strings.Contains(metricsResStr, `http_latency_bucket{http_method="POST",http_status_code="200",path="/metricPost",le="500"} 10`)) + assert.True(t, strings.Contains(metricsResStr, `http_latency_bucket{http_method="POST",http_status_code="200",path="/metricPost",le="250000"} 10`)) // must not contains values in defaultBuckets - assert.False(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="500000"} 10`)) + assert.False(t, strings.Contains(metricsResStr, `http_latency_bucket{http_method="POST",http_status_code="200",path="/metricPost",le="+Inf"} 10`)) } From e30c54d5fbb7d89409aef460d76451656668ea4a Mon Sep 17 00:00:00 2001 From: smx_Moragn <1048492656@qq.com> Date: Wed, 16 Oct 2024 06:33:10 +0800 Subject: [PATCH 4/9] Fix format --- example/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/example/main.go b/example/main.go index a435f9b..d094b8a 100644 --- a/example/main.go +++ b/example/main.go @@ -18,6 +18,7 @@ package main import ( "context" + "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/common/utils" From ccc8892646f2c4729e3e823daa6e6fb12786fd19 Mon Sep 17 00:00:00 2001 From: smx_Moragn <1048492656@qq.com> Date: Mon, 21 Oct 2024 09:45:31 +0800 Subject: [PATCH 5/9] modify header --- example/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/main.go b/example/main.go index d094b8a..77d60a3 100644 --- a/example/main.go +++ b/example/main.go @@ -1,5 +1,5 @@ /* - * Copyright 2024 CloudWeGo Authors + * Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 517e1da8f36247b5c64beca26204350b23bd0d96 Mon Sep 17 00:00:00 2001 From: smx_Moragn <1048492656@qq.com> Date: Tue, 22 Oct 2024 17:33:04 +0800 Subject: [PATCH 6/9] fix ci --- .github/workflows/pr-check.yml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 629d127..7bfe62c 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -11,16 +11,13 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: 1.19 + go-version: 1.21 - name: Check License Header uses: apache/skywalking-eyes@main env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: typos-action - uses: crate-ci/typos@master - staticcheck: runs-on: self-hosted steps: @@ -29,7 +26,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: 1.19 + go-version: 1.21 - uses: actions/cache@v3 with: From 714bc596885ee58134347356726285165bff0642 Mon Sep 17 00:00:00 2001 From: smx_Moragn <1048492656@qq.com> Date: Tue, 22 Oct 2024 17:35:51 +0800 Subject: [PATCH 7/9] update go lint --- .github/workflows/tests.yml | 12 ++++++------ .golangci.yaml | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 .golangci.yaml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 54fd29c..dcd75ea 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,7 +11,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: 1.19 + go-version: 1.21 - uses: actions/cache@v3 with: @@ -20,11 +20,11 @@ jobs: restore-keys: | ${{ runner.os }}-go- - - name: Lint - run: | - go vet -stdmethods=false $(go list ./...) - go install mvdan.cc/gofumpt@v0.2.0 - test -z "$(gofumpt -l -extra .)" + - name: Golangci Lint + # https://golangci-lint.run/ + uses: golangci/golangci-lint-action@v3 + with: + version: latest - name: Unit Test run: go test -race -covermode=atomic -coverprofile=coverage.out ./... diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..d967796 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,34 @@ +# Options for analysis running. +run: + # include `vendor` `third_party` `testdata` `examples` `Godeps` `builtin` + skip-dirs-use-default: true +# output configuration options +output: + # Format: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions + formats: colored-line-number +# All available settings of specific linters. +# Refer to https://golangci-lint.run/usage/linters +linters-settings: + gofumpt: + # Choose whether to use the extra rules. + # Default: false + extra-rules: true + govet: + # Disable analyzers by name. + # Run `go tool vet help` to see all analyzers. + disable: + - stdmethods +linters: + enable: + - gofumpt + - goimports + - gofmt + disable: + - errcheck + - typecheck + - staticcheck +issues: + exclude-use-default: true + exclude-files: + - ".*\\.mock\\.go$" + exclude-dirs: From 4c7ff3e3a8cb58dc202f22ec05c34e4885735105 Mon Sep 17 00:00:00 2001 From: smx_Moragn <1048492656@qq.com> Date: Tue, 22 Oct 2024 18:00:42 +0800 Subject: [PATCH 8/9] update test --- tracer_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracer_test.go b/tracer_test.go index 16ba237..9a547f6 100644 --- a/tracer_test.go +++ b/tracer_test.go @@ -173,5 +173,5 @@ func TestWithBucketsOption(t *testing.T) { assert.True(t, strings.Contains(metricsResStr, `http_latency_bucket{http_method="POST",http_status_code="200",path="/metricPost",le="500"} 10`)) assert.True(t, strings.Contains(metricsResStr, `http_latency_bucket{http_method="POST",http_status_code="200",path="/metricPost",le="250000"} 10`)) // must not contains values in defaultBuckets - assert.False(t, strings.Contains(metricsResStr, `http_latency_bucket{http_method="POST",http_status_code="200",path="/metricPost",le="+Inf"} 10`)) + assert.False(t, strings.Contains(metricsResStr, `http_latency_bucket{http_method="POST",http_status_code="200",path="/metricPost",le="500000"} 10`)) } From 231cdd7566230328ab5bd19644caa6847992d18a Mon Sep 17 00:00:00 2001 From: smx_Moragn <1048492656@qq.com> Date: Tue, 22 Oct 2024 18:16:23 +0800 Subject: [PATCH 9/9] replace --- tracer_test.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tracer_test.go b/tracer_test.go index 9a547f6..51356b1 100644 --- a/tracer_test.go +++ b/tracer_test.go @@ -40,9 +40,10 @@ func TestServerTracerWorkWithHertz(t *testing.T) { }) h.POST("/metricPost", func(c context.Context, ctx *app.RequestContext) { - rand.Seed(time.Now().UnixMilli()) + seed := time.Now().UnixMilli() + rnd := rand.New(rand.NewSource(seed)) // make sure the response time is greater than 50 milliseconds and less than around 151 milliseconds - time.Sleep(time.Duration(rand.Intn(100)+51) * time.Millisecond) + time.Sleep(time.Duration(rnd.Intn(100)+51) * time.Millisecond) ctx.String(200, "hello post") }) @@ -140,9 +141,10 @@ func TestWithBucketsOption(t *testing.T) { }) h.POST("/metricPost", func(c context.Context, ctx *app.RequestContext) { - rand.Seed(time.Now().UnixMilli()) + seed := time.Now().UnixMilli() + rnd := rand.New(rand.NewSource(seed)) // make sure the response time is greater than 50 milliseconds and less than around 151 milliseconds - time.Sleep(time.Duration(rand.Intn(100)+51) * time.Millisecond) + time.Sleep(time.Duration(rnd.Intn(100)+51) * time.Millisecond) ctx.String(200, "hello post") })