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

feat(workers): configure tail consumers #1317

Merged
merged 6 commits into from
Jun 23, 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
3 changes: 3 additions & 0 deletions .changelog/1317.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
workers: Add ability to specify tail Workers in script metadata
```
53 changes: 34 additions & 19 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ type CreateWorkerParams struct {
// ES Module syntax script.
Module bool

// Logpush opts the worker into Workers Logpush logging. A nil value leaves the current setting unchanged.
// https://developers.cloudflare.com/workers/platform/logpush/
// Logpush opts the worker into Workers Logpush logging. A nil value leaves
// the current setting unchanged.
//
// Documentation: https://developers.cloudflare.com/workers/platform/logpush/
Logpush *bool

// TailConsumers specifies a list of Workers that will consume the logs of
// the attached Worker.
// Documentation: https://developers.cloudflare.com/workers/platform/tail-workers/
TailConsumers *[]WorkersTailConsumer

// Bindings should be a map where the keys are the binding name, and the
// values are the binding content
Bindings map[string]WorkerBinding
Expand Down Expand Up @@ -90,17 +97,23 @@ type WorkerScript struct {
UsageModel string `json:"usage_model,omitempty"`
}

type WorkersTailConsumer struct {
Service string `json:"service"`
Environment *string `json:"environment,omitempty"`
}

// WorkerMetaData contains worker script information such as size, creation & modification dates.
type WorkerMetaData struct {
ID string `json:"id,omitempty"`
ETAG string `json:"etag,omitempty"`
Size int `json:"size,omitempty"`
CreatedOn time.Time `json:"created_on,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty"`
Logpush *bool `json:"logpush,omitempty"`
LastDeployedFrom *string `json:"last_deployed_from,omitempty"`
DeploymentId *string `json:"deployment_id,omitempty"`
PlacementMode *PlacementMode `json:"placement_mode,omitempty"`
ID string `json:"id,omitempty"`
ETAG string `json:"etag,omitempty"`
Size int `json:"size,omitempty"`
CreatedOn time.Time `json:"created_on,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty"`
Logpush *bool `json:"logpush,omitempty"`
TailConsumers *[]WorkersTailConsumer `json:"tail_consumers,omitempty"`
LastDeployedFrom *string `json:"last_deployed_from,omitempty"`
DeploymentId *string `json:"deployment_id,omitempty"`
PlacementMode *PlacementMode `json:"placement_mode,omitempty"`
}

// WorkerListResponse wrapper struct for API response to worker script list API call.
Expand Down Expand Up @@ -251,7 +264,7 @@ func (api *API) UploadWorker(ctx context.Context, rc *ResourceContainer, params
err error
)

if params.Module || params.Logpush != nil || params.Placement != nil || len(params.Bindings) > 0 || params.CompatibilityDate != "" || len(params.CompatibilityFlags) > 0 {
if params.Module || params.Logpush != nil || params.Placement != nil || len(params.Bindings) > 0 || params.CompatibilityDate != "" || len(params.CompatibilityFlags) > 0 || params.TailConsumers != nil {
contentType, body, err = formatMultipartBody(params)
if err != nil {
return WorkerScriptResponse{}, err
Expand Down Expand Up @@ -285,16 +298,18 @@ func formatMultipartBody(params CreateWorkerParams) (string, []byte, error) {
// Write metadata part
var scriptPartName string
meta := struct {
BodyPart string `json:"body_part,omitempty"`
MainModule string `json:"main_module,omitempty"`
Bindings []workerBindingMeta `json:"bindings"`
Logpush *bool `json:"logpush,omitempty"`
CompatibilityDate string `json:"compatibility_date,omitempty"`
CompatibilityFlags []string `json:"compatibility_flags,omitempty"`
Placement *Placement `json:"placement,omitempty"`
BodyPart string `json:"body_part,omitempty"`
MainModule string `json:"main_module,omitempty"`
Bindings []workerBindingMeta `json:"bindings"`
Logpush *bool `json:"logpush,omitempty"`
TailConsumers *[]WorkersTailConsumer `json:"tail_consumers,omitempty"`
CompatibilityDate string `json:"compatibility_date,omitempty"`
CompatibilityFlags []string `json:"compatibility_flags,omitempty"`
Placement *Placement `json:"placement,omitempty"`
}{
Bindings: make([]workerBindingMeta, 0, len(params.Bindings)),
Logpush: params.Logpush,
TailConsumers: params.TailConsumers,
CompatibilityDate: params.CompatibilityDate,
CompatibilityFlags: params.CompatibilityFlags,
Placement: params.Placement,
Expand Down
Loading