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

Integration with web architecture #1727

Closed
gaoshenga opened this issue Feb 6, 2023 · 8 comments
Closed

Integration with web architecture #1727

gaoshenga opened this issue Feb 6, 2023 · 8 comments
Labels
needs more info An issue that may be a bug or useful feature, but requires more information question

Comments

@gaoshenga
Copy link

Hello, how do you integrate benthos with the web framework?

@mihaitodor
Copy link
Collaborator

Hey @gaoshenga, I'm not sure I have enough information to answer your question. Which web framework are you referring to? I'm guessing you might be interested in building a custom http_server input which you can do by importing Benthos as a library and adding your own input plugin as shown in this repo.

@mihaitodor mihaitodor added question needs more info An issue that may be a bug or useful feature, but requires more information labels Feb 7, 2023
@gaoshenga
Copy link
Author

Hello @mihaitodor, thank you for your help. I'm ready to integrate the benthos plug-in with the GIN framework. I want to start the benthos instance by calling the bentho method to read the local configuration file. Instead of running a command like the benthos command "benthos -c./config.yaml" to start.

@mihaitodor
Copy link
Collaborator

OK, have a look at the StreamBuilderConfig example here which I think covers what you’re looking for.

@gaoshenga
Copy link
Author

Thank you. I tried the case you provided just now. This case is the result of just one run. I want to execute a local file through benthos, and then launch a benthos instance. Is there any other way to do it?

@mihaitodor
Copy link
Collaborator

mihaitodor commented Feb 8, 2023

Not sure I follow... I think that example does exactly what you're asking for, assuming the input that you configure for Benthos terminates (basically stream.Run() returns) after processing all its data, like, for example, the file or the generate ones.

@gaoshenga
Copy link
Author

Thank you very much. Your answer has given me a deeper understanding. It seems impossible to monitor the state of the stream with StreamBuilderConfig. It would be nice if we could monitor the state of streams like the streams rest api.

@mihaitodor
Copy link
Collaborator

Not sure what issue you bumped into, but here's a slightly modified version of that example I linked above:

package main

import (
	"context"

	"github.com/benthosdev/benthos/v4/public/service"

	// Import only required Benthos components, switch with `components/all` for
	// all standard components.
	_ "github.com/benthosdev/benthos/v4/public/components/io"
	_ "github.com/benthosdev/benthos/v4/public/components/pure"
        _ "github.com/benthosdev/benthos/v4/public/components/prometheus"
)

func main() {
	panicOnErr := func(err error) {
		if err != nil {
			panic(err)
		}
	}

	builder := service.NewStreamBuilder()

	// Set the full Benthos configuration of the stream.
	err := builder.SetYAML(`
http:
  address: 0.0.0.0:4195
  enabled: true

input:
  generate:
    count: 0
    interval: 1s
    mapping: 'root = "hello world"'

  processors:
    - mapping: 'root = content().uppercase()'

output:
  stdout: {}

logger:
  level: none

metrics:
  prometheus: {}
`)
	panicOnErr(err)

	// Build a stream with our configured components.
	stream, err := builder.Build()
	panicOnErr(err)

	// And run it, blocking until it gracefully terminates once the generate
	// input has generated a message and it has flushed through the stream.
	err = stream.Run(context.Background())
	panicOnErr(err)

}

If I run this, I'm able to do curl http://localhost:4195/metrics and it spews out the Prometheus metrics. Note that I also added the prometheus component import from here https://github.com/benthosdev/benthos/blob/main/public/components/all/package.go, but you can always add all of them by importing github.com/benthosdev/benthos/v4/public/components/all if you don't wish to cherry-pick only what you need.

@gaoshenga
Copy link
Author

I see. I want to create a web project that can configure multiple processing data streams through the front page, each stream being a separate configuration file. You can start, stop, and monitor the status of each stream dynamically via benthos. This is my first experience with benthos. Thank you for your wonderful answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info An issue that may be a bug or useful feature, but requires more information question
Projects
None yet
Development

No branches or pull requests

3 participants