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

Update go-client - new telemetry #1330

Merged
merged 6 commits into from May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/pkg/service/buffer/dependencies/dependencies.go
Expand Up @@ -46,8 +46,8 @@ func NewServiceDeps(

// Create base HTTP client for all API requests to other APIs
httpClient := httpclient.New(
httpclient.WithTelemetry(tel),
httpclient.WithUserAgent(userAgent),
httpclient.WithEnvs(envs),
func(c *httpclient.Config) {
Comment on lines 48 to 51
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

httpclient je pkg v kac repe, factory pre go-client klienta.

Pridavam tu podporu telemetri, ... envs sa nikde uz nepouzivali, tak som ich vyhodil.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Niekedy sme mali envs na roznych miestach v kode, ... teraz ma kazda service uz svoj config, ale tu som envs zabudol zmazat.

if cfg.Debug {
httpclient.WithDebugOutput(logger.DebugWriter())(c)
Expand Down
70 changes: 26 additions & 44 deletions internal/pkg/service/common/httpclient/httpclient.go
Expand Up @@ -3,37 +3,33 @@ package httpclient

import (
"io"
"net/http"
"strings"

"github.com/keboola/go-client/pkg/client"
ddHttp "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"github.com/keboola/go-client/pkg/client/trace"
"github.com/keboola/go-client/pkg/client/trace/otel"
"go.opentelemetry.io/contrib/propagators/b3"

"github.com/keboola/keboola-as-code/internal/pkg/env"
"github.com/keboola/keboola-as-code/internal/pkg/telemetry/oteldd"
"github.com/keboola/keboola-as-code/internal/pkg/utils/strhelper"
"github.com/keboola/keboola-as-code/internal/pkg/telemetry"
)

type Config struct {
userAgent string
envs env.Provider
telemetry telemetry.Telemetry
debugWriter io.Writer
dumpWriter io.Writer
}

type Option func(c *Config)

func WithEnvs(envs env.Provider) Option {
func WithUserAgent(v string) Option {
return func(c *Config) {
c.envs = envs
c.userAgent = v
}
}

func WithUserAgent(v string) Option {
func WithTelemetry(v telemetry.Telemetry) Option {
return func(c *Config) {
c.userAgent = v
c.telemetry = v
}
}

Expand All @@ -59,48 +55,34 @@ func New(opts ...Option) client.Client {
// Force HTTP2 transport
transport := client.HTTP2Transport()

// DataDog low-level tracing (raw http requests)
if conf.envs != nil && oteldd.IsDataDogEnabled(conf.envs) {
transport = ddHttp.WrapRoundTripper(
transport,
ddHttp.WithBefore(func(request *http.Request, span ddtrace.Span) {
// We use "http.request" operation name for request to the API,
// so requests to other API must have different operation name.
span.SetOperationName("kac.api.client.http.request")
span.SetTag("http.host", request.URL.Host)
if dotPos := strings.IndexByte(request.URL.Host, '.'); dotPos > 0 {
// E.g. connection, encryption, scheduler ...
span.SetTag("http.hostPrefix", request.URL.Host[:dotPos])
}
span.SetTag(ext.EventSampleRate, 1.0)
span.SetTag(ext.HTTPURL, request.URL.Redacted())
span.SetTag("http.path", request.URL.Path)
span.SetTag("http.query", request.URL.Query().Encode())
}),
ddHttp.RTWithResourceNamer(func(r *http.Request) string {
// Set resource name to request path
return strhelper.MustURLPathUnescape(r.URL.RequestURI())
}))
}

// Create client
cl := client.New().
WithTransport(transport).
WithUserAgent(conf.userAgent)

// Enable telemetry
if conf.telemetry != nil {
cl = cl.WithTelemetry(
conf.telemetry.TracerProvider(),
conf.telemetry.MeterProvider(),
otel.WithRedactedHeaders("X-StorageAPI-Token", "X-KBC-ManageApiToken"),
otel.WithPropagators(
// DataDog supports multiple propagations: tracecontext, B3, legacy DataDog, ...
// W3C tracecontext propagator (propagation.TraceContext{}) is not working with the Storage API dd-trace-php ,
// so the B3 propagator is used.
b3.New(b3.WithInjectEncoding(b3.B3MultipleHeader)),
),
)
}

Comment on lines +63 to +77
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Namiesto DD telemetrie (low/high) sa pouzije telemetria, ktora je uz v go-client.
Management Token sa nikde nepouziva, nevolame management API, ale dal som ho sem preventivne.

// Log each HTTP client request/response as debug message
if conf.debugWriter != nil {
cl = cl.AndTrace(client.LogTracer(conf.debugWriter))
cl = cl.AndTrace(trace.LogTracer(conf.debugWriter))
}

// Dump each HTTP client request/response body
if conf.dumpWriter != nil {
cl = cl.AndTrace(client.DumpTracer(conf.dumpWriter))
}

// DataDog high-level tracing (api client requests)
if conf.envs != nil && oteldd.IsDataDogEnabled(conf.envs) {
cl = cl.AndTrace(oteldd.DDTraceFactory())
cl = cl.AndTrace(trace.DumpTracer(conf.dumpWriter))
}

return cl
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/service/templates/api/dependencies/service.go
Expand Up @@ -46,8 +46,8 @@ func NewServiceDeps(

// Create base HTTP client for all API requests to other APIs
httpClient := httpclient.New(
httpclient.WithTelemetry(tel),
httpclient.WithUserAgent(userAgent),
httpclient.WithEnvs(envs),
func(c *httpclient.Config) {
if cfg.Debug {
httpclient.WithDebugOutput(logger.DebugWriter())(c)
Expand Down
118 changes: 0 additions & 118 deletions internal/pkg/telemetry/oteldd/httpclient.go

This file was deleted.