### Install musl for Linux Binaries Source: https://github.com/rwynn/monstache/blob/rel6/docker/plugin/README.md If your host system is Linux, you may need to install `musl` to run the resulting binary. This command is for Debian-based systems. ```bash sudo apt install musl ``` -------------------------------- ### Initialize Go Module Source: https://github.com/rwynn/monstache/wiki/Go-plugin-guide Initialize a Go module for your project. This command should be run in the directory containing your Go source files. ```bash go mod init github.com/rwynn/monstache/v6 ``` -------------------------------- ### Build and Run Monstache Plugins Source: https://context7.com/rwynn/monstache/llms.txt Compile the Go plugin using buildmode=plugin and reference the resulting shared object in the Monstache configuration. ```bash # Build the plugin (must match monstache Go version) go build -buildmode=plugin -o myplugin.so plugin.go # Run monstache with the plugin monstache -f config.toml -mapper-plugin-path /path/to/myplugin.so ``` -------------------------------- ### Build Docker Image Source: https://github.com/rwynn/monstache/wiki/Go-plugin-guide Build the Docker image using the Dockerfile in the current directory. The image will be tagged as 'monstache'. ```bash docker build -t monstache . ``` -------------------------------- ### Run Monstache via Command Line Source: https://context7.com/rwynn/monstache/llms.txt Executes the Monstache binary with configuration files or command-line overrides. ```bash # Run with configuration file monstache -f /path/to/config.toml # Run with command-line overrides monstache -f config.toml \ -mongo-url "mongodb://localhost:27017" \ -elasticsearch-url "http://localhost:9200" \ -direct-read-namespace "mydb.users" \ -direct-read-namespace "mydb.orders" \ -resume \ -verbose # Print version monstache -v # Print parsed configuration monstache -f config.toml -print-config # Enable pprof profiling endpoints monstache -f config.toml -pprof # Use environment variable template in config monstache -f config.toml -tpl ``` -------------------------------- ### Set Environment Variables Source: https://context7.com/rwynn/monstache/llms.txt Configure connection settings via environment variables, including support for Docker secrets using the __FILE suffix. ```bash # Connection settings export MONSTACHE_MONGO_URL="mongodb://localhost:27017" export MONSTACHE_ES_URLS="http://es1:9200,http://es2:9200" export MONSTACHE_ES_USER="elastic" export MONSTACHE_ES_PASS="changeme" # Docker secrets pattern export MONSTACHE_ES_PASS__FILE="/run/secrets/es_password" ``` -------------------------------- ### Dockerfile for Building Monstache with Go Plugin Source: https://github.com/rwynn/monstache/wiki/Go-plugin-guide This Dockerfile compiles a Go plugin and then copies it into the Monstache base image. It uses multi-stage builds for efficiency. Ensure the plugin.go file is in the same directory. ```dockerfile FROM golang:1.24.4-alpine3.21 AS build WORKDIR /src RUN --mount=target=. \ --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/go/pkg \ go mod download; \ go build -buildmode=plugin -o out/plugin.so plugin.go FROM rwynn/monstache:6.7.22 COPY --from=build out/plugin.so /bin ``` -------------------------------- ### Configure Monstache via Environment Variables Source: https://context7.com/rwynn/monstache/llms.txt Set synchronization namespaces, AWS credentials, and cluster settings using shell environment variables. ```bash # Namespace configuration export MONSTACHE_DIRECT_READ_NS="mydb.users,mydb.orders" export MONSTACHE_CHANGE_STREAM_NS="mydb.users,mydb.orders" export MONSTACHE_NS_REGEX="^mydb\\." # AWS configuration for Amazon OpenSearch export MONSTACHE_AWS_ACCESS_KEY="AKIAIOSFODNN7EXAMPLE" export MONSTACHE_AWS_SECRET_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" export MONSTACHE_AWS_REGION="us-east-1" # Cluster and worker settings export MONSTACHE_CLUSTER="production-sync" export MONSTACHE_WORKER="worker-1" # Logging export MONSTACHE_LOG_DIR="/var/log/monstache" ``` -------------------------------- ### Build Monstache Binaries Source: https://github.com/rwynn/monstache/blob/rel6/docker/local/README.md Execute the build script to compile binaries for multiple platforms within a Docker container. ```bash darwin-amd64 linux-amd64 monstache-0e52792.zip windows-amd64 ``` -------------------------------- ### Deploy Monstache with Docker Source: https://context7.com/rwynn/monstache/llms.txt Containerize Monstache using custom plugins or standard docker-compose orchestration. ```dockerfile # Dockerfile for custom monstache with plugin FROM golang:1.24-alpine AS build WORKDIR /src COPY plugin.go go.mod ./ RUN go build -buildmode=plugin -o /plugin.so plugin.go FROM rwynn/monstache:6.8.0 COPY --from=build /plugin.so /bin/plugin.so COPY config.toml /etc/monstache/config.toml CMD ["-f", "/etc/monstache/config.toml", "-mapper-plugin-path", "/bin/plugin.so"] ``` ```yaml # docker-compose.yml version: '3.8' services: monstache: image: rwynn/monstache:6.8.0 command: -f /config.toml volumes: - ./config.toml:/config.toml:ro environment: - MONSTACHE_MONGO_URL=mongodb://mongo:27017 - MONSTACHE_ES_URLS=http://elasticsearch:9200 depends_on: - mongo - elasticsearch restart: unless-stopped ``` -------------------------------- ### Configure Monstache via TOML Source: https://context7.com/rwynn/monstache/llms.txt Defines connections, sync namespaces, and performance settings for the Monstache daemon. ```toml # monstache.config.toml - Complete configuration example # MongoDB connection mongo-url = "mongodb://username:password@localhost:27017" mongo-config-url = "mongodb://localhost:27019" # For sharded clusters # Elasticsearch connection elasticsearch-urls = ["http://localhost:9200"] elasticsearch-user = "elastic" elasticsearch-password = "changeme" elasticsearch-max-conns = 4 elasticsearch-max-bytes = 8388608 elasticsearch-max-seconds = 5 elasticsearch-retry = true # Change stream configuration (default: watch entire deployment) change-stream-namespaces = ["mydb.users", "mydb.orders"] # Direct reads for initial sync direct-read-namespaces = ["mydb.users", "mydb.orders"] direct-read-split-max = 9 direct-read-concur = 10 # Namespace filtering namespace-regex = "^mydb\\." namespace-exclude-regex = "^mydb\\.temp" # Resume configuration resume = true resume-name = "default" resume-strategy = 0 # 0=timestamp, 1=token # Cluster mode for high availability cluster-name = "monstache-cluster" # HTTP server for health checks enable-http-server = true http-server-addr = ":8080" # Performance options gzip = true stats = true verbose = true ``` -------------------------------- ### Run Monstache Container with Plugin Source: https://github.com/rwynn/monstache/wiki/Go-plugin-guide Run the custom Monstache Docker container. The '--net=host' flag is used for network access, and '-mapper-plugin-path' specifies the location of the embedded plugin. ```bash docker run --rm --net=host monstache:latest -mapper-plugin-path /bin/plugin.so ``` -------------------------------- ### Go Plugin for Document Transformation Source: https://github.com/rwynn/monstache/wiki/Go-plugin-guide This Go plugin transforms document values to uppercase. It requires the monstachemap package and the strings package. Ensure the plugin is placed in a folder with a Dockerfile. ```go package main import ( "github.com/rwynn/monstache/v6/monstachemap" "strings" ) // a plugin to convert document values to uppercase func Map(input *monstachemap.MapperPluginInput) (output *monstachemap.MapperPluginOutput, err error) { doc := input.Document for k, v := range doc { switch v.(type) { case string: doc[k] = strings.ToUpper(v.(string)) } } output = &monstachemap.MapperPluginOutput{Document: doc} return } ``` -------------------------------- ### Enable GridFS File Indexing Source: https://context7.com/rwynn/monstache/llms.txt Configure Monstache to index GridFS file content using the Elasticsearch ingest-attachment plugin. ```toml # Enable GridFS file indexing index-files = true file-namespaces = ["mydb.fs.files"] max-file-size = 10485760 # 10MB limit # Disable automatic pipeline creation disable-file-pipeline-put = false file-highlighting = true ``` -------------------------------- ### Develop a Go Plugin for Monstache Source: https://context7.com/rwynn/monstache/llms.txt Implement Map, Filter, and Process functions to transform documents, control indexing, and perform custom operations. Plugins must be compiled as shared objects. ```go // plugin.go - Go plugin for monstache package main import ( "strings" "github.com/rwynn/monstache/v6/monstachemap" ) // Map transforms documents before indexing to Elasticsearch func Map(input *monstachemap.MapperPluginInput) (output *monstachemap.MapperPluginOutput, err error) { doc := input.Document // Transform document fields if name, ok := doc["name"].(string); ok { doc["name"] = strings.ToUpper(name) } // Add computed fields doc["database"] = input.Database doc["collection"] = input.Collection // Custom index routing output = &monstachemap.MapperPluginOutput{ Document: doc, Index: "custom-" + input.Collection, Routing: doc["region"].(string), } return } // Filter returns false to skip indexing a document func Filter(input *monstachemap.MapperPluginInput) (bool, error) { doc := input.Document // Skip draft documents if status, ok := doc["status"].(string); ok { return status != "draft", nil } return true, nil } // Process provides access to Elasticsearch client for custom operations func Process(input *monstachemap.ProcessPluginInput) error { // Access Elasticsearch bulk processor for custom indexing // input.ElasticBulkProcessor.Add(...) return nil } ``` -------------------------------- ### Configure Index Mappings Source: https://context7.com/rwynn/monstache/llms.txt Maps specific MongoDB namespaces to target Elasticsearch indices and pipelines. ```toml # Map MongoDB namespaces to custom Elasticsearch indices [[mapping]] namespace = "mydb.users" index = "app-users" pipeline = "user-pipeline" [[mapping]] namespace = "mydb.orders" index = "app-orders" [[mapping]] namespace = "mydb.products" index = "catalog-products" ``` -------------------------------- ### Define Monstache Delete Strategies Source: https://context7.com/rwynn/monstache/llms.txt Configure how document deletions are handled in the target index using TOML configuration. ```toml # Strategy 0: Stateless (default) - searches Elasticsearch to find document delete-strategy = 0 delete-index-pattern = "mydb.*" # Strategy 1: Stateful - stores routing metadata in MongoDB delete-strategy = 1 config-database-name = "monstache" # Strategy 2: Ignore - never delete from Elasticsearch delete-strategy = 2 # Disable delete protection for bulk deletes disable-delete-protection = true ``` -------------------------------- ### Configure Time Machine Feature Source: https://context7.com/rwynn/monstache/llms.txt Maintain document version history in Elasticsearch by specifying namespaces and index naming patterns. ```toml # Enable time machine for audit logging time-machine-namespaces = ["mydb.users", "mydb.orders"] time-machine-index-prefix = "audit" time-machine-index-suffix = "2006-01-02" time-machine-direct-reads = true # Custom oplog time fields index-oplog-time = true oplog-ts-field-name = "sync_ts" oplog-date-field-name = "sync_date" oplog-date-field-format = "2006/01/02 15:04:05" ``` -------------------------------- ### Configure Relationship Resolution Source: https://context7.com/rwynn/monstache/llms.txt Denormalize data by embedding related documents from other collections using the relate configuration block. ```toml # Embed user data into order documents [[relate]] namespace = "mydb.orders" with-namespace = "mydb.users" src-field = "userId" match-field = "_id" keep-src = true # Embed product data into order items [[relate]] namespace = "mydb.order_items" with-namespace = "mydb.products" src-field = "productId" match-field = "_id" dot-notation = true max-depth = 2 # Convert field types when matching [[relate]] namespace = "mydb.comments" with-namespace = "mydb.posts" src-field = "postId" match-field = "_id" match-field-type = "objectId" ``` -------------------------------- ### Configure Monstache Cluster Mode Source: https://context7.com/rwynn/monstache/llms.txt Enable high availability and distributed processing by defining cluster names and worker nodes. ```toml # Primary instance configuration cluster-name = "sync-cluster" resume = true resume-name = "cluster-primary" # Worker configuration for distributed processing worker = "worker-1" workers = ["worker-1", "worker-2", "worker-3"] # GTM settings for change stream buffering [gtm-settings] channel-size = 512 buffer-size = 32 buffer-duration = "75ms" max-await-time = "5s" ``` -------------------------------- ### Query HTTP Server Endpoints Source: https://context7.com/rwynn/monstache/llms.txt Interact with the Monstache HTTP server to retrieve health, status, statistics, and pprof profiling data. ```bash # Health check endpoint curl http://localhost:8080/healthz # Response: ok # Startup time endpoint curl http://localhost:8080/started # Response: 2h30m15s # Instance status endpoint curl http://localhost:8080/instance # Response: # { # "enabled": true, # "pid": 12345, # "hostname": "monstache-1", # "cluster": "my-cluster", # "resumeName": "default", # "lastTs": {"T": 1234567890, "I": 1}, # "lastTsFormat": "2024-01-15T10:30:00" # } # Statistics endpoint (requires stats = true) curl http://localhost:8080/stats # Response: Elasticsearch bulk processor statistics # pprof endpoints (requires pprof = true) curl http://localhost:8080/debug/pprof/ curl http://localhost:8080/debug/pprof/heap curl http://localhost:8080/debug/pprof/profile ``` -------------------------------- ### Transform Documents with JavaScript Source: https://context7.com/rwynn/monstache/llms.txt Uses JavaScript to modify documents or filter them before indexing into Elasticsearch. ```toml # Transform documents with JavaScript [[script]] namespace = "mydb.users" script = """ module.exports = function(doc, ns, updateDesc) { // Add computed fields doc.fullName = doc.firstName + ' ' + doc.lastName; doc.searchable = doc.email.toLowerCase(); // Remove sensitive fields delete doc.password; delete doc.ssn; // Add metadata doc._meta_monstache = { index: 'users-' + new Date().getFullYear(), routing: doc.region }; return doc; } """ # Filter documents (return false to skip) [[filter]] namespace = "mydb.events" script = """ module.exports = function(doc, ns) { // Only index published events return doc.status === 'published'; } """ # Load script from file [[script]] namespace = "mydb.orders" path = "/path/to/transform.js" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.