Skip to content

Commit

Permalink
[chore] support multiple required headers and resolve PR findings
Browse files Browse the repository at this point in the history
  • Loading branch information
niwoerner committed Dec 16, 2024
1 parent c9d5be1 commit 585b7de
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 29 deletions.
14 changes: 13 additions & 1 deletion receiver/gitlabreceiver/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
# GitLab Receiver

<!-- status autogenerated section -->
| Status | |
| ------------- |-----------|
| Stability | [development]: traces |
| Distributions | [] |
| Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Areceiver%2Fgitlab%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Areceiver%2Fgitlab) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Areceiver%2Fgitlab%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Areceiver%2Fgitlab) |
| [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@adrielp](https://www.github.com/adrielp), [@atoulme](https://www.github.com/atoulme) |

[development]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-stability.md#development
[contrib]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib
<!-- end autogenerated section -->

## Traces - Getting Started

Workflow tracing support is actively being added to the GitLab receiver.
This is accomplished through the processing of GitLab webhook
events for pipelines. The [`pipeline`](https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#pipeline-events) event payloads are then constructed into `trace`
telemetry.

Each GitLab pipeline, along with it's jobs, are converted
Each GitLab pipeline, along with its jobs, is converted
into trace spans, allowing the observation of workflow execution times,
success, and failure rates.

Expand Down
22 changes: 10 additions & 12 deletions receiver/gitlabreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configopaque"
"go.uber.org/multierr"
)

Expand All @@ -33,16 +34,11 @@ type Config struct {
}

type WebHook struct {
confighttp.ServerConfig `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
Path string `mapstructure:"path"` // path for data collection. Default is /events
HealthPath string `mapstructure:"health_path"` // path for health check api. Default is /health_check
RequiredHeader RequiredHeader `mapstructure:"required_header"` // optional setting to set a required header for all requests to have
Secret string `mapstructure:"secret"` // secret for webhook
}

type RequiredHeader struct {
Key string `mapstructure:"key"`
Value string `mapstructure:"value"`
confighttp.ServerConfig `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
Path string `mapstructure:"path"` // path for data collection. Default is /events
HealthPath string `mapstructure:"health_path"` // path for health check api. Default is /health_check
RequiredHeaders map[string]configopaque.String `mapstructure:"required_headers"` // optional setting to set one or more required headers for all requests to have
Secret string `mapstructure:"secret"` // secret for webhook
}

func createDefaultConfig() component.Config {
Expand Down Expand Up @@ -72,8 +68,10 @@ func (cfg *Config) Validate() error {
errs = multierr.Append(errs, errWriteTimeoutExceedsMaxValue)
}

if (cfg.WebHook.RequiredHeader.Key != "" && cfg.WebHook.RequiredHeader.Value == "") || (cfg.WebHook.RequiredHeader.Value != "" && cfg.WebHook.RequiredHeader.Key == "") {
errs = multierr.Append(errs, errRequiredHeader)
for key, value := range cfg.WebHook.RequiredHeaders {
if key == "" || value == "" {
errs = multierr.Append(errs, errRequiredHeader)
}
}

return errs
Expand Down
11 changes: 8 additions & 3 deletions receiver/gitlabreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/otelcol/otelcoltest"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/gitlabreceiver/internal/metadata"
Expand Down Expand Up @@ -62,9 +63,8 @@ func TestLoadConfig(t *testing.T) {
},
Path: "some/path",
HealthPath: "health/path",
RequiredHeader: RequiredHeader{
Key: "key-present",
Value: "value-present",
RequiredHeaders: map[string]configopaque.String{
"key-present": "value-present",
},
},
}
Expand All @@ -73,6 +73,11 @@ func TestLoadConfig(t *testing.T) {

assert.Equal(t, expectedConfig, r0)

expectedConfig.WebHook.RequiredHeaders = map[string]configopaque.String{
"key-present": "value-present",
"key2-present": "value2-present",
}

r1 := cfg.Receivers[component.NewIDWithName(metadata.Type, "customname")].(*Config)

assert.Equal(t, expectedConfig, r1)
Expand Down
3 changes: 1 addition & 2 deletions receiver/gitlabreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ package gitlabreceiver // import "github.com/open-telemetry/opentelemetry-collec
import (
"context"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/gitlabreceiver/internal/metadata"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/gitlabreceiver/internal/metadata"
)

func createTracesReceiver(_ context.Context, params receiver.Settings, cfg component.Config, consumer consumer.Traces) (receiver.Traces, error) {
Expand Down
3 changes: 1 addition & 2 deletions receiver/gitlabreceiver/generated_package_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion receiver/gitlabreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ status:
class: receiver
stability:
development: [traces]
distributions: [contrib]
distributions: []
codeowners:
active: [adrielp, atoulme]
tests:
config:
webhook:
endpoint: "localhost:8080"
13 changes: 6 additions & 7 deletions receiver/gitlabreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ receivers:
write_timeout: "500ms"
path: "some/path"
health_path: "health/path"
required_header:
key: key-present
value: value-present
required_headers:
key-present: value-present

gitlab/customname:
webhook:
Expand All @@ -17,10 +16,10 @@ receivers:
write_timeout: "500ms"
path: "some/path"
health_path: "health/path"
required_header:
key: key-present
value: value-present

required_headers:
key-present: value-present
key2-present: value2-present
processors:
nop:

Expand Down
2 changes: 1 addition & 1 deletion receiver/gitlabreceiver/traces_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"go.uber.org/zap"
)

const healthyResponse = `{"text": "Gitlab receiver webhook is healthy"}`
const healthyResponse = `{"text": "GitLab receiver webhook is healthy"}`

type gitlabTracesReceiver struct {
cfg *Config
Expand Down

0 comments on commit 585b7de

Please sign in to comment.