From 6d2aec3a073f081359791aeee0c2a2ee0f2e3c90 Mon Sep 17 00:00:00 2001 From: Gabe <7622243+decentralgabe@users.noreply.github.com> Date: Tue, 5 Dec 2023 22:19:14 +0100 Subject: [PATCH] Add otel (#65) * Add otel * dont run 1000 times --- impl/cmd/main.go | 6 ++---- impl/config/config.go | 1 + impl/docs/docs.go | 8 ++++---- impl/docs/swagger.yaml | 6 ++---- impl/go.mod | 16 +++++++++++----- impl/go.sum | 23 +++++++++++++++++++++++ impl/internal/did/client_test.go | 2 +- impl/pkg/server/server.go | 2 ++ 8 files changed, 46 insertions(+), 18 deletions(-) diff --git a/impl/cmd/main.go b/impl/cmd/main.go index c352af1f..8a37320a 100644 --- a/impl/cmd/main.go +++ b/impl/cmd/main.go @@ -16,15 +16,13 @@ import ( // main godoc // -// @title DID DHT Service API +// @title The DID DHT Service // @description The DID DHT Service // @contact.name TBD -// @contact.url https://github.com/TBD54566975/did-dht-method/issues +// @contact.url https://github.com/TBD54566975/did-dht-method // @contact.email tbd-developer@squareup.com // @license.name Apache 2.0 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html -// @host {{.Server.APIHost}} -// @version {{.SVN}} func main() { logrus.Info("Starting up...") diff --git a/impl/config/config.go b/impl/config/config.go index d31ed494..b0eb0bc9 100644 --- a/impl/config/config.go +++ b/impl/config/config.go @@ -13,6 +13,7 @@ import ( ) const ( + ServiceName = "did-dht" DefaultConfigPath = "config/config.toml" DefaultEnvPath = "config/config.env" Extension = ".toml" diff --git a/impl/docs/docs.go b/impl/docs/docs.go index ab9a07fb..394a1cbf 100644 --- a/impl/docs/docs.go +++ b/impl/docs/docs.go @@ -5,15 +5,15 @@ package docs import "github.com/swaggo/swag/v2" const docTemplate = `{ - "schemes": {{ marshal .Schemes }},"swagger":"2.0","info":{"description":"{{escape .Description}}","title":"{{.Title}}","contact":{"name":"TBD","url":"https://github.com/TBD54566975/did-dht-method/issues","email":"tbd-developer@squareup.com"},"license":{"name":"Apache 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0.html"},"version":"{{.Version}}"},"host":"{{.Host}}","basePath":"{{.BasePath}}","paths":{"/health":{"get":{"description":"Health is a simple handler that always responds with a 200 OK","consumes":["application/json"],"produces":["application/json"],"tags":["Health"],"summary":"Health Check","responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/pkg_server.GetHealthCheckResponse"}}}}},"/{id}":{"get":{"description":"GetRecord a Pkarr record from the DHT","consumes":["application/octet-stream"],"produces":["application/octet-stream"],"tags":["Pkarr"],"summary":"GetRecord a Pkarr record from the DHT","parameters":[{"type":"string","description":"ID to get","name":"id","in":"path","required":true}],"responses":{"200":{"description":"64 bytes sig, 8 bytes u64 big-endian seq, 0-1000 bytes of v.","schema":{"type":"array","items":{"type":"integer"}}},"400":{"description":"Bad request","schema":{"type":"string"}},"404":{"description":"Not found","schema":{"type":"string"}},"500":{"description":"Internal server error","schema":{"type":"string"}}}},"put":{"description":"PutRecord a Pkarr record into the DHT","consumes":["application/octet-stream"],"tags":["Pkarr"],"summary":"PutRecord a Pkarr record into the DHT","parameters":[{"type":"string","description":"ID of the record to put","name":"id","in":"path","required":true},{"description":"64 bytes sig, 8 bytes u64 big-endian seq, 0-1000 bytes of v.","name":"request","in":"body","required":true,"schema":{"type":"array","items":{"type":"integer"}}}],"responses":{"200":{"description":"OK"},"400":{"description":"Bad request","schema":{"type":"string"}},"500":{"description":"Internal server error","schema":{"type":"string"}}}}}},"definitions":{"pkg_server.GetHealthCheckResponse":{"type":"object","properties":{"status":{"description":"Status is always equal to ` + "`" + `OK` + "`" + `.","type":"string"}}}}}` + "schemes": {{ marshal .Schemes }},"swagger":"2.0","info":{"description":"{{escape .Description}}","title":"{{.Title}}","contact":{"name":"TBD","url":"https://github.com/TBD54566975/did-dht-method","email":"tbd-developer@squareup.com"},"license":{"name":"Apache 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0.html"},"version":"{{.Version}}"},"host":"{{.Host}}","basePath":"{{.BasePath}}","paths":{"/health":{"get":{"description":"Health is a simple handler that always responds with a 200 OK","consumes":["application/json"],"produces":["application/json"],"tags":["Health"],"summary":"Health Check","responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/pkg_server.GetHealthCheckResponse"}}}}},"/{id}":{"get":{"description":"GetRecord a Pkarr record from the DHT","consumes":["application/octet-stream"],"produces":["application/octet-stream"],"tags":["Pkarr"],"summary":"GetRecord a Pkarr record from the DHT","parameters":[{"type":"string","description":"ID to get","name":"id","in":"path","required":true}],"responses":{"200":{"description":"64 bytes sig, 8 bytes u64 big-endian seq, 0-1000 bytes of v.","schema":{"type":"array","items":{"type":"integer"}}},"400":{"description":"Bad request","schema":{"type":"string"}},"404":{"description":"Not found","schema":{"type":"string"}},"500":{"description":"Internal server error","schema":{"type":"string"}}}},"put":{"description":"PutRecord a Pkarr record into the DHT","consumes":["application/octet-stream"],"tags":["Pkarr"],"summary":"PutRecord a Pkarr record into the DHT","parameters":[{"type":"string","description":"ID of the record to put","name":"id","in":"path","required":true},{"description":"64 bytes sig, 8 bytes u64 big-endian seq, 0-1000 bytes of v.","name":"request","in":"body","required":true,"schema":{"type":"array","items":{"type":"integer"}}}],"responses":{"200":{"description":"OK"},"400":{"description":"Bad request","schema":{"type":"string"}},"500":{"description":"Internal server error","schema":{"type":"string"}}}}}},"definitions":{"pkg_server.GetHealthCheckResponse":{"type":"object","properties":{"status":{"description":"Status is always equal to ` + "`" + `OK` + "`" + `.","type":"string"}}}}}` // SwaggerInfo holds exported Swagger Info so clients can modify it var SwaggerInfo = &swag.Spec{ - Version: "{{.SVN}}", - Host: "{{.Server.APIHost}}", + Version: "", + Host: "", BasePath: "", Schemes: []string{}, - Title: "DID DHT Service API", + Title: "The DID DHT Service", Description: "The DID DHT Service", InfoInstanceName: "swagger", SwaggerTemplate: docTemplate, diff --git a/impl/docs/swagger.yaml b/impl/docs/swagger.yaml index 1e18e6da..b8389710 100644 --- a/impl/docs/swagger.yaml +++ b/impl/docs/swagger.yaml @@ -5,18 +5,16 @@ definitions: description: Status is always equal to `OK`. type: string type: object -host: '{{.Server.APIHost}}' info: contact: email: tbd-developer@squareup.com name: TBD - url: https://github.com/TBD54566975/did-dht-method/issues + url: https://github.com/TBD54566975/did-dht-method description: The DID DHT Service license: name: Apache 2.0 url: http://www.apache.org/licenses/LICENSE-2.0.html - title: DID DHT Service API - version: '{{.SVN}}' + title: The DID DHT Service paths: /{id}: get: diff --git a/impl/go.mod b/impl/go.mod index 6af99037..430de35c 100644 --- a/impl/go.mod +++ b/impl/go.mod @@ -29,7 +29,7 @@ require ( github.com/swaggo/swag/v2 v2.0.0-rc3 github.com/tv42/zbase32 v0.0.0-20220222190657-f76a9fc892fa go.etcd.io/bbolt v1.3.7 - golang.org/x/term v0.13.0 + golang.org/x/term v0.14.0 ) require ( @@ -55,6 +55,8 @@ require ( github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-logr/logr v1.3.0 // indirect + github.com/go-logr/stdr v1.2.2 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/spec v0.20.9 // indirect @@ -110,16 +112,20 @@ require ( github.com/swaggo/swag v1.8.12 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect + go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.46.1 // indirect + go.opentelemetry.io/otel v1.21.0 // indirect + go.opentelemetry.io/otel/metric v1.21.0 // indirect + go.opentelemetry.io/otel/trace v1.21.0 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/arch v0.3.0 // indirect - golang.org/x/crypto v0.14.0 // indirect + golang.org/x/crypto v0.15.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/mod v0.12.0 // indirect - golang.org/x/net v0.17.0 // indirect + golang.org/x/net v0.18.0 // indirect golang.org/x/sync v0.3.0 // indirect - golang.org/x/sys v0.13.0 // indirect - golang.org/x/text v0.13.0 // indirect + golang.org/x/sys v0.14.0 // indirect + golang.org/x/text v0.14.0 // indirect golang.org/x/time v0.3.0 // indirect golang.org/x/tools v0.13.0 // indirect google.golang.org/protobuf v1.31.0 // indirect diff --git a/impl/go.sum b/impl/go.sum index 4c585aec..7a128371 100644 --- a/impl/go.sum +++ b/impl/go.sum @@ -189,6 +189,11 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2 github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= +github.com/go-logr/logr v1.3.0/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/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE= @@ -543,6 +548,14 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= +go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.46.1 h1:mMv2jG58h6ZI5t5S9QCVGdzCmAsTakMa3oxVgpSD44g= +go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.46.1/go.mod h1:oqRuNKG0upTaDPbLVCG8AD0G2ETrfDtmh7jViy7ox6M= +go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc= +go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo= +go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4= +go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM= +go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc= +go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= @@ -562,6 +575,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= +golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -643,6 +658,8 @@ golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= +golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -721,12 +738,16 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/term v0.14.0 h1:LGK9IlZ8T9jvdy6cTdfKUCltatMFOehAQo9SRC46UQ8= +golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -739,6 +760,8 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +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/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/impl/internal/did/client_test.go b/impl/internal/did/client_test.go index 5f00b678..567441ee 100644 --- a/impl/internal/did/client_test.go +++ b/impl/internal/did/client_test.go @@ -13,6 +13,7 @@ import ( func TestClient(t *testing.T) { client, err := NewGatewayClient("https://diddht.tbddev.org") require.NoError(t, err) + start := time.Now() sk, doc, err := GenerateDIDDHT(CreateDIDDHTOpts{}) require.NoError(t, err) @@ -22,7 +23,6 @@ func TestClient(t *testing.T) { assert.NoError(t, err) assert.NotEmpty(t, packet) - start := time.Now() bep44Put, err := dht.CreatePKARRPublishRequest(sk, *packet) assert.NoError(t, err) assert.NotEmpty(t, bep44Put) diff --git a/impl/pkg/server/server.go b/impl/pkg/server/server.go index c09678cc..e2c046c1 100644 --- a/impl/pkg/server/server.go +++ b/impl/pkg/server/server.go @@ -11,6 +11,7 @@ import ( "github.com/sirupsen/logrus" swaggerfiles "github.com/swaggo/files" ginswagger "github.com/swaggo/gin-swagger" + "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin" "github.com/TBD54566975/did-dht-method/config" "github.com/TBD54566975/did-dht-method/docs" @@ -97,6 +98,7 @@ func setupHandler(env config.Environment) *gin.Engine { gin.Recovery(), gin.Logger(), gin.ErrorLogger(), + otelgin.Middleware(config.ServiceName), CORS(), } handler := gin.New()