Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
612 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
content/post/2023-09-19-tracing-is-better/example-api-0.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
func PrepareContainer(ctx context.Context, container ContainerContext, locales []string, dryRun bool, allLocalesRequired bool) (*StatusResult, error) { | ||
|
||
logger.Info(`Filling home page template`) | ||
|
||
homePage, err := RenderPage(ctx, home, container, locales, allLocalesRequired) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
templateIds := []string{homePage.ID} | ||
|
||
if container.PageSlugs.FAQ != "" { | ||
faqPage, err := RenderPage(ctx, faq, container, locales, allLocalesRequired) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
templateIds = append(templateIds, faqPage.ID) | ||
} | ||
|
||
if dryRun { | ||
return &StatusResult{Status: StatusDryRun}, nil | ||
} | ||
|
||
logger.Info(`Marking page template(s) for usage`, "template_ids", templateIds) | ||
|
||
if err := MarkReadyForUsage(ctx, container, templateIds); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &StatusResult{Status: StatusComplete}, nil | ||
} | ||
|
||
func RenderPage(ctx context.Context, source Source, container ContainerContext, locales []string, allLocalesRequired bool) (string, error) { | ||
|
||
logger.Info(fmt.Sprintf(`Filling %s page template`, source.Name)) | ||
|
||
template, err := FetchAndFillTemplate(ctx, source, container, locales) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
page, err := ConfigureFromTemplate(ctx, container, template, locales) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(page.Locales) != len(locales) { | ||
const message = fmt.Sprintf(`Failed to render %s page template for some locales`, source.Name) | ||
if allLocalesRequired { | ||
return nil, fmt.Errorf(message) | ||
} else { | ||
logger.Warn(message, "locales", locales, "pages", page.Locales) | ||
} | ||
} | ||
|
||
return page, nil | ||
} |
62 changes: 62 additions & 0 deletions
62
content/post/2023-09-19-tracing-is-better/example-api-1.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
var tr = otel.Tracer("container_api") | ||
|
||
func PrepareContainer(ctx context.Context, container ContainerContext, locales []string, dryRun bool, allLocalesRequired bool) (*StatusResult, error) { | ||
ctx, span := tr.Start(ctx, "prepare_container") | ||
defer span.End() | ||
|
||
homePage, err := RenderPage(ctx, home, container, locales, allLocalesRequired) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
templateIds := []string{homePage.ID} | ||
|
||
if container.PageSlugs.FAQ != "" { | ||
faqPage, err := RenderPage(ctx, faq, container, locales, allLocalesRequired) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
templateIds = append(templateIds, faqPage.ID) | ||
} | ||
|
||
if dryRun { | ||
return &StatusResult{Status: StatusDryRun}, nil | ||
} | ||
|
||
logger.Info(`Marking page template(s) for usage`, "template_ids", templateIds) | ||
|
||
if err := MarkReadyForUsage(ctx, container, templateIds); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &StatusResult{Status: StatusComplete}, nil | ||
} | ||
|
||
func RenderPage(ctx context.Context, source Source, container ContainerContext, locales []string, allLocalesRequired bool) (string, error) { | ||
ctx, span := tr.Start(ctx, "render_page") | ||
defer span.End() | ||
|
||
logger.Info(fmt.Sprintf(`Filling %s page template`, source.Name)) | ||
|
||
template, err := FetchAndFillTemplate(ctx, source, container, locales) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
page, err := ConfigureFromTemplate(ctx, container, template, locales) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(page.Locales) != len(locales) { | ||
const message = fmt.Sprintf(`Failed to render %s page template for some locales`, source.Name) | ||
if allLocalesRequired { | ||
return nil, fmt.Errorf(message) | ||
} else { | ||
logger.Warn(message, "locales", locales, "pages", page.Locales) | ||
} | ||
} | ||
|
||
return page, nil | ||
} |
62 changes: 62 additions & 0 deletions
62
content/post/2023-09-19-tracing-is-better/example-api-2.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
var tr = otel.Tracer("container_api") | ||
|
||
func PrepareContainer(ctx context.Context, container ContainerContext, locales []string, dryRun bool, allLocalesRequired bool) (*StatusResult, error) { | ||
ctx, span := tr.Start(ctx, "prepare_container") | ||
defer span.End() | ||
|
||
homePage, err := RenderPage(ctx, home, container, locales, allLocalesRequired) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
templateIds := []string{homePage.ID} | ||
|
||
if container.PageSlugs.FAQ != "" { | ||
faqPage, err := RenderPage(ctx, faq, container, locales, allLocalesRequired) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
templateIds = append(templateIds, faqPage.ID) | ||
} | ||
|
||
if dryRun { | ||
return &StatusResult{Status: StatusDryRun}, nil | ||
} | ||
|
||
logger.Info(`Marking page template(s) for usage`, "template_ids", templateIds) | ||
|
||
if err := MarkReadyForUsage(ctx, container, templateIds); err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
return &StatusResult{Status: StatusComplete}, nil | ||
} | ||
|
||
func RenderPage(ctx context.Context, source Source, container ContainerContext, locales []string, allLocalesRequired bool) (string, error) { | ||
ctx, span := tr.Start(ctx, "render_page") | ||
defer span.End() | ||
|
||
logger.Info(fmt.Sprintf(`Filling %s page template`, source.Name)) | ||
|
||
template, err := FetchAndFillTemplate(ctx, source, container, locales) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
page, err := ConfigureFromTemplate(ctx, container, template, locales) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
if len(page.Locales) != len(locales) { | ||
const message = fmt.Sprintf(`Failed to render %s page template for some locales`, source.Name) | ||
if allLocalesRequired { | ||
return nil, tracing.Errorf(message) | ||
} else { | ||
logger.Warn(message, "locales", locales, "pages", page.Locales) | ||
} | ||
} | ||
|
||
return page, nil | ||
} |
69 changes: 69 additions & 0 deletions
69
content/post/2023-09-19-tracing-is-better/example-api-3.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
var tr = otel.Tracer("container_api") | ||
|
||
func PrepareContainer(ctx context.Context, container ContainerContext, locales []string, dryRun bool, allLocalesRequired bool) (*StatusResult, error) { | ||
ctx, span := tr.Start(ctx, "prepare_container") | ||
defer span.End() | ||
|
||
tracing.StringSlice(span, "locales", locales) | ||
tracing.Bool(span, "dry_run", dryRun) | ||
tracing.Bool(span, "locales_mandatory", allLocalesRequired) | ||
|
||
homePage, err := RenderPage(ctx, home, container, locales, allLocalesRequired) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
templateIds := []string{homePage.ID} | ||
|
||
hasFaq := container.PageSlugs.FAQ != "" | ||
tracing.Bool(span, "has_faq", hasFaq) | ||
|
||
if hasFaq { | ||
faqPage, err := RenderPage(ctx, faq, container, locales, allLocalesRequired) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
templateIds = append(templateIds, faqPage.ID) | ||
} | ||
|
||
tracing.StringSlice(span, "template_ids", templateIds) | ||
|
||
if dryRun { | ||
return &StatusResult{Status: StatusDryRun}, nil | ||
} | ||
|
||
if err := MarkReadyForUsage(ctx, container, templateIds); err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
return &StatusResult{Status: StatusComplete}, nil | ||
} | ||
|
||
func RenderPage(ctx context.Context, source Source, container ContainerContext, locales []string, allLocalesRequired bool) (string, error) { | ||
ctx, span := tr.Start(ctx, "render_page") | ||
defer span.End() | ||
|
||
tracing.String(span, "source_name", source.Name) | ||
|
||
template, err := FetchAndFillTemplate(ctx, source, container, locales) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
page, err := ConfigureFromTemplate(ctx, container, template, locales) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
allLocalesRendered := len(page.Locales) == len(locales) | ||
|
||
tracing.Bool(span, "all_locales_rendered", allLocalesRendered) | ||
tracing.StringSlice(span, "locales_rendered", page.Locales) | ||
|
||
if !allLocalesRendered && allLocalesRequired { | ||
return nil, tracing.Errorf(`Failed to render %s page template for some locales`, source.Name) | ||
} | ||
|
||
return page, nil | ||
} |
69 changes: 69 additions & 0 deletions
69
content/post/2023-09-19-tracing-is-better/example-api-4.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
var tr = otel.Tracer("container_api") | ||
|
||
func PrepareContainer(ctx context.Context, container ContainerContext, locales []string, dryRun bool, allLocalesRequired bool) (*StatusResult, error) { | ||
ctx, span := tr.Start(ctx, "prepare_container") | ||
defer span.End() | ||
|
||
tracing.StringSlice(span, "locales", locales) | ||
tracing.Bool(span, "dry_run", dryRun) | ||
tracing.Bool(span, "locales_mandatory", allLocalesRequired) | ||
|
||
homePage, err := RenderPage(ctx, home, container, locales, allLocalesRequired) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
templateIds := []string{homePage.ID} | ||
|
||
hasFaq := container.PageSlugs.FAQ != "" | ||
tracing.Bool(span, "has_faq", hasFaq) | ||
|
||
if hasFaq { | ||
faqPage, err := RenderPage(ctx, faq, container, locales, allLocalesRequired) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
templateIds = append(templateIds, faqPage.ID) | ||
} | ||
|
||
tracing.StringSlice(span, "template_ids", templateIds) | ||
|
||
if dryRun { | ||
return &StatusResult{Status: StatusDryRun}, nil | ||
} | ||
|
||
if err := MarkReadyForUsage(ctx, container, templateIds); err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
return &StatusResult{Status: StatusComplete}, nil | ||
} | ||
|
||
func RenderPage(ctx context.Context, source Source, container ContainerContext, locales []string, allLocalesRequired bool) (string, error) { | ||
ctx, span := tr.Start(ctx, "render_page") | ||
defer span.End() | ||
|
||
tracing.String(span, "source_name", source.Name) | ||
|
||
template, err := FetchAndFillTemplate(ctx, source, container, locales) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
page, err := ConfigureFromTemplate(ctx, container, template, locales) | ||
if err != nil { | ||
return nil, tracing.Error(span, err) | ||
} | ||
|
||
allLocalesRendered := len(page.Locales) == len(locales) | ||
|
||
tracing.Bool(span, "all_locales_rendered", allLocalesRendered) | ||
tracing.StringSlice(span, "locales_rendered", page.Locales) | ||
|
||
if !allLocalesRendered && required { | ||
return nil, tracing.Errorf(`Failed to render %s page template for some locales`, source.Name) | ||
} | ||
|
||
return page, nil | ||
} |
Oops, something went wrong.