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

add oltp(http&grpc) trace support #2641

Merged
merged 5 commits into from
May 29, 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
22 changes: 19 additions & 3 deletions .github/workflows/build_and_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ for file in `find . -name go.mod`; do
fi
fi

# package example needs golang >= v1.18
# package example needs golang >= v1.19
if [ "example" = $(basename $dirpath) ]; then
if ! go version|grep -q "1.18"; then
if ! go version|grep -q "1.19"; then
echo "ignore example as go version: $(go version)"
continue 1
fi
Expand All @@ -31,7 +31,23 @@ for file in `find . -name go.mod`; do
# package cmd/gf needs golang >= v1.18
if [ "gf" = $(basename $dirpath) ]; then
if ! go version|grep -q "1.18"; then
echo "ignore example as go version: $(go version)"
echo "ignore cmd/gf as go version: $(go version)"
continue 1
fi
fi

# package otlpgrpc needs golang >= v1.20
if [ "otlpgrpc" = $(basename $dirpath) ]; then
if ! go version|grep -q "1.20"; then
echo "ignore otlpgrpc as go version: $(go version)"
continue 1
fi
fi

# package otlphttp needs golang >= v1.20
if [ "otlphttp" = $(basename $dirpath) ]; then
if ! go version|grep -q "1.20"; then
echo "ignore otlphttp as go version: $(go version)"
continue 1
fi
fi
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ jobs:

strategy:
matrix:
go-version: [ "1.15", "1.16", "1.17", "1.18" ]
go-version: [ "1.15", "1.16", "1.17", "1.18", "1.19", "1.20" ]
goarch: [ "386", "amd64" ]

steps:
Expand Down
29 changes: 29 additions & 0 deletions contrib/trace/otlpgrpc/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module github.com/gogf/gf/contrib/trace/otlpgrpc/v2

go 1.20
houseme marked this conversation as resolved.
Show resolved Hide resolved

require (
github.com/gogf/gf/v2 v2.4.1
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0
go.opentelemetry.io/otel/sdk v1.16.0
google.golang.org/grpc v1.55.0
)

require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
492 changes: 492 additions & 0 deletions contrib/trace/otlpgrpc/go.sum

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions contrib/trace/otlpgrpc/otlpgrpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

// Package otlpgrpc provides gtrace.Tracer implementation using OpenTelemetry protocol.
package otlpgrpc

import (
"context"

"github.com/gogf/gf/v2/net/gipv4"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"google.golang.org/grpc"
)

const (
tracerHostnameTagKey = "hostname"
)

// Init initializes and registers `otlpgrpc` to global TracerProvider.
//
// The output parameter `Shutdown` is used for waiting exported trace spans to be uploaded,
// which is useful if your program is ending, and you do not want to lose recent spans.
func Init(serviceName, endpoint, traceToken string) (*sdktrace.TracerProvider, error) {
// Try retrieving host ip for tracing info.
var (
intranetIPArray, err = gipv4.GetIntranetIpArray()
hostIP = "NoHostIpFound"
)

if err != nil {
return nil, err
}

if len(intranetIPArray) == 0 {
if intranetIPArray, err = gipv4.GetIpArray(); err != nil {
return nil, err
}
}
if len(intranetIPArray) > 0 {
hostIP = intranetIPArray[0]
}

traceClient := otlptracegrpc.NewClient(
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(endpoint), // Replace the otel Agent Addr with the access point obtained in the prerequisite。
otlptracegrpc.WithHeaders(map[string]string{"Authentication": traceToken}),
otlptracegrpc.WithDialOption(grpc.WithBlock()))
ctx := context.Background()
traceExp, err := otlptrace.New(ctx, traceClient)
if err != nil {
return nil, err
}
res, err := resource.New(ctx,
resource.WithFromEnv(),
resource.WithProcess(),
resource.WithTelemetrySDK(),
resource.WithHost(),
resource.WithAttributes(
// The name of the service displayed on the traceback end。
semconv.ServiceNameKey.String(serviceName),
semconv.HostNameKey.String(hostIP),
attribute.String(tracerHostnameTagKey, hostIP),
),
)
if err != nil {
return nil, err
}

bsp := sdktrace.NewBatchSpanProcessor(traceExp)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(bsp),
)

// Set the global propagator to traceContext (not set by default).
otel.SetTextMapPropagator(propagation.TraceContext{})
otel.SetTracerProvider(tracerProvider)

return tracerProvider, nil
}
29 changes: 29 additions & 0 deletions contrib/trace/otlphttp/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module github.com/gogf/gf/contrib/trace/otlphttp/v2

go 1.20

require (
github.com/gogf/gf/v2 v2.4.1
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.16.0
go.opentelemetry.io/otel/sdk v1.16.0
)

require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
Loading