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 traceparent header support #81

Merged
merged 2 commits into from
Dec 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func (c *Client) withMiddleware(wrapped http.RoundTripper) http.RoundTripper {
if c.UserAgent != nil {
req.Header.Set("User-Agent", *c.UserAgent)
}
if c.TraceParent != nil {
req.Header.Set("Traceparent", *c.TraceParent)
}

// log request and response
c.logRequest(ctx, req)
Expand Down
3 changes: 3 additions & 0 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type Config struct {

// optional managing id to tag Observe resources with
ManagingObjectID *string `json:"managing_object_id"`

// optional traceparent identifier to pass via header
TraceParent *string `json:"traceparent"`
}

func (c *Config) Hash() uint64 {
Expand Down
11 changes: 11 additions & 0 deletions observe/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ func Provider() *schema.Provider {
Optional: true,
Description: "ID of an Observe object that serves as the parent (managing) object for all resources created by the provider (internal use).",
},
"traceparent": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering whether it's worth adding validation to this field, since traceparent is not just an opaque ID and instead has multiple well-structured parts:

https://www.w3.org/TR/trace-context/#traceparent-header-field-values

The RFC says multiple times that "vendors MUST ignore" the traceparent if any of the fields are invalid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thought about this, and leaned against adding validation. Mainly as we are just passing the context through to the server. And the extract logic on the server does validate and ignore if the fields are invalid (essentially starts a new trace).

Copy link
Contributor

Choose a reason for hiding this comment

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

If it's exposed to users I tend to think more validation is better if it can turn a silent failure into an explicit error. By not officially exposing the option to users that safety/predictability imperative largely goes away. So 👍🏻 to no validation in combo with the move to env only.

Type: schema.TypeString,
DefaultFunc: schema.EnvDefaultFunc("W3C_TRACEPARENT", nil),
Copy link
Contributor

@bendrucker bendrucker Nov 30, 2023

Choose a reason for hiding this comment

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

Suggested change
DefaultFunc: schema.EnvDefaultFunc("W3C_TRACEPARENT", nil),
DefaultFunc: schema.EnvDefaultFunc("TRACEPARENT", nil),

I'm unable to find any cases where this is used as a configuration input (versus just internally to the code):

https://github.com/search?q=%22W3C_TRACEPARENT%22&type=code&p=1

I did find this:

open-telemetry/opentelemetry-specification#740 (comment)

Optional: true,
Description: "Optional traceparent identifie.",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Description: "Optional traceparent identifie.",
Description: "A trace identifier to attach to all HTTP requests in the traceparent header (https://www.w3.org/TR/trace-context/#traceparent-header)",

},
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -247,6 +253,11 @@ func getConfigureContextFunc(userAgent func() string) schema.ConfigureContextFun
config.ManagingObjectID = &managingId
}

if v, ok := data.GetOk("traceparent"); ok {
traceparent := v.(string)
config.TraceParent = &traceparent
}

// by omission, cache client
useCache := true
if v, ok := config.Flags[flagCacheClient]; ok {
Expand Down
Loading