Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] support jaeger telemetry #317

Merged
merged 9 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions command/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
GraphQLAddressFlag = "graphql-address"
PprofFlag = "pprof"
PprofAddressFlag = "pprof-address"
JaegerFlag = "jaeger"
JaegerAddressFlag = "jaeger-address"
)

// Legacy flag that needs to be present to preserve backwards
Expand Down
30 changes: 30 additions & 0 deletions command/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,36 @@ func GetPprofFlag(cmd *cobra.Command) bool {
return v
}

// RegustreTelemetryFlag registers the telemetry flags
func RegisterJaegerFlag(cmd *cobra.Command) {
cmd.PersistentFlags().Bool(
command.JaegerFlag,
false,
"enable the jaeger telemetry",
)

cmd.PersistentFlags().String(
command.JaegerAddressFlag,
fmt.Sprintf("http://%s:%d/api/traces", LocalHostBinding, server.DefaultJaegerPort),
"jaeger thrift protocol (HTTP POST to /api/traces)",
)
}

// GetJaegerFlag extracts the telemetry flag
func GetJaegerFlag(cmd *cobra.Command) (bool, string) {
enableJaeger, err := cmd.Flags().GetBool(command.JaegerFlag)
if err != nil && enableJaeger {
addr, err := cmd.Flags().GetString(command.JaegerAddressFlag)
if err != nil {
return false, ""
}

return true, addr
}

return false, ""
}

// ParseGraphQLAddress parses the passed in GraphQL address
func ParseGraphQLAddress(graphqlAddress string) (*url.URL, error) {
return url.ParseRequestURI(graphqlAddress)
Expand Down
3 changes: 3 additions & 0 deletions command/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Config struct {
type Telemetry struct {
PrometheusAddr string `json:"prometheus_addr"`
EnableIOTimer bool `json:"prometheus_enable_disk_io_timer"`
EnableJaeger bool `json:"enable_jaeger"`
JaegerURL string `json:"jaeger_url"`
}

// Network defines the network configuration params
Expand Down Expand Up @@ -91,6 +93,7 @@ func DefaultConfig() *Config {
},
Telemetry: &Telemetry{
EnableIOTimer: false,
EnableJaeger: false,
},
ShouldSeal: false,
TxPool: &TxPool{
Expand Down
2 changes: 2 additions & 0 deletions command/server/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ func (p *serverParams) generateConfig() *server.Config {
Telemetry: &server.Telemetry{
PrometheusAddr: p.prometheusAddress,
EnableIOMetrics: p.prometheusIOMetrics,
EnableJaeger: p.rawConfig.Telemetry.EnableJaeger,
JaegerURL: p.rawConfig.Telemetry.JaegerURL,
},
Network: &network.Config{
NoDiscover: p.rawConfig.Network.NoDiscover,
Expand Down
7 changes: 7 additions & 0 deletions command/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ func setDevFlags(cmd *cobra.Command) {
)

helper.RegisterPprofFlag(cmd)
helper.RegisterJaegerFlag(cmd)

_ = cmd.Flags().MarkHidden(devIntervalFlag)
}
Expand Down Expand Up @@ -482,6 +483,12 @@ func runCommand(cmd *cobra.Command, _ []string) {
// pprof flag
params.rawConfig.EnablePprof = helper.GetPprofFlag(cmd)

// jaeger flag
if enableJaeger, jaegerURL := helper.GetJaegerFlag(cmd); enableJaeger {
params.rawConfig.Telemetry.EnableJaeger = enableJaeger
params.rawConfig.Telemetry.JaegerURL = jaegerURL
}

if err := runServerLoop(params.generateConfig(), outputter); err != nil {
outputter.SetError(err)
outputter.WriteOutput()
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/prometheus/client_golang v1.13.0
github.com/ryanuber/columnize v2.1.2+incompatible
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.2
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/umbracle/go-eth-bn256 v0.0.0-20190607160430-b36caf4e0f6b
github.com/umbracle/go-web3 v0.0.0-20220224145938-aaa1038c1b69
Expand Down Expand Up @@ -79,9 +79,14 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.18.3 // indirect
github.com/aws/smithy-go v1.13.5 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/marten-seemann/qtls-go1-19 v0.1.0 // indirect
github.com/mattn/go-pointer v0.0.1 // indirect
github.com/multiformats/go-multicodec v0.5.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/sdk v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
)

require (
Expand Down Expand Up @@ -176,6 +181,7 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.44.0 // indirect
github.com/whyrusleeping/timecache v0.0.0-20160911033111-cfcb2f1abfee // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.14.0
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.7.0 // indirect
Expand Down
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
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-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
Expand Down Expand Up @@ -311,6 +313,7 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -792,6 +795,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -802,6 +807,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
Expand Down Expand Up @@ -846,7 +853,15 @@ 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.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
go.opentelemetry.io/otel v1.6.3/go.mod h1:7BgNga5fNlF/iZjG06hM3yofffp0ofKCDwSXx1GC4dI=
go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM=
go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
go.opentelemetry.io/otel/exporters/jaeger v1.14.0 h1:CjbUNd4iN2hHmWekmOqZ+zSCU+dzZppG8XsV+A3oc8Q=
go.opentelemetry.io/otel/exporters/jaeger v1.14.0/go.mod h1:4Ay9kk5vELRrbg5z4cpP9EtmQRFap2Wb0woPG4lujZA=
go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY=
go.opentelemetry.io/otel/sdk v1.14.0/go.mod h1:bwIC5TjrNG6QDCHNWvW4HLHtUQ4I+VQDsnjhvyZCALM=
go.opentelemetry.io/otel/trace v1.6.3/go.mod h1:GNJQusJlUgZl9/TQBPKU/Y/ty+0iVB5fjhKeJGZPGFs=
go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M=
go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
Expand Down
39 changes: 39 additions & 0 deletions helper/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math"
"net"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -216,3 +217,41 @@ func PadLeftOrTrim(bb []byte, size int) []byte {

return tmp
}

// Substr returns a substring of the input string
func Substr(input string, start int, size int) string {
strRunes := []rune(input)

if start < 0 {
start = 0
}

if start >= len(strRunes) {
return ""
}

if (start + size) > len(strRunes) {
size = len(strRunes) - start
}

return string(strRunes[start : start+size])
}

// GetOutboundIP returns the preferred outbound ip of this machine
func GetOutboundIP() (net.IP, error) {
// any public address will do
conn, err := net.Dial("udp", "1.1.1.1")
if err != nil {
return nil, err
}
defer conn.Close()

address, ok := conn.LocalAddr().(*net.UDPAddr)
if !ok {
return nil, errors.New("cannot assert UDPAddr")
}

ipaddress := address.IP

return ipaddress, nil
}
73 changes: 73 additions & 0 deletions helper/common/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package common

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_UnicodeSubstr(t *testing.T) {
textSimples := []string{
"",
"hello world",
"你好世界",
"こんにちは世界",
"Привіт Світ",
}

// start >= 0, size <= len(string)
{
results := []string{
"",
"he",
"你好",
"こん",
"Пр",
}

for i, text := range textSimples {
result := Substr(text, 0, 2)

assert.Equal(t, result, results[i])
}
}

// start < 0, size <= len(string)
{
results := []string{
"",
"he",
"你好",
"こん",
"Пр",
}

for i, text := range textSimples {
result := Substr(text, -1, 2)

assert.Equal(t, result, results[i])
}
}

// start >= 0, size > len(string)
{
results := textSimples

for i, text := range textSimples {
result := Substr(text, 0, 255)

assert.Equal(t, result, results[i])
}
}

// start < 0, size > len(string)
{
results := textSimples

for i, text := range textSimples {
result := Substr(text, -1, 255)

assert.Equal(t, result, results[i])
}
}
}
81 changes: 81 additions & 0 deletions helper/telemetry/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package telemetry

import (
"context"

"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

type contextLabel string
type contextValue string

const (
ContextNamespace contextLabel = "telemetry"
)

type Code codes.Code

const (
// Unset is the default status code
Unset Code = Code(codes.Unset)

// Error indicates the operation contains an error
Error Code = Code(codes.Error)

// Ok indicates operation has been validated by an Application developers
Ok Code = Code(codes.Ok)
)

type Span interface {
// SetAttribute set attribute (base type)
SetAttribute(label string, value interface{})

// SetAttributes set attributes
SetAttributes(attributes map[string]interface{})

// AddEvent adds an event
AddEvent(name string, attributes map[string]interface{})

// SetStatus set status
SetStatus(code Code, info string)

// RecordError will record err as an exception span event for this span. An
// additional call to SetStatus is required if the Status of the Span should
// be set to Error, as this method does not change the Span status. If this
// span is not being recorded or err is nil then this method does nothing.
// (wrapping otel.Span.RecordError)
RecordError(err error)

// End ends the span
End()

// SpanContext returns the span context
SpanContext() trace.SpanContext

// Context returns the context.Context (span warrapped)
Context() context.Context
}

// Tracer provides a tracer
type Tracer interface {
// Start starts a new span
Start(name string) Span

// StartWithParent starts a new span with a parent
StartWithParent(parent trace.SpanContext, name string) Span

// StartWithContext starts a new span with a parent from context
StartWithContext(ctx context.Context, name string) Span

// GetTraceProvider returns the trace provider
GetTraceProvider() TracerProvider
}

type TracerProvider interface {
// NewTracer creates a new tracer
NewTracer(namespace string) Tracer

// Shutdown shuts down the tracer provider
Shutdown(context.Context) error
}
Loading